1.1 --- a/task4/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java Fri Oct 17 17:40:14 2008 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,95 +0,0 @@
1.4 -package org.apidesign.apifest08.currency;
1.5 -
1.6 -import java.io.Serializable;
1.7 -import java.math.BigDecimal;
1.8 -import java.math.RoundingMode;
1.9 -import java.util.Currency;
1.10 -
1.11 -/**
1.12 - * Default {@link Convertor} implementation. Exchange rate is stored as equivalents. It means if we have USD to CZK convertor and USD1 = CZK17
1.13 - * we store 1 in sourceEquivalent and 17 in destinationEquivalent. This class is immutable.
1.14 - * @author lukas
1.15 - *
1.16 - */
1.17 -class DefaultConvertor implements ExtendedConvertor, Serializable {
1.18 -
1.19 - private static final long serialVersionUID = 6660014633685135034L;
1.20 -
1.21 - /**
1.22 - * Equivalent in source currency.
1.23 - */
1.24 - private final BigDecimal sourceEquivalent;
1.25 -
1.26 - /**
1.27 - * Equivalent in destination currency.
1.28 - */
1.29 - private final BigDecimal destinationEquivalent;
1.30 -
1.31 - private final Currency sourceCurrency;
1.32 -
1.33 - private final Currency destinationCurrency;
1.34 -
1.35 - public DefaultConvertor(Money sourceEquivalent, Money destinationEquivalent) {
1.36 - super();
1.37 - if (BigDecimal.ZERO.compareTo(sourceEquivalent.getAmount())==0)
1.38 - {
1.39 - throw new IllegalArgumentException("Source equivalent amount can not be 0.");
1.40 - }
1.41 - if (BigDecimal.ZERO.compareTo(destinationEquivalent.getAmount())==0)
1.42 - {
1.43 - throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
1.44 - }
1.45 - this.sourceEquivalent = sourceEquivalent.getAmount();
1.46 - this.destinationEquivalent = destinationEquivalent.getAmount();
1.47 - this.sourceCurrency = sourceEquivalent.getCurrency();
1.48 - this.destinationCurrency = destinationEquivalent.getCurrency();
1.49 - }
1.50 -
1.51 - public Money convert(Money amount, Currency destinationCurrency) {
1.52 - if (amount==null)
1.53 - {
1.54 - throw new NullPointerException("Money is null");
1.55 - }
1.56 - if (destinationCurrency==null)
1.57 - {
1.58 - throw new NullPointerException("destionationCurrency is null");
1.59 - }
1.60 - if (!amount.getCurrency().equals(getSourceCurrency()))
1.61 - {
1.62 - throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
1.63 - }
1.64 - if (!getDestinationCurrency().equals(destinationCurrency))
1.65 - {
1.66 - throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
1.67 - }
1.68 - BigDecimal sourceAmount = amount.getAmount();
1.69 - BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
1.70 - return new MoneyImpl(destinationAmount, getDestinationCurrency());
1.71 - }
1.72 -
1.73 - public boolean isConversionSupported(Currency from, Currency to) {
1.74 - return sourceCurrency.equals(from) && destinationCurrency.equals(to);
1.75 - }
1.76 -
1.77 - public BigDecimal getSourceEquivalent() {
1.78 - return sourceEquivalent;
1.79 - }
1.80 -
1.81 - public BigDecimal getDestinationEquivalent() {
1.82 - return destinationEquivalent;
1.83 - }
1.84 -
1.85 - public Currency getSourceCurrency() {
1.86 - return sourceCurrency;
1.87 - }
1.88 -
1.89 - public Currency getDestinationCurrency() {
1.90 - return destinationCurrency;
1.91 - }
1.92 -
1.93 - @Override
1.94 - public String toString() {
1.95 - return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
1.96 - +getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
1.97 - }
1.98 -}