task4/solution07/src/org/apidesign/apifest08/currency/ConversionRate.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution07/src/org/apidesign/apifest08/currency/ConversionRate.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.math.BigDecimal;
     4 import java.math.RoundingMode;
     5 
     6 /**
     7  * A rate of conversion from one currency to another.
     8  * @author jdvorak
     9  */
    10 public class ConversionRate {
    11 
    12     private final MonetaryAmount srcUnitAmount;
    13     private final MonetaryAmount tgtUnitAmount;
    14     private final int tgtScale;
    15     private final RoundingMode roundingMode;
    16 
    17     /**
    18      * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount.
    19      * @param srcUnitAmount the amount of source currency
    20      * @param tgtUnitAmount the corresponding amount of target currency
    21      * @param tgtScale the scale of the target amounts
    22      * @param roundingMode the rounding mode to use when producing the target amounts
    23      */
    24     public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount, final int targetScale, final RoundingMode roundingMode ) {
    25         this.srcUnitAmount = srcUnitAmount;
    26         this.tgtUnitAmount = tgtUnitAmount;
    27         this.tgtScale = targetScale;
    28         this.roundingMode = roundingMode;
    29     }
    30 
    31     /**
    32      * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount, default number of fraction digits and the given rounding mode.
    33      * @param srcUnitAmount the amount of source currency
    34      * @param tgtUnitAmount the corresponding amount of target currency
    35      * @param roundingMode the rounding mode to use
    36      */
    37     public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount, final RoundingMode roundingMode ) {
    38         this( srcUnitAmount, tgtUnitAmount, tgtUnitAmount.getCurrency().getDefaultFractionDigits(), roundingMode );
    39     }
    40 
    41     /**
    42      * A new conversion rate that gives tgtUnitAmount per every srcUnitAmount, default number of fraction digits and {@link RoundingMode#HALF_EVEN}.
    43      * @param srcUnitAmount the amount of source currency
    44      * @param tgtUnitAmount the corresponding amount of target currency
    45      */
    46     public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount ) {
    47         this( srcUnitAmount, tgtUnitAmount, RoundingMode.HALF_EVEN );
    48     }
    49 
    50     public RoundingMode getRoundingMode() {
    51         return roundingMode;
    52     }
    53 
    54     public MonetaryAmount getSrcUnitAmount() {
    55         return srcUnitAmount;
    56     }
    57 
    58     public int getTgtScale() {
    59         return tgtScale;
    60     }
    61 
    62     public MonetaryAmount getTgtUnitAmount() {
    63         return tgtUnitAmount;
    64     }
    65     
    66     /**
    67      * Multiplies the given amount with the given rate.
    68      * @param srcAmount
    69      * @return
    70      */
    71     public BigDecimal convert( final BigDecimal srcAmount ) {
    72         return srcAmount
    73             .multiply( tgtUnitAmount.getAmount() )
    74             .divide( srcUnitAmount.getAmount(), tgtScale, roundingMode );
    75     }
    76     
    77     /**
    78      * Creates a monetary amount that corresponds to the given source amount multiplied by the rate.
    79      * @param srcAmount the source amount
    80      * @return the monetary amount in the target currency
    81      * @throws IllegalArgumentException if the currency of srcAmount is not equal to the source currency of this rate
    82      */
    83     public MonetaryAmount convert( final MonetaryAmount srcAmount ) {
    84         if ( srcUnitAmount.getCurrency().equals( srcAmount.getCurrency() ) ) {
    85             return new MonetaryAmount( convert( srcAmount.getAmount() ), tgtUnitAmount.getCurrency() );
    86         } else {
    87             throw new IllegalArgumentException( "This rate converts from " + srcUnitAmount.getCurrency() + ", but a conversion from " + srcAmount.getCurrency() + " is attempted" );
    88         }
    89     }
    90     
    91 }