task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 16 2864c6d744c0
permissions -rw-r--r--
Adding solutions received for task1
     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 Convertor, Serializable {
    15 
    16 	private static final long serialVersionUID = -1754789142402148099L;
    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(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
    33 		super();
    34 		this.sourceEquivalent = sourceEquivalent;
    35 		this.destinationEquivalent = destinationEquivalent;
    36 		this.sourceCurrency = sourceCurrency;
    37 		this.destinationCurrency = destinationCurrency;
    38 	}
    39 	
    40 	public Money convert(Money money) {
    41 		if (money==null)
    42 		{
    43 			throw new NullPointerException("Money is null");
    44 		}
    45 		if (!money.getCurrency().equals(getSourceCurrency()))
    46 		{
    47 			throw new IllegalArgumentException("Can not convert from "+money.getCurrency()+". Converts "+getSourceCurrency()+" to "+getDestinationCurrency());
    48 		}
    49 		BigDecimal sourceAmount = money.getAmount();
    50 		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_DOWN);
    51 		return new MoneyImpl(destinationAmount, getDestinationCurrency());
    52 	}
    53 
    54 
    55 	public Convertor revert() {
    56 		return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);
    57 	}
    58 
    59 	public BigDecimal getSourceEquivalent() {
    60 		return sourceEquivalent;
    61 	}
    62 
    63 	public BigDecimal getDestinationEquivalent() {
    64 		return destinationEquivalent;
    65 	}
    66 
    67 	public Currency getSourceCurrency() {
    68 		return sourceCurrency;
    69 	}
    70 
    71 	public Currency getDestinationCurrency() {
    72 		return destinationCurrency;
    73 	}
    74 	
    75 	@Override
    76 	public String toString() {
    77 		return getClass().getName()+" converts "+getSourceCurrency()+" to "+getDestinationCurrency()+" "
    78 			+getSourceCurrency()+getSourceEquivalent()+"="+getDestinationCurrency()+getDestinationEquivalent();
    79 	}
    80 
    81 	@Override
    82 	public int hashCode() {
    83 		final int prime = 31;
    84 		int result = 1;
    85 		result = prime
    86 				* result
    87 				+ ((destinationCurrency == null) ? 0 : destinationCurrency
    88 						.hashCode());
    89 		result = prime
    90 				* result
    91 				+ ((destinationEquivalent == null) ? 0 : destinationEquivalent
    92 						.hashCode());
    93 		result = prime * result
    94 				+ ((sourceCurrency == null) ? 0 : sourceCurrency.hashCode());
    95 		result = prime
    96 				* result
    97 				+ ((sourceEquivalent == null) ? 0 : sourceEquivalent.hashCode());
    98 		return result;
    99 	}
   100 
   101 	@Override
   102 	public boolean equals(Object obj) {
   103 		if (this == obj)
   104 			return true;
   105 		if (obj == null)
   106 			return false;
   107 		if (!(obj instanceof DefaultConvertor))
   108 			return false;
   109 		DefaultConvertor other = (DefaultConvertor) obj;
   110 		if (destinationCurrency == null) {
   111 			if (other.destinationCurrency != null)
   112 				return false;
   113 		} else if (!destinationCurrency.equals(other.destinationCurrency))
   114 			return false;
   115 		if (destinationEquivalent == null) {
   116 			if (other.destinationEquivalent != null)
   117 				return false;
   118 		} else if (!destinationEquivalent.equals(other.destinationEquivalent))
   119 			return false;
   120 		if (sourceCurrency == null) {
   121 			if (other.sourceCurrency != null)
   122 				return false;
   123 		} else if (!sourceCurrency.equals(other.sourceCurrency))
   124 			return false;
   125 		if (sourceEquivalent == null) {
   126 			if (other.sourceEquivalent != null)
   127 				return false;
   128 		} else if (!sourceEquivalent.equals(other.sourceEquivalent))
   129 			return false;
   130 		return true;
   131 	}
   132 
   133 
   134 }