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