001/**
002 * Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package org.javamoney.moneta.convert.internal;
017
018import java.math.BigDecimal;
019import java.net.MalformedURLException;
020
021import javax.money.convert.*;
022
023import org.javamoney.moneta.ExchangeRateBuilder;
024import org.javamoney.moneta.spi.AbstractRateProvider;
025import org.javamoney.moneta.spi.DefaultNumberValue;
026
027/**
028 * This class implements an {@link javax.money.convert.ExchangeRateProvider} that provides exchange rate with factor
029 * one for identical base/term currencies.
030 *
031 * @author Anatole Tresch
032 * @author Werner Keil
033 */
034public class IdentityRateProvider extends AbstractRateProvider {
035
036    /**
037     * The {@link javax.money.convert.ConversionContext} of this provider.
038     */
039    private static final ProviderContext CONTEXT =
040            ProviderContextBuilder.of("IDENT", RateType.OTHER).set("providerDescription", "Identitiy Provider").build();
041
042    /**
043     * Constructor, also loads initial data.
044     *
045     * @throws java.net.MalformedURLException
046     */
047    public IdentityRateProvider() throws MalformedURLException {
048        super(CONTEXT);
049    }
050
051    /**
052     * Check if this provider can provide a rate, which is only the case if base and term are equal.
053     *
054     * @param conversionQuery the required {@link ConversionQuery}, not {@code null}
055     * @return true, if the contained base and term currencies are known to this provider.
056     */
057    public boolean isAvailable(ConversionQuery conversionQuery) {
058        return conversionQuery.getBaseCurrency().getCurrencyCode()
059                .equals(conversionQuery.getCurrency().getCurrencyCode());
060    }
061
062    public ExchangeRate getExchangeRate(ConversionQuery query) {
063        if (query.getBaseCurrency().getCurrencyCode().equals(query.getCurrency().getCurrencyCode())) {
064            ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER)
065                    .setBase(query.getBaseCurrency());
066            builder.setTerm(query.getCurrency());
067            builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE));
068            return builder.build();
069        }
070        return null;
071    }
072
073    /*
074     * (non-Javadoc)
075         *
076         * @see
077         * javax.money.convert.ExchangeRateProvider#getReversed(javax.money.convert
078         * .ExchangeRate)
079         */
080    @Override
081    public ExchangeRate getReversed(ExchangeRate rate) {
082        if (rate.getContext().getProviderName().equals(CONTEXT.getProviderName())) {
083            return new ExchangeRateBuilder(rate.getContext()).setTerm(rate.getBaseCurrency())
084                    .setBase(rate.getCurrency()).setFactor(new DefaultNumberValue(BigDecimal.ONE)).build();
085        }
086        return null;
087    }
088
089}