japod@56: package org.apidesign.apifest08.test; japod@56: japod@56: import java.math.BigDecimal; japod@56: import java.util.Currency; japod@56: japod@56: import org.apidesign.apifest08.currency.ConvertorFactory; japod@56: import org.apidesign.apifest08.currency.ExtendedConvertor; japod@56: import org.apidesign.apifest08.currency.Money; japod@56: import org.apidesign.apifest08.currency.MoneyImpl; japod@56: japod@56: /** japod@56: * Third party implementation of the convertor. IT IS NOT THREAD SAFE. japod@56: * @author lukas japod@56: * japod@56: */ japod@56: class OnlineConvertor implements ExtendedConvertor { japod@56: japod@56: private static final BigDecimal LOWER_LIMIT = BigDecimal.valueOf(15); japod@56: private static final BigDecimal HIGHER_LIMIT = BigDecimal.valueOf(16); japod@56: japod@56: private static final Currency USD = Currency.getInstance("USD"); japod@56: private static final Currency CZK = Currency.getInstance("CZK"); japod@56: private static final MoneyImpl ONE_USD = new MoneyImpl(1,USD); japod@56: japod@56: private ExtendedConvertor wrappedConvertor = ConvertorFactory.createConvertor(ONE_USD, new MoneyImpl(16, CZK)); japod@56: japod@56: private BigDecimal increment = new BigDecimal("-0.01"); japod@56: japod@56: public boolean isConversionSupported(Currency from, Currency to) { japod@56: return wrappedConvertor.isConversionSupported(from, to); japod@56: } japod@56: japod@56: public Money convert(Money amount, Currency destinationCurrency) japod@56: throws IllegalArgumentException { japod@56: Money result = wrappedConvertor.convert(amount, destinationCurrency); japod@56: updateConvertor(); japod@56: return result; japod@56: } japod@56: japod@56: /** japod@56: * Prepares convertor for the next conversion. japod@56: */ japod@56: private void updateConvertor() { japod@56: BigDecimal currentRate = wrappedConvertor.convert(ONE_USD, CZK).getAmount(); japod@56: BigDecimal newRate = currentRate.add(increment); japod@56: japod@56: if (LOWER_LIMIT.compareTo(newRate)==0 || HIGHER_LIMIT.compareTo(newRate)==0) japod@56: { japod@56: increment = increment.negate(); japod@56: } japod@56: wrappedConvertor = ConvertorFactory.createConvertor(ONE_USD, new MoneyImpl(newRate, CZK)); japod@56: } japod@56: japod@56: }