task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java
changeset 16 2864c6d744c0
parent 6 97662396c0fd
     1.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java	Tue Sep 30 11:47:02 2008 +0200
     1.3 @@ -31,26 +31,56 @@
     1.4  	
     1.5  	public DefaultConvertor(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
     1.6  		super();
     1.7 +		if (BigDecimal.ZERO.compareTo(sourceEquivalent)==0)
     1.8 +		{
     1.9 +			throw new IllegalArgumentException("Source equivalent amount can not be 0.");
    1.10 +		}
    1.11 +		if (BigDecimal.ZERO.compareTo(destinationEquivalent)==0)
    1.12 +		{
    1.13 +			throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
    1.14 +		}
    1.15  		this.sourceEquivalent = sourceEquivalent;
    1.16  		this.destinationEquivalent = destinationEquivalent;
    1.17  		this.sourceCurrency = sourceCurrency;
    1.18  		this.destinationCurrency = destinationCurrency;
    1.19  	}
    1.20  	
    1.21 -	public Money convert(Money money) {
    1.22 -		if (money==null)
    1.23 +	public Money convert(Money amount, Currency destinationCurrency) {
    1.24 +		if (amount==null)
    1.25  		{
    1.26  			throw new NullPointerException("Money is null");
    1.27  		}
    1.28 -		if (!money.getCurrency().equals(getSourceCurrency()))
    1.29 +		if (destinationCurrency==null)
    1.30  		{
    1.31 -			throw new IllegalArgumentException("Can not convert from "+money.getCurrency()+". Converts "+getSourceCurrency()+" to "+getDestinationCurrency());
    1.32 +			throw new NullPointerException("destionationCurrency is null");
    1.33  		}
    1.34 -		BigDecimal sourceAmount = money.getAmount();
    1.35 -		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_DOWN);
    1.36 +		if (isConversionInOpositeDirection(amount, destinationCurrency))
    1.37 +		{
    1.38 +			return revert().convert(amount, destinationCurrency);
    1.39 +		}
    1.40 +		if (!amount.getCurrency().equals(getSourceCurrency()))
    1.41 +		{
    1.42 +			throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    1.43 +		}
    1.44 +		if (!getDestinationCurrency().equals(destinationCurrency))
    1.45 +		{
    1.46 +			throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    1.47 +		}
    1.48 +		BigDecimal sourceAmount = amount.getAmount();
    1.49 +		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
    1.50  		return new MoneyImpl(destinationAmount, getDestinationCurrency());
    1.51  	}
    1.52  
    1.53 +	/**
    1.54 +	 * Returns true, if the conversion is in oposit direction.
    1.55 +	 * @param amount
    1.56 +	 * @param destinationCurrency
    1.57 +	 * @return
    1.58 +	 */
    1.59 +	private boolean isConversionInOpositeDirection(Money amount,	Currency destinationCurrency) {
    1.60 +		return amount.getCurrency().equals(getDestinationCurrency()) && destinationCurrency.equals(getSourceCurrency());
    1.61 +	}
    1.62 +
    1.63  
    1.64  	public Convertor revert() {
    1.65  		return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);