001/* 002 * Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil. Licensed under the Apache 003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the 004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 005 * Unless required by applicable law or agreed to in writing, software distributed under the License 006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 007 * or implied. See the License for the specific language governing permissions and limitations under 008 * the License. 009 */ 010package org.javamoney.moneta.spi; 011 012import javax.money.CurrencyUnit; 013import javax.money.MonetaryAmount; 014import javax.money.convert.ConversionContext; 015import javax.money.convert.CurrencyConversion; 016import javax.money.convert.ExchangeRate; 017import javax.money.convert.ExchangeRateProvider; 018 019/** 020 * This class defines a {@link CurrencyConversion} that is converting to a 021 * specific target {@link CurrencyUnit}. Each instance of this class is bound to 022 * a specific {@link ExchangeRateProvider}, a term {@link CurrencyUnit} and a 023 * target timestamp. 024 * 025 * @author Anatole Tresch 026 */ 027public class LazyBoundCurrencyConversion extends AbstractCurrencyConversion 028 implements CurrencyConversion { 029 030 private ExchangeRateProvider rateProvider; 031 032 public LazyBoundCurrencyConversion(CurrencyUnit termCurrency, 033 ExchangeRateProvider rateProvider, 034 ConversionContext conversionContext) { 035 super(termCurrency, conversionContext); 036 this.rateProvider = rateProvider; 037 } 038 039 /** 040 * Get the exchange rate type that this provider instance is providing data 041 * for. 042 * 043 * @return the exchange rate type if this instance. 044 */ 045 @Override 046 public ExchangeRate getExchangeRate(MonetaryAmount amount) { 047 return this.rateProvider.getExchangeRate(amount.getCurrency(), 048 getTermCurrency(), getConversionContext()); 049 } 050 051 /* 052 * (non-Javadoc) 053 * 054 * @see 055 * org.javamoney.moneta.conversion.AbstractCurrencyConversion#with(javax 056 * .money.convert.ConversionContext) 057 */ 058 public CurrencyConversion with(ConversionContext conversionContext) { 059 return new LazyBoundCurrencyConversion(getTermCurrency(), rateProvider, 060 conversionContext); 061 } 062 063 /* 064 * (non-Javadoc) 065 * 066 * @see java.lang.Object#toString() 067 */ 068 @Override 069 public String toString() { 070 return "CurrencyConversion [MonetaryAmount -> MonetaryAmount; provider=" 071 + rateProvider 072 + ", context=" 073 + getConversionContext() 074 + ", termCurrency=" + getTermCurrency() + "]"; 075 } 076 077}