author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 25 Oct 2008 20:53:00 +0200 | |
changeset 84 | 2ae6e4aa7aef |
parent 61 | 58ec6da75f6f |
permissions | -rw-r--r-- |
japod@17 | 1 |
package org.apidesign.apifest08.currency; |
japod@17 | 2 |
|
japod@17 | 3 |
|
japod@17 | 4 |
import java.math.BigDecimal; |
japod@17 | 5 |
import java.util.Currency; |
japod@35 | 6 |
import java.util.Set; |
japod@17 | 7 |
|
japod@17 | 8 |
|
japod@17 | 9 |
/** |
japod@17 | 10 |
* Convert between two currencies. |
japod@17 | 11 |
* |
japod@17 | 12 |
* @author D'Arcy Smith |
jaroslav@69 | 13 |
* @version 1.1 |
japod@17 | 14 |
*/ |
jaroslav@69 | 15 |
class ConvertorImpl |
jaroslav@69 | 16 |
implements ExchangeRateConvertor |
japod@17 | 17 |
{ |
japod@17 | 18 |
/** |
japod@17 | 19 |
*/ |
jaroslav@69 | 20 |
private final ExchangeRate rate; |
japod@17 | 21 |
|
japod@17 | 22 |
/** |
japod@17 | 23 |
* Constructs a convertor with the specified currencies. |
japod@17 | 24 |
* |
japod@17 | 25 |
* @param a the currency to convert from. |
japod@17 | 26 |
* @param aRate the exchage rage between from and to. |
japod@17 | 27 |
* @param b the currency to convert to. |
japod@17 | 28 |
* @param bRate the exchage rage between to and from. |
japod@17 | 29 |
* @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0. |
japod@17 | 30 |
*/ |
jaroslav@69 | 31 |
public ConvertorImpl(final ExchangeRate r) |
japod@17 | 32 |
{ |
jaroslav@69 | 33 |
if(r == null) |
japod@17 | 34 |
{ |
jaroslav@69 | 35 |
throw new IllegalArgumentException("r cannot be null"); |
japod@17 | 36 |
} |
japod@17 | 37 |
|
jaroslav@69 | 38 |
rate = r; |
japod@17 | 39 |
} |
japod@17 | 40 |
|
japod@17 | 41 |
/** |
japod@17 | 42 |
* Convert an amount from one currency to another. |
japod@17 | 43 |
* |
japod@17 | 44 |
* @param from the currency to convert from. |
japod@17 | 45 |
* @param to the currency to convert to. |
japod@17 | 46 |
* @param amount the amount to convert. |
japod@17 | 47 |
* @return the converted amount. |
japod@17 | 48 |
* @throws IllegalArgumentException if any of the arguments are null. |
japod@17 | 49 |
* @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor. |
japod@17 | 50 |
*/ |
japod@17 | 51 |
public BigDecimal convert(final Currency from, |
japod@17 | 52 |
final Currency to, |
japod@17 | 53 |
final BigDecimal amount) |
japod@17 | 54 |
throws InvalidConversionException |
japod@17 | 55 |
{ |
jaroslav@69 | 56 |
return (rate.convert(from, to, amount)); |
japod@35 | 57 |
} |
japod@35 | 58 |
|
japod@35 | 59 |
/** |
japod@35 | 60 |
* Check to see if converting between the two currencies is possible. |
japod@35 | 61 |
* |
japod@35 | 62 |
* @param from the currency to convert from. |
japod@35 | 63 |
* @param to the currency to convert to. |
japod@35 | 64 |
* @return true if the conversion is possible. |
japod@55 | 65 |
* @throws IllegalArgumentException if either from or to are null. |
japod@35 | 66 |
*/ |
japod@35 | 67 |
public boolean canConvert(final Currency from, final Currency to) |
japod@35 | 68 |
{ |
jaroslav@69 | 69 |
return (rate.canConvert(from, to)); |
japod@35 | 70 |
} |
japod@35 | 71 |
|
japod@35 | 72 |
/** |
japod@35 | 73 |
* Get the currencies that the convertor supports. |
japod@35 | 74 |
* |
japod@35 | 75 |
* @return the supported currencies. |
japod@35 | 76 |
*/ |
japod@35 | 77 |
public Set<Currency> getCurrencies() |
japod@35 | 78 |
{ |
jaroslav@69 | 79 |
return (rate.getCurrencies()); |
japod@35 | 80 |
} |
japod@35 | 81 |
|
japod@35 | 82 |
/** |
japod@35 | 83 |
* Get the conversion rate between two currencies. |
japod@35 | 84 |
* |
japod@35 | 85 |
* @param from the currency to convert from. |
japod@35 | 86 |
* @param to the currency to convert to. |
japod@35 | 87 |
* @return the conversion rate between the two currencies. |
japod@35 | 88 |
* @throws InvalidConversionException if canConvert would return false. |
japod@55 | 89 |
* @throws IllegalArgumentException if either from or to are null. |
japod@35 | 90 |
*/ |
japod@35 | 91 |
public BigDecimal getConversionRate(final Currency from, |
japod@35 | 92 |
final Currency to) |
japod@35 | 93 |
throws InvalidConversionException |
japod@55 | 94 |
{ |
jaroslav@69 | 95 |
return (rate.getConversionRate(from, to)); |
japod@35 | 96 |
} |
japod@35 | 97 |
|
japod@35 | 98 |
/** |
japod@35 | 99 |
* Check to see if two ConvertorImpls are equal. |
japod@35 | 100 |
* |
japod@35 | 101 |
* @param obj the object to check |
japod@35 | 102 |
* @return if the ConvertorImpls are not the same (cuyrrencies and rates). |
japod@35 | 103 |
*/ |
japod@35 | 104 |
@Override |
japod@35 | 105 |
public boolean equals(Object obj) |
japod@35 | 106 |
{ |
japod@35 | 107 |
if (obj == null) |
japod@35 | 108 |
{ |
japod@35 | 109 |
return false; |
japod@35 | 110 |
} |
japod@35 | 111 |
|
japod@35 | 112 |
if (getClass() != obj.getClass()) |
japod@35 | 113 |
{ |
japod@35 | 114 |
return false; |
japod@17 | 115 |
} |
japod@17 | 116 |
|
japod@35 | 117 |
final ConvertorImpl other = (ConvertorImpl) obj; |
japod@35 | 118 |
|
jaroslav@69 | 119 |
return (rate.equals(other.rate)); |
japod@35 | 120 |
} |
japod@35 | 121 |
|
japod@35 | 122 |
/** |
japod@35 | 123 |
* Get the hashCode of the Convertor. |
japod@35 | 124 |
* |
japod@35 | 125 |
* @return the hashCode of the convertor. |
japod@35 | 126 |
*/ |
japod@35 | 127 |
@Override |
japod@35 | 128 |
public int hashCode() |
japod@35 | 129 |
{ |
jaroslav@69 | 130 |
return (rate.hashCode()); |
japod@35 | 131 |
} |
japod@35 | 132 |
|
japod@35 | 133 |
/** |
japod@35 | 134 |
* Get the currencyCode of both currencies. |
japod@35 | 135 |
* |
japod@35 | 136 |
* @return the currency codes of both currencies. |
japod@35 | 137 |
*/ |
japod@35 | 138 |
@Override |
japod@35 | 139 |
public String toString() |
japod@35 | 140 |
{ |
jaroslav@69 | 141 |
return (rate.getCurrencyA().getCurrencyCode() + " to " + rate.getCurrencyB().getCurrencyCode()); |
jaroslav@69 | 142 |
} |
jaroslav@69 | 143 |
|
jaroslav@69 | 144 |
public ExchangeRate getExchangeRate() |
jaroslav@69 | 145 |
{ |
jaroslav@69 | 146 |
return (rate); |
japod@17 | 147 |
} |
japod@17 | 148 |
} |