1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import java.util.Currency;
6 import org.apidesign.apifest08.currency.ConvertorFactory;
7 import org.apidesign.apifest08.currency.ExtendedConvertor;
8 import org.apidesign.apifest08.currency.Money;
9 import org.apidesign.apifest08.currency.MoneyImpl;
12 * Third party implementation of the convertor. IT IS NOT THREAD SAFE.
16 class OnlineConvertor implements ExtendedConvertor {
18 private static final BigDecimal LOWER_LIMIT = BigDecimal.valueOf(15);
19 private static final BigDecimal HIGHER_LIMIT = BigDecimal.valueOf(16);
21 private static final Currency USD = Currency.getInstance("USD");
22 private static final Currency CZK = Currency.getInstance("CZK");
23 private static final MoneyImpl ONE_USD = new MoneyImpl(1,USD);
25 private ExtendedConvertor wrappedConvertor = ConvertorFactory.createConvertor(ONE_USD, new MoneyImpl(16, CZK));
27 private BigDecimal increment = new BigDecimal("-0.01");
29 public boolean isConversionSupported(Currency from, Currency to) {
30 return wrappedConvertor.isConversionSupported(from, to);
33 public Money convert(Money amount, Currency destinationCurrency)
34 throws IllegalArgumentException {
35 Money result = wrappedConvertor.convert(amount, destinationCurrency);
41 * Prepares convertor for the next conversion.
43 private void updateConvertor() {
44 BigDecimal currentRate = wrappedConvertor.convert(ONE_USD, CZK).getAmount();
45 BigDecimal newRate = currentRate.add(increment);
47 if (LOWER_LIMIT.compareTo(newRate)==0 || HIGHER_LIMIT.compareTo(newRate)==0)
49 increment = increment.negate();
51 wrappedConvertor = ConvertorFactory.createConvertor(ONE_USD, new MoneyImpl(newRate, CZK));