001/*
002  Copyright (c) 2012, 2020, 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;
017
018import java.math.BigDecimal;
019
020import javax.money.convert.ConversionQuery;
021import javax.money.convert.ExchangeRate;
022import javax.money.convert.ProviderContext;
023import javax.money.convert.ProviderContextBuilder;
024import javax.money.convert.RateType;
025
026import org.javamoney.moneta.convert.ExchangeRateBuilder;
027import org.javamoney.moneta.spi.AbstractRateProvider;
028import org.javamoney.moneta.spi.DefaultNumberValue;
029
030/**
031 * This class implements an {@link javax.money.convert.ExchangeRateProvider} that provides exchange rate with factor
032 * one for identical base/term currencies.
033 *
034 * @author Anatole Tresch
035 * @author Werner Keil
036 */
037public class IdentityRateProvider extends AbstractRateProvider {
038
039    /**
040     * The {@link javax.money.convert.ConversionContext} of this provider.
041     */
042    private static final ProviderContext CONTEXT =
043            ProviderContextBuilder.of("IDENT", RateType.OTHER).set("providerDescription", "Identitiy Provider").build();
044
045    /**
046     * Constructor, also loads initial data.
047     */
048    public IdentityRateProvider() {
049        super(CONTEXT);
050    }
051
052    /**
053     * Check if this provider can provide a rate, which is only the case if base and term are equal.
054     *
055     * @param conversionQuery the required {@link ConversionQuery}, not {@code null}
056     * @return true, if the contained base and term currencies are known to this provider.
057     */
058    @Override
059        public boolean isAvailable(ConversionQuery conversionQuery) {
060        return conversionQuery.getBaseCurrency().getCurrencyCode()
061                .equals(conversionQuery.getCurrency().getCurrencyCode());
062    }
063
064    @Override
065        public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
066        if (conversionQuery.getBaseCurrency().getCurrencyCode().equals(conversionQuery.getCurrency().getCurrencyCode())) {
067            ExchangeRateBuilder builder = new ExchangeRateBuilder(getContext().getProviderName(), RateType.OTHER)
068                    .setBase(conversionQuery.getBaseCurrency());
069            builder.setTerm(conversionQuery.getCurrency());
070            builder.setFactor(DefaultNumberValue.of(BigDecimal.ONE));
071            return builder.build();
072        }
073        return null;
074    }
075
076    /*
077     * (non-Javadoc)
078         *
079         * @see
080         * javax.money.convert.ExchangeRateProvider#getReversed(javax.money.convert
081         * .ExchangeRate)
082         */
083    @Override
084    public ExchangeRate getReversed(ExchangeRate rate) {
085        if (rate.getContext().getProviderName().equals(CONTEXT.getProviderName())) {
086            return new ExchangeRateBuilder(rate.getContext()).setTerm(rate.getBaseCurrency())
087                    .setBase(rate.getCurrency()).setFactor(new DefaultNumberValue(BigDecimal.ONE)).build();
088        }
089        return null;
090    }
091
092}