author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Wed, 01 Oct 2008 10:43:05 +0200 | |
changeset 29 | f6073056b9fe |
parent 6 | task1/solution01/src/org/apidesign/apifest08/currency/ConversionProperties.java@97662396c0fd |
permissions | -rw-r--r-- |
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 |
* This class encapsulates settings for currency conversion |
japod@6 | 8 |
* We can use it eg. for uptime options etc. in future versions |
japod@6 | 9 |
* |
japod@6 | 10 |
* @author Ladislav Vitasek |
japod@6 | 11 |
*/ |
japod@6 | 12 |
final public class ConversionProperties { |
japod@6 | 13 |
|
japod@6 | 14 |
private final ConversionRatioProvider conversionRatioProvider; |
japod@6 | 15 |
private final RoundingMode roundingMode; |
japod@6 | 16 |
|
japod@6 | 17 |
/** |
japod@6 | 18 |
* Returns conversion constants - conversion operation is invertable |
japod@6 | 19 |
* Default |
japod@6 | 20 |
*/ |
japod@6 | 21 |
private final static class FixedBidirectionalRatioConversion implements ConversionRatioProvider { |
japod@6 | 22 |
private final BigDecimal currency1ToCurrency2Constant; |
japod@6 | 23 |
private final BigDecimal currency2ToCurrency1Constant; |
japod@6 | 24 |
private static final int SCALE_MAX_DEFAULT = 20; |
japod@6 | 25 |
|
japod@6 | 26 |
/** |
japod@6 | 27 |
* Constructor |
japod@6 | 28 |
* @param ratioConstant fixed ratio constant |
japod@6 | 29 |
* @param mode math rounding mode |
japod@6 | 30 |
* @throws IllegalArgumentException if conversion value is <= 0 |
japod@6 | 31 |
*/ |
japod@6 | 32 |
private FixedBidirectionalRatioConversion(BigDecimal ratioConstant, RoundingMode mode) { |
japod@6 | 33 |
if (ratioConstant.compareTo(BigDecimal.ZERO) <= 0) |
japod@6 | 34 |
throw new IllegalArgumentException("Conversion value cannot be <= 0"); |
japod@6 | 35 |
this.currency1ToCurrency2Constant = ratioConstant; |
japod@6 | 36 |
this.currency2ToCurrency1Constant = BigDecimal.ONE.setScale(SCALE_MAX_DEFAULT).divide(ratioConstant, mode); |
japod@6 | 37 |
} |
japod@6 | 38 |
|
japod@6 | 39 |
public BigDecimal getCurrency1ToCurrency2Constant() { |
japod@6 | 40 |
return currency1ToCurrency2Constant; |
japod@6 | 41 |
} |
japod@6 | 42 |
|
japod@6 | 43 |
public BigDecimal getCurrency2ToCurrency1Constant() { |
japod@6 | 44 |
return currency2ToCurrency1Constant; |
japod@6 | 45 |
} |
japod@6 | 46 |
} |
japod@6 | 47 |
|
japod@6 | 48 |
|
japod@6 | 49 |
/** |
japod@6 | 50 |
* Returns new instance Conversion properties - fixed conversion ratio - both directions |
japod@6 | 51 |
* Default max scale is set to 20 |
japod@6 | 52 |
* |
japod@6 | 53 |
* @param conversionConstant value of constant for conversion |
japod@6 | 54 |
* @param roundingMode math rounding mode |
japod@6 | 55 |
* @return new instance of ConversionProperties class |
japod@6 | 56 |
* @throws IllegalArgumentException if conversion constant is <= 0 |
japod@6 | 57 |
* @see org.apidesign.apifest08.currency.ConversionProperties.FixedBidirectionalRatioConversion |
japod@6 | 58 |
*/ |
japod@6 | 59 |
public static ConversionProperties create(BigDecimal conversionConstant, RoundingMode roundingMode) { |
japod@6 | 60 |
return create(new FixedBidirectionalRatioConversion(conversionConstant, roundingMode), roundingMode); |
japod@6 | 61 |
} |
japod@6 | 62 |
|
japod@6 | 63 |
/** |
japod@6 | 64 |
* Returns new instance Conversion properties |
japod@6 | 65 |
* |
japod@6 | 66 |
* @param conversionRatioProvider provider for conversion constants |
japod@6 | 67 |
* @param roundingMode math rounding mode |
japod@6 | 68 |
* @return new instance of ConversionProperties class |
japod@6 | 69 |
*/ |
japod@6 | 70 |
public static ConversionProperties create(ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode) { |
japod@6 | 71 |
if (conversionRatioProvider == null || roundingMode == null) |
japod@6 | 72 |
throw new NullPointerException(); |
japod@6 | 73 |
return new ConversionProperties(conversionRatioProvider, roundingMode); |
japod@6 | 74 |
} |
japod@6 | 75 |
|
japod@6 | 76 |
/** |
japod@6 | 77 |
* Returns new instance Conversion properties with values set to conversionConstant=BigDecimal.ONE |
japod@6 | 78 |
* and roundingMode=RoundingMode.HALF_EVEN |
japod@6 | 79 |
* |
japod@6 | 80 |
* Conversion is bidirectional. |
japod@6 | 81 |
* |
japod@6 | 82 |
* @return |
japod@6 | 83 |
*/ |
japod@6 | 84 |
public static ConversionProperties createDefault() { |
japod@6 | 85 |
return create(BigDecimal.ONE, RoundingMode.HALF_EVEN); |
japod@6 | 86 |
} |
japod@6 | 87 |
|
japod@6 | 88 |
private ConversionProperties(ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode) { |
japod@6 | 89 |
this.conversionRatioProvider = conversionRatioProvider; |
japod@6 | 90 |
this.roundingMode = roundingMode; |
japod@6 | 91 |
} |
japod@6 | 92 |
|
japod@6 | 93 |
|
japod@6 | 94 |
public RoundingMode getRoundingMode() { |
japod@6 | 95 |
return roundingMode; |
japod@6 | 96 |
} |
japod@6 | 97 |
|
japod@6 | 98 |
public ConversionRatioProvider getConversionRatioProvider() { |
japod@6 | 99 |
return conversionRatioProvider; |
japod@6 | 100 |
} |
japod@6 | 101 |
} |