| 1 | /* |
| 2 | * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences. |
| 3 | * Copyright (C) 2007 - JScience (http://jscience.org/) |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software is |
| 7 | * freely granted, provided that this notice is preserved. |
| 8 | */ |
| 9 | package javax.measure; |
| 10 | |
| 11 | import java.math.BigDecimal; |
| 12 | import java.math.MathContext; |
| 13 | |
| 14 | import javax.measure.converter.AddConverter; |
| 15 | import javax.measure.converter.RationalConverter; |
| 16 | import javax.measure.converter.UnitConverter; |
| 17 | import javax.measure.quantity.Quantity; |
| 18 | import javax.measure.unit.Unit; |
| 19 | |
| 20 | /** |
| 21 | * <p> This class represents a measure whose value is an arbitrary-precision |
| 22 | * decimal number.</p> |
| 23 | * |
| 24 | * <p> When converting, applications may supply the |
| 25 | * <code>java.math.Context</code>:[code] |
| 26 | * DecimalMeasure<Velocity> c = DecimalMeasure.valueOf("299792458 m/s"); |
| 27 | * DecimalMeasure<Velocity> milesPerHour = c.to(MILES_PER_HOUR, MathContext.DECIMAL128); |
| 28 | * System.out.println(milesPerHour); |
| 29 | * |
| 30 | * > 670616629.3843951324266284896206156 mph |
| 31 | * [/code] |
| 32 | * |
| 33 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
| 34 | * @version 4.3, October 3, 2007 |
| 35 | */ |
| 36 | public class DecimalMeasure<Q extends Quantity> extends Measure<BigDecimal, Q> { |
| 37 | |
| 38 | /** |
| 39 | * Holds the BigDecimal value. |
| 40 | */ |
| 41 | private final BigDecimal _value; |
| 42 | |
| 43 | /** |
| 44 | * Holds the unit. |
| 45 | */ |
| 46 | private final Unit<Q> _unit; |
| 47 | |
| 48 | /** |
| 49 | * Creates a decimal measure for the specified number stated in the |
| 50 | * specified unit. |
| 51 | */ |
| 52 | public DecimalMeasure(BigDecimal value, Unit<Q> unit) { |
| 53 | _value = value; |
| 54 | _unit = unit; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the decimal measure for the specified number stated in the |
| 59 | * specified unit. |
| 60 | * |
| 61 | * @param decimal the measurement value. |
| 62 | * @param unit the measurement unit. |
| 63 | */ |
| 64 | public static <Q extends Quantity> DecimalMeasure<Q> valueOf( |
| 65 | BigDecimal decimal, Unit<Q> unit) { |
| 66 | return new DecimalMeasure<Q>(decimal, unit); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the decimal measure for the specified textual representation. |
| 71 | * This method first reads the <code>BigDecimal</code> value, then |
| 72 | * the unit if any (value and unit should be separated by white spaces). |
| 73 | * |
| 74 | * @param csq the decimal measure representation (including unit if any). |
| 75 | * @throws NumberFormatException if the specified character sequence is |
| 76 | * not a valid representation of decimal measure. |
| 77 | */ |
| 78 | @SuppressWarnings("unchecked") |
| 79 | public static <Q extends Quantity> DecimalMeasure<Q> valueOf(CharSequence csq) { |
| 80 | String str = csq.toString(); |
| 81 | int numberLength = str.length(); |
| 82 | int unitStartIndex = -1; |
| 83 | for (int i=0; i < str.length(); i++) { |
| 84 | if (Character.isWhitespace(str.charAt(i))) { |
| 85 | for (int j=i+1; j < str.length(); j++) { |
| 86 | if (!Character.isWhitespace(str.charAt(j))) { |
| 87 | unitStartIndex = j; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | numberLength = i; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | BigDecimal decimal = new BigDecimal(str.substring(0, numberLength)); |
| 96 | Unit unit = Unit.ONE; |
| 97 | if (unitStartIndex > 0) { |
| 98 | unit = Unit.valueOf(str.substring(unitStartIndex)); |
| 99 | } |
| 100 | return new DecimalMeasure<Q>(decimal, unit); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Unit<Q> getUnit() { |
| 105 | return _unit; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public BigDecimal getValue() { |
| 110 | return _value; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the decimal measure equivalent to this measure but stated in the |
| 115 | * specified unit. This method will raise an ArithmeticException if the |
| 116 | * resulting measure does not have a terminating decimal expansion. |
| 117 | * |
| 118 | * @param unit the new measurement unit. |
| 119 | * @return the measure stated in the specified unit. |
| 120 | * @throws ArithmeticException if the converted measure value does not have |
| 121 | * a terminating decimal expansion |
| 122 | * @see #to(Unit, MathContext) |
| 123 | */ |
| 124 | @Override |
| 125 | public DecimalMeasure<Q> to(Unit<Q> unit) { |
| 126 | return to(unit, null); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the decimal measure equivalent to this measure but stated in the |
| 131 | * specified unit, the conversion is performed using the specified math |
| 132 | * context. |
| 133 | * |
| 134 | * @param unit the new measurement unit. |
| 135 | * @param mathContext the mathContext used to convert |
| 136 | * <code>BigDecimal</code> values or <code>null</code> if none. |
| 137 | * @return the measure stated in the specified unit. |
| 138 | * @throws ArithmeticException if the result is inexact but the |
| 139 | * rounding mode is <code>MathContext.UNNECESSARY</code> or |
| 140 | * <code>mathContext.precision == 0</tt> and the quotient has a |
| 141 | * non-terminating decimal expansion. |
| 142 | */ |
| 143 | public DecimalMeasure<Q> to(Unit<Q> unit, MathContext mathContext) { |
| 144 | if ((unit == _unit) || (unit.equals(_unit))) |
| 145 | return this; |
| 146 | UnitConverter cvtr = _unit.getConverterTo(unit); |
| 147 | if (cvtr instanceof RationalConverter) { |
| 148 | RationalConverter factor = (RationalConverter) cvtr; |
| 149 | BigDecimal dividend = BigDecimal.valueOf(factor.getDividend()); |
| 150 | BigDecimal divisor = BigDecimal.valueOf(factor.getDivisor()); |
| 151 | BigDecimal result = mathContext == null ? |
| 152 | _value.multiply(dividend).divide(divisor) : |
| 153 | _value.multiply(dividend, mathContext).divide(divisor, mathContext); |
| 154 | return new DecimalMeasure<Q>(result, unit); |
| 155 | } else if (cvtr.isLinear()) { |
| 156 | BigDecimal factor = BigDecimal.valueOf(cvtr.convert(1.0)); |
| 157 | BigDecimal result = mathContext == null ? |
| 158 | _value.multiply(factor) : _value.multiply(factor, mathContext); |
| 159 | return new DecimalMeasure<Q>(result, unit); |
| 160 | } else if (cvtr instanceof AddConverter) { |
| 161 | BigDecimal offset = BigDecimal.valueOf(((AddConverter)cvtr).getOffset()); |
| 162 | BigDecimal result = mathContext == null ? |
| 163 | _value.add(offset) : _value.add(offset, mathContext); |
| 164 | return new DecimalMeasure<Q>(result, unit); |
| 165 | } else { // Non-linear and not an offset, convert the double value. |
| 166 | BigDecimal result = BigDecimal.valueOf(cvtr.convert(_value.doubleValue())); |
| 167 | return new DecimalMeasure<Q>(result, unit); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public double doubleValue(Unit<Q> unit) { |
| 172 | if ((unit == _unit) || (unit.equals(_unit))) |
| 173 | return _value.doubleValue(); |
| 174 | return _unit.getConverterTo(unit).convert(_value.doubleValue()); |
| 175 | } |
| 176 | |
| 177 | private static final long serialVersionUID = 1L; |
| 178 | } |