1.1 --- a/task1/solution06/src/org/apidesign/apifest08/currency/Bid.java Tue Sep 30 12:24:45 2008 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,74 +0,0 @@
1.4 -package org.apidesign.apifest08.currency;
1.5 -
1.6 -import java.math.BigDecimal;
1.7 -import java.math.RoundingMode;
1.8 -import java.util.Currency;
1.9 -import static org.apidesign.apifest08.currency.Assert.*;
1.10 -
1.11 -
1.12 -/**
1.13 - * A bid representation. A did is defined as two currenncies and its value.
1.14 - */
1.15 -public final class Bid {
1.16 - private final Currency first;
1.17 - private final Currency second;
1.18 - private final BigDecimal bidValue; // a bid between the first currency and the second currency
1.19 - public static final BigDecimal one = new BigDecimal(1);
1.20 -
1.21 - Bid(Currency first, Currency second, BigDecimal bid) {
1.22 - notNull(first, "first");
1.23 - notNull(second, "second");
1.24 - this.first = first;
1.25 - this.bidValue = bid;
1.26 - this.second = second;
1.27 - }
1.28 -
1.29 - Bid(Currency first, Currency second) {
1.30 - notNull(first, "first");
1.31 - notNull(second, "second");
1.32 - this.first = first;
1.33 - this.second = second;
1.34 - this.bidValue = null;
1.35 - }
1.36 -
1.37 -
1.38 - BigDecimal getBidValue(Currency from, Currency to) {
1.39 - if((from != first || from != second) || (to != first || to != second)) {
1.40 - new IllegalArgumentException("This bid can be used only for: " + first + " " + second);
1.41 - }
1.42 -
1.43 - notNull(bidValue, "bidValue");
1.44 -
1.45 - BigDecimal retVal;
1.46 -
1.47 - if(first == from) {
1.48 - retVal = bidValue;
1.49 - } else {
1.50 - //reverse bid
1.51 - retVal = one.divide(bidValue, 10 ,RoundingMode.HALF_UP);
1.52 - }
1.53 -
1.54 - return retVal;
1.55 - }
1.56 -
1.57 - @Override
1.58 - public int hashCode() {
1.59 - final int prime = 31;
1.60 - int result = 1;
1.61 - result = prime * result + first.hashCode() + second.hashCode();
1.62 - return result;
1.63 - }
1.64 -
1.65 - @Override
1.66 - public boolean equals(Object obj) {
1.67 - if (this == obj)
1.68 - return true;
1.69 - if (obj == null)
1.70 - return false;
1.71 - if (getClass() != obj.getClass())
1.72 - return false;
1.73 - Bid other = (Bid) obj;
1.74 -
1.75 - return (this.first == other.first || this.first == other.second) && (this.second == other.second || this.second == other.first) ;
1.76 - }
1.77 -}
1.78 \ No newline at end of file
2.1 --- a/task1/solution06/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Sep 30 12:24:45 2008 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,65 +0,0 @@
2.4 -package org.apidesign.apifest08.currency;
2.5 -
2.6 -import static org.apidesign.apifest08.currency.Assert.notNull;
2.7 -
2.8 -import java.math.BigDecimal;
2.9 -import java.math.RoundingMode;
2.10 -import java.util.ArrayList;
2.11 -import java.util.Currency;
2.12 -import java.util.List;
2.13 -
2.14 -/**
2.15 - * The factory for {@link Convertor}.
2.16 - *
2.17 - * @see #newInstance()
2.18 - */
2.19 -public final class ConvertorFactory {
2.20 - //do not expose constructor
2.21 - private ConvertorFactory(){}
2.22 -
2.23 - /**
2.24 - * @return a convertor instance.
2.25 - *
2.26 - * @throws CurrencyException in a convertor cannot be instantiated
2.27 - */
2.28 - public static Convertor newInstance() throws CurrencyException{
2.29 - return new DefaultConvertor();
2.30 - }
2.31 -
2.32 - private static final class DefaultConvertor extends Convertor {
2.33 - private List<Bid> bids;
2.34 -
2.35 - private DefaultConvertor() {
2.36 - bids = new ArrayList<Bid>();
2.37 - Bid usdCzk = new Bid(Currencies.USD, Currencies.CZK, new BigDecimal("17"));
2.38 - bids.add(usdCzk);
2.39 - Bid skCzk = new Bid(Currencies.SKK, Currencies.CZK, new BigDecimal("0.8"));
2.40 - bids.add(skCzk);
2.41 - }
2.42 -
2.43 - @Override
2.44 - public Amount convert(Amount from, Currency targetCurrency)
2.45 - throws CurrencyException {
2.46 - return convert(from.getValue(), from.getCurrency(), targetCurrency);
2.47 -
2.48 - }
2.49 -
2.50 - @Override
2.51 - public Amount convert(BigDecimal amount, Currency from,
2.52 - Currency targetCurrency) throws ConversionException,
2.53 - UnsupportedConversionException {
2.54 - notNull(from, "from");
2.55 - notNull(targetCurrency, "targetCurrency");
2.56 -
2.57 - int index = bids.indexOf(new Bid(from, targetCurrency));
2.58 - if(index == -1) {
2.59 - throw new UnsupportedConversionException(from, targetCurrency);
2.60 - }
2.61 - Bid bid = bids.get(index);
2.62 - BigDecimal bidValue = bid.getBidValue(from, targetCurrency);
2.63 - BigDecimal result = bidValue.multiply(amount);
2.64 - return new Amount(result, targetCurrency);
2.65 - }
2.66 -
2.67 - }
2.68 -}
3.1 --- a/task1/solution06/src/org/apidesign/apifest08/currency/Currencies.java Tue Sep 30 12:24:45 2008 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,9 +0,0 @@
3.4 -package org.apidesign.apifest08.currency;
3.5 -
3.6 -import java.util.Currency;
3.7 -
3.8 -public class Currencies {
3.9 - public static final Currency CZK = Currency.getInstance("CZK");
3.10 - public static final Currency SKK = Currency.getInstance("SKK");
3.11 - public static final Currency USD = Currency.getInstance("USD");
3.12 -}