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.spi; 017 018import javax.money.CurrencyUnit; 019import javax.money.MonetaryAmount; 020import javax.money.convert.ConversionContext; 021import javax.money.convert.CurrencyConversion; 022import javax.money.convert.ExchangeRate; 023import javax.money.convert.ExchangeRateProvider; 024 025/** 026 * This class defines a {@link CurrencyConversion} that is converting to a 027 * specific target {@link CurrencyUnit}. Each instance of this class is bound to 028 * a specific {@link ExchangeRateProvider}, a term {@link CurrencyUnit} and a 029 * target timestamp. 030 * 031 * @author Anatole Tresch 032 */ 033public class LazyBoundCurrencyConversion extends AbstractCurrencyConversion implements CurrencyConversion{ 034 035 private ExchangeRateProvider rateProvider; 036 037 public LazyBoundCurrencyConversion(CurrencyUnit termCurrency, ExchangeRateProvider rateProvider, 038 ConversionContext conversionContext){ 039 super(termCurrency, conversionContext); 040 this.rateProvider = rateProvider; 041 } 042 043 /** 044 * Get the exchange rate type that this provider instance is providing data 045 * for. 046 * 047 * @return the exchange rate type if this instance. 048 */ 049 @Override 050 public ExchangeRate getExchangeRate(MonetaryAmount amount){ 051 return this.rateProvider.getExchangeRate(amount.getCurrency(), getCurrency()); 052 } 053 054 /* 055 * (non-Javadoc) 056 * 057 * @see 058 * org.javamoney.moneta.conversion.AbstractCurrencyConversion#with(javax 059 * .money.convert.ConversionContext) 060 */ 061 public CurrencyConversion with(ConversionContext conversionContext){ 062 return new LazyBoundCurrencyConversion(getCurrency(), rateProvider, conversionContext); 063 } 064 065 /* 066 * (non-Javadoc) 067 * 068 * @see java.lang.Object#toString() 069 */ 070 @Override 071 public String toString(){ 072 return "CurrencyConversion [MonetaryAmount -> MonetaryAmount; provider=" + rateProvider + ", context=" + 073 getConversionContext() + ", termCurrency=" + getCurrency() + "]"; 074 } 075 076}