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    @Override
055    public ExchangeRateProvider getExchangeRateProvider() {
056        return this.rateProvider;
057    }
058
059    /*
060     * (non-Javadoc)
061     *
062     * @see
063     * org.javamoney.moneta.conversion.AbstractCurrencyConversion#with(javax
064     * .money.convert.ConversionContext)
065     */
066    public CurrencyConversion with(ConversionContext conversionContext) {
067        return new LazyBoundCurrencyConversion(getCurrency(), rateProvider, conversionContext);
068    }
069
070    /*
071     * (non-Javadoc)
072     *
073     * @see java.lang.Object#toString()
074     */
075    @Override
076    public String toString() {
077        return "CurrencyConversion [MonetaryAmount -> MonetaryAmount; provider=" + rateProvider + ", context=" +
078                getConversionContext() + ", termCurrency=" + getCurrency() + ']';
079    }
080
081}