|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package net.java.javamoney.extras; |
| 17 | + |
| 18 | +import java.math.BigDecimal; |
| 19 | +import java.math.MathContext; |
| 20 | +import java.text.NumberFormat; |
| 21 | + |
| 22 | +import javax.money.MonetaryAmount; |
| 23 | +import javax.money.MonetaryOperator; |
| 24 | +import javax.money.Permil; |
| 25 | + |
| 26 | +/** |
| 27 | + * This class allows to extract the permil of a {@link MonetaryAmount} instance. |
| 28 | + * |
| 29 | + * @version 0.5 |
| 30 | + * @author Anatole Tresch |
| 31 | + * @author Werner Keil |
| 32 | + * |
| 33 | + * @see <a href="http://en.wikipedia.org/wiki/Per_mil">Wikipedia: Per mil</a> |
| 34 | + */ |
| 35 | +public final class BasisPoint implements MonetaryOperator { |
| 36 | + |
| 37 | + private static final MathContext DEFAULT_MATH_CONTEXT = initDefaultMathContext(); |
| 38 | + private static final BigDecimal ONE_TENTHOUSAND = new BigDecimal(10000, |
| 39 | + DEFAULT_MATH_CONTEXT); |
| 40 | + |
| 41 | + private final BigDecimal basisPointValue; |
| 42 | + |
| 43 | + /** |
| 44 | + * Access the shared instance of {@link Permil} for use. |
| 45 | + * |
| 46 | + * @return the shared instance, never {@code null}. |
| 47 | + */ |
| 48 | + private BasisPoint(final BigDecimal decimal) { |
| 49 | + basisPointValue = calcBasisPoint(decimal); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get {@link MathContext} for {@link Permil} instances. |
| 54 | + * |
| 55 | + * @return the {@link MathContext} to be used, by default |
| 56 | + * {@link MathContext#DECIMAL64}. |
| 57 | + */ |
| 58 | + private static MathContext initDefaultMathContext() { |
| 59 | + // TODO Initialize default, e.g. by system properties, or better: |
| 60 | + // classpath properties! |
| 61 | + return MathContext.DECIMAL64; |
| 62 | + } |
| 63 | + |
| 64 | +/** |
| 65 | + * Factory method creating a new instance with the given {@code BigDecimal) permil value; |
| 66 | + * @param decimal the decimal value of the permil operator being created. |
| 67 | + * @return a new {@code Permil} operator |
| 68 | + */ |
| 69 | + public static BasisPoint of(BigDecimal decimal) { |
| 70 | + return new BasisPoint(decimal); // TODO caching, e.g. array for 1-100 |
| 71 | + // might |
| 72 | + // work. |
| 73 | + } |
| 74 | + |
| 75 | +/** |
| 76 | + * Factory method creating a new instance with the given {@code Number) permil value; |
| 77 | + * @param decimal the decimal value of the permil operator being created. |
| 78 | + * @return a new {@code Permil} operator |
| 79 | + */ |
| 80 | + public static BasisPoint of(Number number) { |
| 81 | + return of(getBigDecimal(number, DEFAULT_MATH_CONTEXT)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets the permil of the amount. |
| 86 | + * <p> |
| 87 | + * This returns the monetary amount in permil. For example, for 10% 'EUR |
| 88 | + * 2.35' will return 0.235. |
| 89 | + * <p> |
| 90 | + * This is returned as a {@code MonetaryAmount}. |
| 91 | + * |
| 92 | + * @return the permil result of the amount, never {@code null} |
| 93 | + */ |
| 94 | + public MonetaryAmount apply(MonetaryAmount amount) { |
| 95 | + return amount.multiply(basisPointValue); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * (non-Javadoc) |
| 100 | + * |
| 101 | + * @see java.lang.Object#toString() |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + return NumberFormat.getInstance() |
| 106 | + .format( |
| 107 | + basisPointValue.multiply(ONE_TENTHOUSAND, |
| 108 | + DEFAULT_MATH_CONTEXT)) |
| 109 | + + |
| 110 | + "\u2031"; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Converts to {@link BigDecimal}, if necessary, or casts, if possible. |
| 115 | + * |
| 116 | + * @param number |
| 117 | + * The {@link Number} |
| 118 | + * @param mathContext |
| 119 | + * the {@link MathContext} |
| 120 | + * @return the {@code number} as {@link BigDecimal} |
| 121 | + */ |
| 122 | + private static final BigDecimal getBigDecimal(Number number, |
| 123 | + MathContext mathContext) { |
| 124 | + if (number instanceof BigDecimal) { |
| 125 | + return (BigDecimal) number; |
| 126 | + } else { |
| 127 | + return new BigDecimal(number.doubleValue(), mathContext); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Calculate a BigDecimal value for a Permil e.g. "3" (3 permil) will |
| 133 | + * generate .003 |
| 134 | + * |
| 135 | + * @return java.math.BigDecimal |
| 136 | + * @param decimal |
| 137 | + * java.math.BigDecimal |
| 138 | + */ |
| 139 | + private static final BigDecimal calcBasisPoint(BigDecimal decimal) { |
| 140 | + return decimal.divide(ONE_TENTHOUSAND, DEFAULT_MATH_CONTEXT); // we now |
| 141 | + // have |
| 142 | + // .003 |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments