1.1 --- a/task2/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java Wed Oct 01 10:43:05 2008 +0200
1.2 +++ b/task2/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java Tue Oct 07 00:19:37 2008 +0200
1.3 @@ -11,9 +11,9 @@
1.4 * @author lukas
1.5 *
1.6 */
1.7 -class DefaultConvertor implements Convertor, Serializable {
1.8 +class DefaultConvertor implements ExtendedConvertor, Serializable {
1.9
1.10 - private static final long serialVersionUID = -1754789142402148099L;
1.11 + private static final long serialVersionUID = 6660014633685135034L;
1.12
1.13 /**
1.14 * Equivalent in source currency.
1.15 @@ -29,20 +29,20 @@
1.16
1.17 private final Currency destinationCurrency;
1.18
1.19 - public DefaultConvertor(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
1.20 + public DefaultConvertor(Money sourceEquivalent, Money destinationEquivalent) {
1.21 super();
1.22 - if (BigDecimal.ZERO.compareTo(sourceEquivalent)==0)
1.23 + if (BigDecimal.ZERO.compareTo(sourceEquivalent.getAmount())==0)
1.24 {
1.25 throw new IllegalArgumentException("Source equivalent amount can not be 0.");
1.26 }
1.27 - if (BigDecimal.ZERO.compareTo(destinationEquivalent)==0)
1.28 + if (BigDecimal.ZERO.compareTo(destinationEquivalent.getAmount())==0)
1.29 {
1.30 throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
1.31 }
1.32 - this.sourceEquivalent = sourceEquivalent;
1.33 - this.destinationEquivalent = destinationEquivalent;
1.34 - this.sourceCurrency = sourceCurrency;
1.35 - this.destinationCurrency = destinationCurrency;
1.36 + this.sourceEquivalent = sourceEquivalent.getAmount();
1.37 + this.destinationEquivalent = destinationEquivalent.getAmount();
1.38 + this.sourceCurrency = sourceEquivalent.getCurrency();
1.39 + this.destinationCurrency = destinationEquivalent.getCurrency();
1.40 }
1.41
1.42 public Money convert(Money amount, Currency destinationCurrency) {
1.43 @@ -54,10 +54,6 @@
1.44 {
1.45 throw new NullPointerException("destionationCurrency is null");
1.46 }
1.47 - if (isConversionInOpositeDirection(amount, destinationCurrency))
1.48 - {
1.49 - return revert().convert(amount, destinationCurrency);
1.50 - }
1.51 if (!amount.getCurrency().equals(getSourceCurrency()))
1.52 {
1.53 throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
1.54 @@ -70,20 +66,9 @@
1.55 BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
1.56 return new MoneyImpl(destinationAmount, getDestinationCurrency());
1.57 }
1.58 -
1.59 - /**
1.60 - * Returns true, if the conversion is in oposit direction.
1.61 - * @param amount
1.62 - * @param destinationCurrency
1.63 - * @return
1.64 - */
1.65 - private boolean isConversionInOpositeDirection(Money amount, Currency destinationCurrency) {
1.66 - return amount.getCurrency().equals(getDestinationCurrency()) && destinationCurrency.equals(getSourceCurrency());
1.67 - }
1.68 -
1.69 -
1.70 - public Convertor revert() {
1.71 - return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);
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 @@ -107,58 +92,4 @@
1.79 return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
1.80 +getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
1.81 }
1.82 -
1.83 - @Override
1.84 - public int hashCode() {
1.85 - final int prime = 31;
1.86 - int result = 1;
1.87 - result = prime
1.88 - * result
1.89 - + ((destinationCurrency == null) ? 0 : destinationCurrency
1.90 - .hashCode());
1.91 - result = prime
1.92 - * result
1.93 - + ((destinationEquivalent == null) ? 0 : destinationEquivalent
1.94 - .hashCode());
1.95 - result = prime * result
1.96 - + ((sourceCurrency == null) ? 0 : sourceCurrency.hashCode());
1.97 - result = prime
1.98 - * result
1.99 - + ((sourceEquivalent == null) ? 0 : sourceEquivalent.hashCode());
1.100 - return result;
1.101 - }
1.102 -
1.103 - @Override
1.104 - public boolean equals(Object obj) {
1.105 - if (this == obj)
1.106 - return true;
1.107 - if (obj == null)
1.108 - return false;
1.109 - if (!(obj instanceof DefaultConvertor))
1.110 - return false;
1.111 - DefaultConvertor other = (DefaultConvertor) obj;
1.112 - if (destinationCurrency == null) {
1.113 - if (other.destinationCurrency != null)
1.114 - return false;
1.115 - } else if (!destinationCurrency.equals(other.destinationCurrency))
1.116 - return false;
1.117 - if (destinationEquivalent == null) {
1.118 - if (other.destinationEquivalent != null)
1.119 - return false;
1.120 - } else if (!destinationEquivalent.equals(other.destinationEquivalent))
1.121 - return false;
1.122 - if (sourceCurrency == null) {
1.123 - if (other.sourceCurrency != null)
1.124 - return false;
1.125 - } else if (!sourceCurrency.equals(other.sourceCurrency))
1.126 - return false;
1.127 - if (sourceEquivalent == null) {
1.128 - if (other.sourceEquivalent != null)
1.129 - return false;
1.130 - } else if (!sourceEquivalent.equals(other.sourceEquivalent))
1.131 - return false;
1.132 - return true;
1.133 - }
1.134 -
1.135 -
1.136 }