task1/solution06/src/org/apidesign/apifest08/currency/Bid.java
changeset 6 97662396c0fd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/Bid.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,74 @@
     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