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