task2/solution01/src/org/apidesign/apifest08/currency/ConversionProperties.java
changeset 29 f6073056b9fe
parent 6 97662396c0fd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task2/solution01/src/org/apidesign/apifest08/currency/ConversionProperties.java	Wed Oct 01 10:43:05 2008 +0200
     1.3 @@ -0,0 +1,101 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import java.math.RoundingMode;
     1.8 +
     1.9 +/**
    1.10 + * This class encapsulates settings for currency conversion
    1.11 + * We can use it eg. for uptime options etc. in future versions
    1.12 + *
    1.13 + * @author Ladislav Vitasek
    1.14 + */
    1.15 +final public class ConversionProperties {
    1.16 +
    1.17 +    private final ConversionRatioProvider conversionRatioProvider;
    1.18 +    private final RoundingMode roundingMode;
    1.19 +
    1.20 +    /**
    1.21 +     * Returns conversion constants - conversion operation is invertable
    1.22 +     * Default
    1.23 +     */
    1.24 +    private final static class FixedBidirectionalRatioConversion implements ConversionRatioProvider {
    1.25 +        private final BigDecimal currency1ToCurrency2Constant;
    1.26 +        private final BigDecimal currency2ToCurrency1Constant;
    1.27 +        private static final int SCALE_MAX_DEFAULT = 20;
    1.28 +
    1.29 +        /**
    1.30 +         * Constructor
    1.31 +         * @param ratioConstant fixed ratio constant
    1.32 +         * @param mode math rounding mode
    1.33 +         * @throws IllegalArgumentException if conversion value is <= 0
    1.34 +         */
    1.35 +        private FixedBidirectionalRatioConversion(BigDecimal ratioConstant, RoundingMode mode) {
    1.36 +            if (ratioConstant.compareTo(BigDecimal.ZERO) <= 0)
    1.37 +                throw new IllegalArgumentException("Conversion value cannot be <= 0");
    1.38 +            this.currency1ToCurrency2Constant = ratioConstant;
    1.39 +            this.currency2ToCurrency1Constant = BigDecimal.ONE.setScale(SCALE_MAX_DEFAULT).divide(ratioConstant, mode);
    1.40 +        }
    1.41 +
    1.42 +        public BigDecimal getCurrency1ToCurrency2Constant() {
    1.43 +            return currency1ToCurrency2Constant;
    1.44 +        }
    1.45 +
    1.46 +        public BigDecimal getCurrency2ToCurrency1Constant() {
    1.47 +            return currency2ToCurrency1Constant;
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +
    1.52 +    /**
    1.53 +     * Returns new instance Conversion properties - fixed conversion ratio - both directions
    1.54 +     * Default max scale is set to 20
    1.55 +     *
    1.56 +     * @param conversionConstant value of constant for conversion
    1.57 +     * @param roundingMode       math rounding mode
    1.58 +     * @return new instance of ConversionProperties class
    1.59 +     * @throws IllegalArgumentException if conversion constant is <= 0 
    1.60 +     * @see org.apidesign.apifest08.currency.ConversionProperties.FixedBidirectionalRatioConversion
    1.61 +     */
    1.62 +    public static ConversionProperties create(BigDecimal conversionConstant, RoundingMode roundingMode) {
    1.63 +        return create(new FixedBidirectionalRatioConversion(conversionConstant, roundingMode), roundingMode);
    1.64 +    }
    1.65 +
    1.66 +    /**
    1.67 +     * Returns new instance Conversion properties
    1.68 +     *
    1.69 +     * @param conversionRatioProvider provider for conversion constants
    1.70 +     * @param roundingMode       math rounding mode
    1.71 +     * @return new instance of ConversionProperties class
    1.72 +     */
    1.73 +    public static ConversionProperties create(ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode) {
    1.74 +        if (conversionRatioProvider == null || roundingMode == null)
    1.75 +            throw new NullPointerException();
    1.76 +        return new ConversionProperties(conversionRatioProvider, roundingMode);
    1.77 +    }
    1.78 +
    1.79 +    /**
    1.80 +     * Returns new instance Conversion properties with values set to conversionConstant=BigDecimal.ONE
    1.81 +     * and roundingMode=RoundingMode.HALF_EVEN
    1.82 +     * 
    1.83 +     * Conversion is bidirectional.
    1.84 +     *
    1.85 +     * @return
    1.86 +     */
    1.87 +    public static ConversionProperties createDefault() {
    1.88 +        return create(BigDecimal.ONE, RoundingMode.HALF_EVEN);
    1.89 +    }
    1.90 +
    1.91 +    private ConversionProperties(ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode) {
    1.92 +        this.conversionRatioProvider = conversionRatioProvider;
    1.93 +        this.roundingMode = roundingMode;
    1.94 +    }
    1.95 +
    1.96 +
    1.97 +    public RoundingMode getRoundingMode() {
    1.98 +        return roundingMode;
    1.99 +    }
   1.100 +
   1.101 +    public ConversionRatioProvider getConversionRatioProvider() {
   1.102 +        return conversionRatioProvider;
   1.103 +    }
   1.104 +}