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.internal.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; 025import org.javamoney.moneta.ExchangeRateBuilder; 026import org.javamoney.moneta.spi.AbstractRateProvider; 027import org.javamoney.moneta.spi.DefaultNumberValue; 028 029/** 030 * This class implements an {@link javax.money.convert.ExchangeRateProvider} that provides exchange rate with factor 031 * one for identical base/term currencies. 032 * 033 * @author Anatole Tresch 034 * @author Werner Keil 035 */ 036public class IdentityRateProvider extends AbstractRateProvider { 037 038 /** 039 * The {@link javax.money.convert.ConversionContext} of this provider. 040 */ 041 private static final ProviderContext CONTEXT = 042 ProviderContextBuilder.of("IDENT", RateType.OTHER).set("providerDescription", "Identity Provider").build(); 043 044 /** 045 * Constructor, also loads initial data. 046 */ 047 public IdentityRateProvider(){ 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 * ExchangeRateProvider#getReversed(org.javamoney.bp.api.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}