1.1 --- a/task4/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java Sat Oct 11 23:38:46 2008 +0200
1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java Fri Oct 17 17:40:14 2008 +0200
1.3 @@ -2,11 +2,7 @@
1.4
1.5
1.6 import java.math.BigDecimal;
1.7 -import java.math.MathContext;
1.8 -import java.math.RoundingMode;
1.9 -import java.util.Collections;
1.10 import java.util.Currency;
1.11 -import java.util.HashSet;
1.12 import java.util.Set;
1.13
1.14
1.15 @@ -14,32 +10,16 @@
1.16 * Convert between two currencies.
1.17 *
1.18 * @author D'Arcy Smith
1.19 - * @version 1.0
1.20 + * @version 1.1
1.21 */
1.22 -final class ConvertorImpl
1.23 - implements Convertor
1.24 +class ConvertorImpl
1.25 + implements ExchangeRateConvertor
1.26 {
1.27 /**
1.28 - * The currency to convert from.
1.29 */
1.30 - private final Currency currencyA;
1.31 + private final ExchangeRate rate;
1.32
1.33 /**
1.34 - * The currency to convert to.
1.35 - */
1.36 - private final Currency currencyB;
1.37 -
1.38 - /**
1.39 - * The echange rate between a and b.
1.40 - */
1.41 - private final BigDecimal currencyARate;
1.42 -
1.43 - /**
1.44 - * The echange rate between b and a.
1.45 - */
1.46 - private final BigDecimal currencyBRate;
1.47 -
1.48 - /**
1.49 * Constructs a convertor with the specified currencies.
1.50 *
1.51 * @param a the currency to convert from.
1.52 @@ -48,45 +28,14 @@
1.53 * @param bRate the exchage rage between to and from.
1.54 * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
1.55 */
1.56 - public ConvertorImpl(final Currency a,
1.57 - final BigDecimal aRate,
1.58 - final Currency b,
1.59 - final BigDecimal bRate)
1.60 + public ConvertorImpl(final ExchangeRate r)
1.61 {
1.62 - if(a == null)
1.63 + if(r == null)
1.64 {
1.65 - throw new IllegalArgumentException("a cannot be null");
1.66 + throw new IllegalArgumentException("r cannot be null");
1.67 }
1.68
1.69 - if(b == null)
1.70 - {
1.71 - throw new IllegalArgumentException("b cannot be null");
1.72 - }
1.73 -
1.74 - if(aRate == null)
1.75 - {
1.76 - throw new IllegalArgumentException("aRate cannot be null");
1.77 - }
1.78 -
1.79 - if(bRate == null)
1.80 - {
1.81 - throw new IllegalArgumentException("bRate cannot be null");
1.82 - }
1.83 -
1.84 - if(aRate.compareTo(BigDecimal.ZERO) <= 0)
1.85 - {
1.86 - throw new IllegalArgumentException("aRate must be > 0, was: " + aRate);
1.87 - }
1.88 -
1.89 - if(bRate.compareTo(BigDecimal.ZERO) <= 0)
1.90 - {
1.91 - throw new IllegalArgumentException("bRate must be > 0, was: " + bRate);
1.92 - }
1.93 -
1.94 - currencyA = a;
1.95 - currencyB = b;
1.96 - currencyARate = aRate;
1.97 - currencyBRate = bRate;
1.98 + rate = r;
1.99 }
1.100
1.101 /**
1.102 @@ -104,36 +53,7 @@
1.103 final BigDecimal amount)
1.104 throws InvalidConversionException
1.105 {
1.106 - final BigDecimal result;
1.107 -
1.108 - if(amount == null)
1.109 - {
1.110 - throw new IllegalArgumentException("amount cannot be null");
1.111 - }
1.112 -
1.113 - if(from == null)
1.114 - {
1.115 - throw new IllegalArgumentException("from cannot be null");
1.116 - }
1.117 -
1.118 - if(to == null)
1.119 - {
1.120 - throw new IllegalArgumentException("to cannot be null");
1.121 - }
1.122 -
1.123 - if(!(from.equals(currencyA)) && (!(from.equals(currencyB))))
1.124 - {
1.125 - throw new InvalidConversionException("cannot convert from: " + from.getCurrencyCode(), from, currencyA, currencyB);
1.126 - }
1.127 -
1.128 - if(!(to.equals(currencyA)) && (!(to.equals(currencyB))))
1.129 - {
1.130 - throw new InvalidConversionException("cannot convert to: " + to.getCurrencyCode(), to, currencyA, currencyB);
1.131 - }
1.132 -
1.133 - result = amount.multiply(getConversionRate(from, to));
1.134 -
1.135 - return (result.setScale(2, RoundingMode.HALF_DOWN));
1.136 + return (rate.convert(from, to, amount));
1.137 }
1.138
1.139 /**
1.140 @@ -146,18 +66,7 @@
1.141 */
1.142 public boolean canConvert(final Currency from, final Currency to)
1.143 {
1.144 - if(from == null)
1.145 - {
1.146 - throw new IllegalArgumentException("from cannot be null");
1.147 - }
1.148 -
1.149 - if(to == null)
1.150 - {
1.151 - throw new IllegalArgumentException("to cannot be null");
1.152 - }
1.153 -
1.154 - return ((from.equals(currencyA) || from.equals(currencyB)) &&
1.155 - (to.equals(currencyA) || to.equals(currencyB)));
1.156 + return (rate.canConvert(from, to));
1.157 }
1.158
1.159 /**
1.160 @@ -167,13 +76,7 @@
1.161 */
1.162 public Set<Currency> getCurrencies()
1.163 {
1.164 - final Set<Currency> currencies;
1.165 -
1.166 - currencies = new HashSet<Currency>();
1.167 - currencies.add(currencyA);
1.168 - currencies.add(currencyB);
1.169 -
1.170 - return (Collections.unmodifiableSet(currencies));
1.171 + return (rate.getCurrencies());
1.172 }
1.173
1.174 /**
1.175 @@ -189,44 +92,7 @@
1.176 final Currency to)
1.177 throws InvalidConversionException
1.178 {
1.179 - final BigDecimal rate;
1.180 -
1.181 - if(from == null)
1.182 - {
1.183 - throw new IllegalArgumentException("from cannot be null");
1.184 - }
1.185 -
1.186 - if(to == null)
1.187 - {
1.188 - throw new IllegalArgumentException("to cannot be null");
1.189 - }
1.190 -
1.191 - if(from.equals(to))
1.192 - {
1.193 - rate = BigDecimal.ONE;
1.194 - }
1.195 - else
1.196 - {
1.197 - final BigDecimal rateX;
1.198 - final BigDecimal rateY;
1.199 - final BigDecimal temp;
1.200 -
1.201 - if(from.equals(currencyA))
1.202 - {
1.203 - rateX = currencyARate;
1.204 - rateY = currencyBRate;
1.205 - }
1.206 - else
1.207 - {
1.208 - rateX = currencyBRate;
1.209 - rateY = currencyARate;
1.210 - }
1.211 -
1.212 - temp = BigDecimal.ONE.divide(rateX, MathContext.DECIMAL64);
1.213 - rate = temp.multiply(rateY);
1.214 - }
1.215 -
1.216 - return (rate.setScale(20, RoundingMode.HALF_EVEN));
1.217 + return (rate.getConversionRate(from, to));
1.218 }
1.219
1.220 /**
1.221 @@ -250,29 +116,7 @@
1.222
1.223 final ConvertorImpl other = (ConvertorImpl) obj;
1.224
1.225 - // it would be nice if NetBeans could chck to see if the variable is final and guaranteed not to be null... but that
1.226 - // would likely be tricky... but in a NetBeans engineer reads that see if you can do it :-)
1.227 - if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA)))
1.228 - {
1.229 - return false;
1.230 - }
1.231 -
1.232 - if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB)))
1.233 - {
1.234 - return false;
1.235 - }
1.236 -
1.237 - if (this.currencyARate != other.currencyARate && (this.currencyARate == null || !this.currencyARate.equals(other.currencyARate)))
1.238 - {
1.239 - return false;
1.240 - }
1.241 -
1.242 - if (this.currencyBRate != other.currencyBRate && (this.currencyBRate == null || !this.currencyBRate.equals(other.currencyBRate)))
1.243 - {
1.244 - return false;
1.245 - }
1.246 -
1.247 - return true;
1.248 + return (rate.equals(other.rate));
1.249 }
1.250
1.251 /**
1.252 @@ -283,12 +127,7 @@
1.253 @Override
1.254 public int hashCode()
1.255 {
1.256 - int hash = 7;
1.257 - hash = 37 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
1.258 - hash = 37 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
1.259 - hash = 37 * hash + (this.currencyARate != null ? this.currencyARate.hashCode() : 0);
1.260 - hash = 37 * hash + (this.currencyBRate != null ? this.currencyBRate.hashCode() : 0);
1.261 - return hash;
1.262 + return (rate.hashCode());
1.263 }
1.264
1.265 /**
1.266 @@ -299,6 +138,11 @@
1.267 @Override
1.268 public String toString()
1.269 {
1.270 - return (currencyA.getCurrencyCode() + " to " + currencyB.getCurrencyCode());
1.271 + return (rate.getCurrencyA().getCurrencyCode() + " to " + rate.getCurrencyB().getCurrencyCode());
1.272 + }
1.273 +
1.274 + public ExchangeRate getExchangeRate()
1.275 + {
1.276 + return (rate);
1.277 }
1.278 }