task4/solution07/src/org/apidesign/apifest08/currency/ConversionRate.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution07/src/org/apidesign/apifest08/currency/ConversionRate.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,91 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import java.math.RoundingMode;
     1.8 +
     1.9 +/**
    1.10 + * A rate of conversion from one currency to another.
    1.11 + * @author jdvorak
    1.12 + */
    1.13 +public class ConversionRate {
    1.14 +
    1.15 +    private final MonetaryAmount srcUnitAmount;
    1.16 +    private final MonetaryAmount tgtUnitAmount;
    1.17 +    private final int tgtScale;
    1.18 +    private final RoundingMode roundingMode;
    1.19 +
    1.20 +    /**
    1.21 +     * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount.
    1.22 +     * @param srcUnitAmount the amount of source currency
    1.23 +     * @param tgtUnitAmount the corresponding amount of target currency
    1.24 +     * @param tgtScale the scale of the target amounts
    1.25 +     * @param roundingMode the rounding mode to use when producing the target amounts
    1.26 +     */
    1.27 +    public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount, final int targetScale, final RoundingMode roundingMode ) {
    1.28 +        this.srcUnitAmount = srcUnitAmount;
    1.29 +        this.tgtUnitAmount = tgtUnitAmount;
    1.30 +        this.tgtScale = targetScale;
    1.31 +        this.roundingMode = roundingMode;
    1.32 +    }
    1.33 +
    1.34 +    /**
    1.35 +     * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount, default number of fraction digits and the given rounding mode.
    1.36 +     * @param srcUnitAmount the amount of source currency
    1.37 +     * @param tgtUnitAmount the corresponding amount of target currency
    1.38 +     * @param roundingMode the rounding mode to use
    1.39 +     */
    1.40 +    public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount, final RoundingMode roundingMode ) {
    1.41 +        this( srcUnitAmount, tgtUnitAmount, tgtUnitAmount.getCurrency().getDefaultFractionDigits(), roundingMode );
    1.42 +    }
    1.43 +
    1.44 +    /**
    1.45 +     * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount, default number of fraction digits and {@link RoundingMode#HALF_EVEN}.
    1.46 +     * @param srcUnitAmount the amount of source currency
    1.47 +     * @param tgtUnitAmount the corresponding amount of target currency
    1.48 +     */
    1.49 +    public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount ) {
    1.50 +        this( srcUnitAmount, tgtUnitAmount, RoundingMode.HALF_EVEN );
    1.51 +    }
    1.52 +
    1.53 +    public RoundingMode getRoundingMode() {
    1.54 +        return roundingMode;
    1.55 +    }
    1.56 +
    1.57 +    public MonetaryAmount getSrcUnitAmount() {
    1.58 +        return srcUnitAmount;
    1.59 +    }
    1.60 +
    1.61 +    public int getTgtScale() {
    1.62 +        return tgtScale;
    1.63 +    }
    1.64 +
    1.65 +    public MonetaryAmount getTgtUnitAmount() {
    1.66 +        return tgtUnitAmount;
    1.67 +    }
    1.68 +    
    1.69 +    /**
    1.70 +     * Multiplies the given amount with the given rate.
    1.71 +     * @param srcAmount
    1.72 +     * @return
    1.73 +     */
    1.74 +    public BigDecimal convert( final BigDecimal srcAmount ) {
    1.75 +        return srcAmount
    1.76 +            .multiply( tgtUnitAmount.getAmount() )
    1.77 +            .divide( srcUnitAmount.getAmount(), tgtScale, roundingMode );
    1.78 +    }
    1.79 +    
    1.80 +    /**
    1.81 +     * Creates a monetary amount that corresponds to the given source amount multiplied by the rate.
    1.82 +     * @param srcAmount the source amount
    1.83 +     * @return the monetary amount in the target currency
    1.84 +     * @throws IllegalArgumentException if the currency of srcAmount is not equal to the source currency of this rate
    1.85 +     */
    1.86 +    public MonetaryAmount convert( final MonetaryAmount srcAmount ) {
    1.87 +        if ( srcUnitAmount.getCurrency().equals( srcAmount.getCurrency() ) ) {
    1.88 +            return new MonetaryAmount( convert( srcAmount.getAmount() ), tgtUnitAmount.getCurrency() );
    1.89 +        } else {
    1.90 +            throw new IllegalArgumentException( "This rate converts from " + srcUnitAmount.getCurrency() + ", but a conversion from " + srcAmount.getCurrency() + " is attempted" );
    1.91 +        }
    1.92 +    }
    1.93 +    
    1.94 +}