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