task4/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.io.Serializable;
     4 import java.math.BigDecimal;
     5 import java.math.RoundingMode;
     6 import java.util.Currency;
     7 
     8 /**
     9  * Default {@link Convertor} implementation. Exchange rate is stored as equivalents. It means if we have USD to CZK convertor and USD1 = CZK17 
    10  * we store 1 in sourceEquivalent and 17 in destinationEquivalent. This class is immutable.
    11  * @author lukas
    12  *
    13  */
    14 class DefaultConvertor implements ExtendedConvertor, Serializable {
    15 
    16 	private static final long serialVersionUID = 6660014633685135034L;
    17 
    18 	/**
    19 	 * Equivalent in source currency.
    20 	 */
    21 	private final BigDecimal sourceEquivalent;
    22 	
    23 	/**
    24 	 * Equivalent in destination currency.
    25 	 */
    26 	private final BigDecimal destinationEquivalent;
    27 	
    28 	private final Currency sourceCurrency;
    29 	
    30 	private final Currency destinationCurrency;
    31 	
    32 	public DefaultConvertor(Money sourceEquivalent, Money destinationEquivalent) {
    33 		super();
    34 		if (BigDecimal.ZERO.compareTo(sourceEquivalent.getAmount())==0)
    35 		{
    36 			throw new IllegalArgumentException("Source equivalent amount can not be 0.");
    37 		}
    38 		if (BigDecimal.ZERO.compareTo(destinationEquivalent.getAmount())==0)
    39 		{
    40 			throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
    41 		}
    42 		this.sourceEquivalent = sourceEquivalent.getAmount();
    43 		this.destinationEquivalent = destinationEquivalent.getAmount();
    44 		this.sourceCurrency = sourceEquivalent.getCurrency();
    45 		this.destinationCurrency = destinationEquivalent.getCurrency();
    46 	}
    47 	
    48 	public Money convert(Money amount, Currency destinationCurrency) {
    49 		if (amount==null)
    50 		{
    51 			throw new NullPointerException("Money is null");
    52 		}
    53 		if (destinationCurrency==null)
    54 		{
    55 			throw new NullPointerException("destionationCurrency is null");
    56 		}
    57 		if (!amount.getCurrency().equals(getSourceCurrency()))
    58 		{
    59 			throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    60 		}
    61 		if (!getDestinationCurrency().equals(destinationCurrency))
    62 		{
    63 			throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    64 		}
    65 		BigDecimal sourceAmount = amount.getAmount();
    66 		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
    67 		return new MoneyImpl(destinationAmount, getDestinationCurrency());
    68 	}
    69 	
    70 	public boolean isConversionSupported(Currency from, Currency to) {
    71 		return sourceCurrency.equals(from) && destinationCurrency.equals(to);
    72 	}
    73 
    74 	public BigDecimal getSourceEquivalent() {
    75 		return sourceEquivalent;
    76 	}
    77 
    78 	public BigDecimal getDestinationEquivalent() {
    79 		return destinationEquivalent;
    80 	}
    81 
    82 	public Currency getSourceCurrency() {
    83 		return sourceCurrency;
    84 	}
    85 
    86 	public Currency getDestinationCurrency() {
    87 		return destinationCurrency;
    88 	}
    89 	
    90 	@Override
    91 	public String toString() {
    92 		return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
    93 			+getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
    94 	}
    95 }