task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java
changeset 35 8898c620fe96
parent 29 f6073056b9fe
     1.1 --- a/task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java	Wed Oct 01 10:43:05 2008 +0200
     1.2 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java	Tue Oct 07 00:21:03 2008 +0200
     1.3 @@ -4,7 +4,10 @@
     1.4  import java.math.BigDecimal;
     1.5  import java.math.MathContext;
     1.6  import java.math.RoundingMode;
     1.7 +import java.util.Collections;
     1.8  import java.util.Currency;
     1.9 +import java.util.HashSet;
    1.10 +import java.util.Set;
    1.11  
    1.12  
    1.13  /**
    1.14 @@ -128,12 +131,59 @@
    1.15              throw new InvalidConversionException("cannot convert to: " + to.getCurrencyCode(), to, currencyA, currencyB);
    1.16          }
    1.17  
    1.18 -        // converting between the same currency is no converstion at all.
    1.19 +        result = amount.multiply(getConversionRate(from, to));
    1.20 +
    1.21 +        return (result.setScale(2, RoundingMode.HALF_DOWN));
    1.22 +    }
    1.23 +
    1.24 +    /**
    1.25 +     * Check to see if converting between the two currencies is possible.
    1.26 +     * 
    1.27 +     * @param from the currency to convert from.
    1.28 +     * @param to the currency to convert to.
    1.29 +     * @return true if the conversion is possible.
    1.30 +     */
    1.31 +    public boolean canConvert(final Currency from, final Currency to)
    1.32 +    {
    1.33 +        return ((from.equals(currencyA) || from.equals(currencyB)) &&
    1.34 +                (to.equals(currencyA)   || to.equals(currencyB)));
    1.35 +    }
    1.36 +
    1.37 +    /**
    1.38 +     * Get the currencies that the convertor supports.
    1.39 +     * 
    1.40 +     * @return the supported currencies.
    1.41 +     */
    1.42 +    public Set<Currency> getCurrencies()
    1.43 +    {
    1.44 +        final Set<Currency> currencies;
    1.45 +        
    1.46 +        currencies = new HashSet<Currency>();
    1.47 +        currencies.add(currencyA);
    1.48 +        currencies.add(currencyB);
    1.49 +
    1.50 +        return (Collections.unmodifiableSet(currencies));
    1.51 +    }
    1.52 +
    1.53 +    /**
    1.54 +     * Get the conversion rate between two currencies.
    1.55 +     * 
    1.56 +     * @param from the currency to convert from.
    1.57 +     * @param to the currency to convert to.
    1.58 +     * @return the conversion rate between the two currencies.
    1.59 +     * @throws InvalidConversionException if canConvert would return false.
    1.60 +     */
    1.61 +    public BigDecimal getConversionRate(final Currency from, 
    1.62 +                                        final Currency to)
    1.63 +        throws InvalidConversionException
    1.64 +    {
    1.65 +        final BigDecimal rate;
    1.66 +
    1.67          if(from.equals(to))
    1.68          {
    1.69 -            result = amount;
    1.70 +            rate = BigDecimal.ONE;
    1.71          }
    1.72 -        else
    1.73 +        else 
    1.74          {
    1.75              final BigDecimal rateX;
    1.76              final BigDecimal rateY;
    1.77 @@ -150,10 +200,83 @@
    1.78                  rateY = currencyARate;
    1.79              }
    1.80  
    1.81 -            temp   = amount.divide(rateX, MathContext.DECIMAL32);
    1.82 -            result = temp.multiply(rateY);
    1.83 +            temp = BigDecimal.ONE.divide(rateX, MathContext.DECIMAL64);
    1.84 +            rate = temp.multiply(rateY);
    1.85 +        }
    1.86 +        
    1.87 +        return (rate.setScale(20, RoundingMode.HALF_EVEN));
    1.88 +    }
    1.89 +
    1.90 +    /**
    1.91 +     * Check to see if two ConvertorImpls are equal.
    1.92 +     *
    1.93 +     * @param obj the object to check
    1.94 +     * @return if the ConvertorImpls are not the same (cuyrrencies and rates).
    1.95 +     */
    1.96 +    @Override
    1.97 +    public boolean equals(Object obj)
    1.98 +    {
    1.99 +        if (obj == null)
   1.100 +        {
   1.101 +            return false;
   1.102 +        }
   1.103 +        
   1.104 +        if (getClass() != obj.getClass())
   1.105 +        {
   1.106 +            return false;
   1.107          }
   1.108  
   1.109 -        return (result.setScale(2, RoundingMode.HALF_DOWN));
   1.110 +        final ConvertorImpl other = (ConvertorImpl) obj;
   1.111 +
   1.112 +        // it would be nice if NetBeans could chck to see if the variable is final and guaranteed not to be null... but that
   1.113 +        // would likely be tricky... but in a NetBeans engineer reads that see if you can do it :-)
   1.114 +        if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA)))
   1.115 +        {
   1.116 +            return false;
   1.117 +        }
   1.118 +        
   1.119 +        if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB)))
   1.120 +        {
   1.121 +            return false;
   1.122 +        }
   1.123 +        
   1.124 +        if (this.currencyARate != other.currencyARate && (this.currencyARate == null || !this.currencyARate.equals(other.currencyARate)))
   1.125 +        {
   1.126 +            return false;
   1.127 +        }
   1.128 +        
   1.129 +        if (this.currencyBRate != other.currencyBRate && (this.currencyBRate == null || !this.currencyBRate.equals(other.currencyBRate)))
   1.130 +        {
   1.131 +            return false;
   1.132 +        }
   1.133 +
   1.134 +        return true;
   1.135 +    }
   1.136 +
   1.137 +    /**
   1.138 +     * Get the hashCode of the Convertor.
   1.139 +     *
   1.140 +     * @return the hashCode of the convertor.
   1.141 +     */
   1.142 +    @Override
   1.143 +    public int hashCode()
   1.144 +    {
   1.145 +        int hash = 7;
   1.146 +        hash = 37 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
   1.147 +        hash = 37 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
   1.148 +        hash = 37 * hash + (this.currencyARate != null ? this.currencyARate.hashCode() : 0);
   1.149 +        hash = 37 * hash + (this.currencyBRate != null ? this.currencyBRate.hashCode() : 0);
   1.150 +        return hash;
   1.151 +    }
   1.152 +
   1.153 +    /**
   1.154 +     * Get the currencyCode of both currencies.
   1.155 +     *
   1.156 +     * @return the currency codes of both currencies.
   1.157 +     */
   1.158 +    @Override
   1.159 +    public String toString()
   1.160 +    {
   1.161 +        return (currencyA.getCurrencyCode() + " to " + currencyB.getCurrencyCode());
   1.162      }
   1.163  }