task1/solution06/src/org/apidesign/apifest08/currency/Bid.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.math.BigDecimal;
     4 import java.math.RoundingMode;
     5 import java.util.Currency;
     6 import static org.apidesign.apifest08.currency.Assert.*;
     7 
     8 
     9 /**
    10  * A bid representation. A did is defined as two currenncies and its value. 
    11  */
    12 public final class Bid {
    13 	private final Currency first;
    14 	private final Currency second;
    15 	private final BigDecimal bidValue; // a bid between the first currency and the second currency
    16 	public static final BigDecimal one = new BigDecimal(1);		
    17 	
    18 	Bid(Currency first, Currency second, BigDecimal bid) {
    19 		notNull(first, "first");
    20 		notNull(second, "second");
    21 		this.first = first;
    22 		this.bidValue = bid;
    23 		this.second = second;	
    24 	}
    25 	
    26 	Bid(Currency first, Currency second) {
    27 		notNull(first, "first");
    28 		notNull(second, "second");
    29 		this.first = first;
    30 		this.second = second;
    31 		this.bidValue = null;
    32 	}
    33 	
    34 			
    35 	BigDecimal getBidValue(Currency from, Currency to) {
    36 		if((from != first || from != second) || (to != first || to != second)) {
    37 			new IllegalArgumentException("This bid can be used only for: " + first + " " + second);
    38 		}
    39 		
    40 		notNull(bidValue, "bidValue");
    41 		
    42 		BigDecimal retVal;
    43 		
    44 		if(first == from) {
    45 			retVal = bidValue;
    46 		} else {	
    47 			//reverse bid		
    48 			retVal = one.divide(bidValue, 10 ,RoundingMode.HALF_UP);
    49 		}
    50 		
    51 		return retVal;
    52 	}
    53 
    54 	@Override
    55 	public int hashCode() {
    56 		final int prime = 31;
    57 		int result = 1;
    58 		result = prime * result + first.hashCode() +  second.hashCode();
    59 		return result;
    60 	}
    61 	
    62 	@Override
    63 	public boolean equals(Object obj) {
    64 		if (this == obj)
    65 			return true;
    66 		if (obj == null)
    67 			return false;
    68 		if (getClass() != obj.getClass())
    69 			return false;
    70 		Bid other = (Bid) obj;
    71 		
    72 		return (this.first == other.first || this.first == other.second) && (this.second == other.second || this.second == other.first) ;
    73 	}		
    74 }