author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Sat, 11 Oct 2008 23:38:46 +0200 | |
changeset 61 | 58ec6da75f6f |
parent 45 | task3/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java@251d0ed461fb |
permissions | -rw-r--r-- |
japod@6 | 1 |
package org.apidesign.apifest08.currency; |
japod@6 | 2 |
|
japod@6 | 3 |
import java.io.Serializable; |
japod@6 | 4 |
import java.math.BigDecimal; |
japod@6 | 5 |
import java.math.RoundingMode; |
japod@6 | 6 |
import java.util.Currency; |
japod@6 | 7 |
|
japod@6 | 8 |
/** |
japod@6 | 9 |
* Default {@link Convertor} implementation. Exchange rate is stored as equivalents. It means if we have USD to CZK convertor and USD1 = CZK17 |
japod@6 | 10 |
* we store 1 in sourceEquivalent and 17 in destinationEquivalent. This class is immutable. |
japod@6 | 11 |
* @author lukas |
japod@6 | 12 |
* |
japod@6 | 13 |
*/ |
japod@34 | 14 |
class DefaultConvertor implements ExtendedConvertor, Serializable { |
japod@6 | 15 |
|
japod@34 | 16 |
private static final long serialVersionUID = 6660014633685135034L; |
japod@6 | 17 |
|
japod@6 | 18 |
/** |
japod@6 | 19 |
* Equivalent in source currency. |
japod@6 | 20 |
*/ |
japod@6 | 21 |
private final BigDecimal sourceEquivalent; |
japod@6 | 22 |
|
japod@6 | 23 |
/** |
japod@6 | 24 |
* Equivalent in destination currency. |
japod@6 | 25 |
*/ |
japod@6 | 26 |
private final BigDecimal destinationEquivalent; |
japod@6 | 27 |
|
japod@6 | 28 |
private final Currency sourceCurrency; |
japod@6 | 29 |
|
japod@6 | 30 |
private final Currency destinationCurrency; |
japod@6 | 31 |
|
japod@34 | 32 |
public DefaultConvertor(Money sourceEquivalent, Money destinationEquivalent) { |
japod@6 | 33 |
super(); |
japod@34 | 34 |
if (BigDecimal.ZERO.compareTo(sourceEquivalent.getAmount())==0) |
japod@16 | 35 |
{ |
japod@16 | 36 |
throw new IllegalArgumentException("Source equivalent amount can not be 0."); |
japod@16 | 37 |
} |
japod@34 | 38 |
if (BigDecimal.ZERO.compareTo(destinationEquivalent.getAmount())==0) |
japod@16 | 39 |
{ |
japod@16 | 40 |
throw new IllegalArgumentException("Destination equivalent amount can not be 0."); |
japod@16 | 41 |
} |
japod@34 | 42 |
this.sourceEquivalent = sourceEquivalent.getAmount(); |
japod@34 | 43 |
this.destinationEquivalent = destinationEquivalent.getAmount(); |
japod@34 | 44 |
this.sourceCurrency = sourceEquivalent.getCurrency(); |
japod@34 | 45 |
this.destinationCurrency = destinationEquivalent.getCurrency(); |
japod@6 | 46 |
} |
japod@6 | 47 |
|
japod@16 | 48 |
public Money convert(Money amount, Currency destinationCurrency) { |
japod@16 | 49 |
if (amount==null) |
japod@6 | 50 |
{ |
japod@6 | 51 |
throw new NullPointerException("Money is null"); |
japod@6 | 52 |
} |
japod@16 | 53 |
if (destinationCurrency==null) |
japod@6 | 54 |
{ |
japod@16 | 55 |
throw new NullPointerException("destionationCurrency is null"); |
japod@6 | 56 |
} |
japod@16 | 57 |
if (!amount.getCurrency().equals(getSourceCurrency())) |
japod@16 | 58 |
{ |
japod@16 | 59 |
throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency()); |
japod@16 | 60 |
} |
japod@16 | 61 |
if (!getDestinationCurrency().equals(destinationCurrency)) |
japod@16 | 62 |
{ |
japod@16 | 63 |
throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency()); |
japod@16 | 64 |
} |
japod@16 | 65 |
BigDecimal sourceAmount = amount.getAmount(); |
japod@16 | 66 |
BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP); |
japod@6 | 67 |
return new MoneyImpl(destinationAmount, getDestinationCurrency()); |
japod@6 | 68 |
} |
japod@34 | 69 |
|
japod@34 | 70 |
public boolean isConversionSupported(Currency from, Currency to) { |
japod@34 | 71 |
return sourceCurrency.equals(from) && destinationCurrency.equals(to); |
japod@6 | 72 |
} |
japod@6 | 73 |
|
japod@6 | 74 |
public BigDecimal getSourceEquivalent() { |
japod@6 | 75 |
return sourceEquivalent; |
japod@6 | 76 |
} |
japod@6 | 77 |
|
japod@6 | 78 |
public BigDecimal getDestinationEquivalent() { |
japod@6 | 79 |
return destinationEquivalent; |
japod@6 | 80 |
} |
japod@6 | 81 |
|
japod@6 | 82 |
public Currency getSourceCurrency() { |
japod@6 | 83 |
return sourceCurrency; |
japod@6 | 84 |
} |
japod@6 | 85 |
|
japod@6 | 86 |
public Currency getDestinationCurrency() { |
japod@6 | 87 |
return destinationCurrency; |
japod@6 | 88 |
} |
japod@6 | 89 |
|
japod@6 | 90 |
@Override |
japod@6 | 91 |
public String toString() { |
japod@6 | 92 |
return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" " |
japod@6 | 93 |
+getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent(); |
japod@6 | 94 |
} |
japod@6 | 95 |
} |