1 package org.apidesign.apifest08.currency;
4 import java.math.BigDecimal;
5 import java.util.Currency;
10 * Convert between two currencies.
12 * @author D'Arcy Smith
16 implements ExchangeRateConvertor
20 private final ExchangeRate rate;
23 * Constructs a convertor with the specified currencies.
25 * @param a the currency to convert from.
26 * @param aRate the exchage rage between from and to.
27 * @param b the currency to convert to.
28 * @param bRate the exchage rage between to and from.
29 * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
31 public ConvertorImpl(final ExchangeRate r)
35 throw new IllegalArgumentException("r cannot be null");
42 * Convert an amount from one currency to another.
44 * @param from the currency to convert from.
45 * @param to the currency to convert to.
46 * @param amount the amount to convert.
47 * @return the converted amount.
48 * @throws IllegalArgumentException if any of the arguments are null.
49 * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
51 public BigDecimal convert(final Currency from,
53 final BigDecimal amount)
54 throws InvalidConversionException
56 return (rate.convert(from, to, amount));
60 * Check to see if converting between the two currencies is possible.
62 * @param from the currency to convert from.
63 * @param to the currency to convert to.
64 * @return true if the conversion is possible.
65 * @throws IllegalArgumentException if either from or to are null.
67 public boolean canConvert(final Currency from, final Currency to)
69 return (rate.canConvert(from, to));
73 * Get the currencies that the convertor supports.
75 * @return the supported currencies.
77 public Set<Currency> getCurrencies()
79 return (rate.getCurrencies());
83 * Get the conversion rate between two currencies.
85 * @param from the currency to convert from.
86 * @param to the currency to convert to.
87 * @return the conversion rate between the two currencies.
88 * @throws InvalidConversionException if canConvert would return false.
89 * @throws IllegalArgumentException if either from or to are null.
91 public BigDecimal getConversionRate(final Currency from,
93 throws InvalidConversionException
95 return (rate.getConversionRate(from, to));
99 * Check to see if two ConvertorImpls are equal.
101 * @param obj the object to check
102 * @return if the ConvertorImpls are not the same (cuyrrencies and rates).
105 public boolean equals(Object obj)
112 if (getClass() != obj.getClass())
117 final ConvertorImpl other = (ConvertorImpl) obj;
119 return (rate.equals(other.rate));
123 * Get the hashCode of the Convertor.
125 * @return the hashCode of the convertor.
128 public int hashCode()
130 return (rate.hashCode());
134 * Get the currencyCode of both currencies.
136 * @return the currency codes of both currencies.
139 public String toString()
141 return (rate.getCurrencyA().getCurrencyCode() + " to " + rate.getCurrencyB().getCurrencyCode());
144 public ExchangeRate getExchangeRate()