1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,164 @@
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 Convertor, Serializable {
1.18 +
1.19 + private static final long serialVersionUID = -1754789142402148099L;
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(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
1.36 + super();
1.37 + if (BigDecimal.ZERO.compareTo(sourceEquivalent)==0)
1.38 + {
1.39 + throw new IllegalArgumentException("Source equivalent amount can not be 0.");
1.40 + }
1.41 + if (BigDecimal.ZERO.compareTo(destinationEquivalent)==0)
1.42 + {
1.43 + throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
1.44 + }
1.45 + this.sourceEquivalent = sourceEquivalent;
1.46 + this.destinationEquivalent = destinationEquivalent;
1.47 + this.sourceCurrency = sourceCurrency;
1.48 + this.destinationCurrency = destinationCurrency;
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 (isConversionInOpositeDirection(amount, destinationCurrency))
1.61 + {
1.62 + return revert().convert(amount, destinationCurrency);
1.63 + }
1.64 + if (!amount.getCurrency().equals(getSourceCurrency()))
1.65 + {
1.66 + throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
1.67 + }
1.68 + if (!getDestinationCurrency().equals(destinationCurrency))
1.69 + {
1.70 + throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
1.71 + }
1.72 + BigDecimal sourceAmount = amount.getAmount();
1.73 + BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
1.74 + return new MoneyImpl(destinationAmount, getDestinationCurrency());
1.75 + }
1.76 +
1.77 + /**
1.78 + * Returns true, if the conversion is in oposit direction.
1.79 + * @param amount
1.80 + * @param destinationCurrency
1.81 + * @return
1.82 + */
1.83 + private boolean isConversionInOpositeDirection(Money amount, Currency destinationCurrency) {
1.84 + return amount.getCurrency().equals(getDestinationCurrency()) && destinationCurrency.equals(getSourceCurrency());
1.85 + }
1.86 +
1.87 +
1.88 + public Convertor revert() {
1.89 + return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);
1.90 + }
1.91 +
1.92 + public BigDecimal getSourceEquivalent() {
1.93 + return sourceEquivalent;
1.94 + }
1.95 +
1.96 + public BigDecimal getDestinationEquivalent() {
1.97 + return destinationEquivalent;
1.98 + }
1.99 +
1.100 + public Currency getSourceCurrency() {
1.101 + return sourceCurrency;
1.102 + }
1.103 +
1.104 + public Currency getDestinationCurrency() {
1.105 + return destinationCurrency;
1.106 + }
1.107 +
1.108 + @Override
1.109 + public String toString() {
1.110 + return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
1.111 + +getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
1.112 + }
1.113 +
1.114 + @Override
1.115 + public int hashCode() {
1.116 + final int prime = 31;
1.117 + int result = 1;
1.118 + result = prime
1.119 + * result
1.120 + + ((destinationCurrency == null) ? 0 : destinationCurrency
1.121 + .hashCode());
1.122 + result = prime
1.123 + * result
1.124 + + ((destinationEquivalent == null) ? 0 : destinationEquivalent
1.125 + .hashCode());
1.126 + result = prime * result
1.127 + + ((sourceCurrency == null) ? 0 : sourceCurrency.hashCode());
1.128 + result = prime
1.129 + * result
1.130 + + ((sourceEquivalent == null) ? 0 : sourceEquivalent.hashCode());
1.131 + return result;
1.132 + }
1.133 +
1.134 + @Override
1.135 + public boolean equals(Object obj) {
1.136 + if (this == obj)
1.137 + return true;
1.138 + if (obj == null)
1.139 + return false;
1.140 + if (!(obj instanceof DefaultConvertor))
1.141 + return false;
1.142 + DefaultConvertor other = (DefaultConvertor) obj;
1.143 + if (destinationCurrency == null) {
1.144 + if (other.destinationCurrency != null)
1.145 + return false;
1.146 + } else if (!destinationCurrency.equals(other.destinationCurrency))
1.147 + return false;
1.148 + if (destinationEquivalent == null) {
1.149 + if (other.destinationEquivalent != null)
1.150 + return false;
1.151 + } else if (!destinationEquivalent.equals(other.destinationEquivalent))
1.152 + return false;
1.153 + if (sourceCurrency == null) {
1.154 + if (other.sourceCurrency != null)
1.155 + return false;
1.156 + } else if (!sourceCurrency.equals(other.sourceCurrency))
1.157 + return false;
1.158 + if (sourceEquivalent == null) {
1.159 + if (other.sourceEquivalent != null)
1.160 + return false;
1.161 + } else if (!sourceEquivalent.equals(other.sourceEquivalent))
1.162 + return false;
1.163 + return true;
1.164 + }
1.165 +
1.166 +
1.167 +}