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