1 package org.apidesign.apifest08.currency;
3 import java.math.BigDecimal;
4 import java.math.RoundingMode;
7 * A rate of conversion from one currency to another.
10 public class ConversionRate {
12 private final MonetaryAmount srcUnitAmount;
13 private final MonetaryAmount tgtUnitAmount;
14 private final int tgtScale;
15 private final RoundingMode roundingMode;
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
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;
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
37 public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount, final RoundingMode roundingMode ) {
38 this( srcUnitAmount, tgtUnitAmount, tgtUnitAmount.getCurrency().getDefaultFractionDigits(), roundingMode );
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
46 public ConversionRate( final MonetaryAmount srcUnitAmount, final MonetaryAmount tgtUnitAmount ) {
47 this( srcUnitAmount, tgtUnitAmount, RoundingMode.HALF_EVEN );
50 public RoundingMode getRoundingMode() {
54 public MonetaryAmount getSrcUnitAmount() {
58 public int getTgtScale() {
62 public MonetaryAmount getTgtUnitAmount() {
67 * Multiplies the given amount with the given rate.
71 public BigDecimal convert( final BigDecimal srcAmount ) {
73 .multiply( tgtUnitAmount.getAmount() )
74 .divide( srcUnitAmount.getAmount(), tgtScale, roundingMode );
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
83 public MonetaryAmount convert( final MonetaryAmount srcAmount ) {
84 if ( srcUnitAmount.getCurrency().equals( srcAmount.getCurrency() ) ) {
85 return new MonetaryAmount( convert( srcAmount.getAmount() ), tgtUnitAmount.getCurrency() );
87 throw new IllegalArgumentException( "This rate converts from " + srcUnitAmount.getCurrency() + ", but a conversion from " + srcAmount.getCurrency() + " is attempted" );