task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java
changeset 6 97662396c0fd
child 16 2864c6d744c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.io.Serializable;
     1.7 +import java.math.BigDecimal;
     1.8 +import java.math.RoundingMode;
     1.9 +import java.util.Currency;
    1.10 +
    1.11 +/**
    1.12 + * Default {@link Convertor} implementation. Exchange rate is stored as equivalents. It means if we have USD to CZK convertor and USD1 = CZK17 
    1.13 + * we store 1 in sourceEquivalent and 17 in destinationEquivalent. This class is immutable.
    1.14 + * @author lukas
    1.15 + *
    1.16 + */
    1.17 +class DefaultConvertor implements Convertor, Serializable {
    1.18 +
    1.19 +	private static final long serialVersionUID = -1754789142402148099L;
    1.20 +
    1.21 +	/**
    1.22 +	 * Equivalent in source currency.
    1.23 +	 */
    1.24 +	private final BigDecimal sourceEquivalent;
    1.25 +	
    1.26 +	/**
    1.27 +	 * Equivalent in destination currency.
    1.28 +	 */
    1.29 +	private final BigDecimal destinationEquivalent;
    1.30 +	
    1.31 +	private final Currency sourceCurrency;
    1.32 +	
    1.33 +	private final Currency destinationCurrency;
    1.34 +	
    1.35 +	public DefaultConvertor(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
    1.36 +		super();
    1.37 +		this.sourceEquivalent = sourceEquivalent;
    1.38 +		this.destinationEquivalent = destinationEquivalent;
    1.39 +		this.sourceCurrency = sourceCurrency;
    1.40 +		this.destinationCurrency = destinationCurrency;
    1.41 +	}
    1.42 +	
    1.43 +	public Money convert(Money money) {
    1.44 +		if (money==null)
    1.45 +		{
    1.46 +			throw new NullPointerException("Money is null");
    1.47 +		}
    1.48 +		if (!money.getCurrency().equals(getSourceCurrency()))
    1.49 +		{
    1.50 +			throw new IllegalArgumentException("Can not convert from "+money.getCurrency()+". Converts "+getSourceCurrency()+" to "+getDestinationCurrency());
    1.51 +		}
    1.52 +		BigDecimal sourceAmount = money.getAmount();
    1.53 +		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_DOWN);
    1.54 +		return new MoneyImpl(destinationAmount, getDestinationCurrency());
    1.55 +	}
    1.56 +
    1.57 +
    1.58 +	public Convertor revert() {
    1.59 +		return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);
    1.60 +	}
    1.61 +
    1.62 +	public BigDecimal getSourceEquivalent() {
    1.63 +		return sourceEquivalent;
    1.64 +	}
    1.65 +
    1.66 +	public BigDecimal getDestinationEquivalent() {
    1.67 +		return destinationEquivalent;
    1.68 +	}
    1.69 +
    1.70 +	public Currency getSourceCurrency() {
    1.71 +		return sourceCurrency;
    1.72 +	}
    1.73 +
    1.74 +	public Currency getDestinationCurrency() {
    1.75 +		return destinationCurrency;
    1.76 +	}
    1.77 +	
    1.78 +	@Override
    1.79 +	public String toString() {
    1.80 +		return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
    1.81 +			+getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
    1.82 +	}
    1.83 +
    1.84 +	@Override
    1.85 +	public int hashCode() {
    1.86 +		final int prime = 31;
    1.87 +		int result = 1;
    1.88 +		result = prime
    1.89 +				* result
    1.90 +				+ ((destinationCurrency == null) ? 0 : destinationCurrency
    1.91 +						.hashCode());
    1.92 +		result = prime
    1.93 +				* result
    1.94 +				+ ((destinationEquivalent == null) ? 0 : destinationEquivalent
    1.95 +						.hashCode());
    1.96 +		result = prime * result
    1.97 +				+ ((sourceCurrency == null) ? 0 : sourceCurrency.hashCode());
    1.98 +		result = prime
    1.99 +				* result
   1.100 +				+ ((sourceEquivalent == null) ? 0 : sourceEquivalent.hashCode());
   1.101 +		return result;
   1.102 +	}
   1.103 +
   1.104 +	@Override
   1.105 +	public boolean equals(Object obj) {
   1.106 +		if (this == obj)
   1.107 +			return true;
   1.108 +		if (obj == null)
   1.109 +			return false;
   1.110 +		if (!(obj instanceof DefaultConvertor))
   1.111 +			return false;
   1.112 +		DefaultConvertor other = (DefaultConvertor) obj;
   1.113 +		if (destinationCurrency == null) {
   1.114 +			if (other.destinationCurrency != null)
   1.115 +				return false;
   1.116 +		} else if (!destinationCurrency.equals(other.destinationCurrency))
   1.117 +			return false;
   1.118 +		if (destinationEquivalent == null) {
   1.119 +			if (other.destinationEquivalent != null)
   1.120 +				return false;
   1.121 +		} else if (!destinationEquivalent.equals(other.destinationEquivalent))
   1.122 +			return false;
   1.123 +		if (sourceCurrency == null) {
   1.124 +			if (other.sourceCurrency != null)
   1.125 +				return false;
   1.126 +		} else if (!sourceCurrency.equals(other.sourceCurrency))
   1.127 +			return false;
   1.128 +		if (sourceEquivalent == null) {
   1.129 +			if (other.sourceEquivalent != null)
   1.130 +				return false;
   1.131 +		} else if (!sourceEquivalent.equals(other.sourceEquivalent))
   1.132 +			return false;
   1.133 +		return true;
   1.134 +	}
   1.135 +
   1.136 +
   1.137 +}