From 830fba1995229ee3111147b98374737d27925e3c Mon Sep 17 00:00:00 2001
From: Chris Hennick
- * The base class for our {@link UnitConverter} implementations.
- *
+ * The base class for our {@link UnitConverter} implementations.
+ *
- * This class represents the immutable result of a scalar measurement stated in a known unit.
- *
- * To avoid any loss of precision, known exact quantities (e.g. physical constants) should not be created from
+ * This class represents the immutable result of a scalar measurement stated in a known unit.
+ *
+ * To avoid any loss of precision, known exact quantities (e.g. physical constants) should not be created from
- * Quantities can be converted to different units.
+ * Quantities can be converted to different units.
- * Applications may sub-class {@link AbstractQuantity} for particular quantity types.
+ * Applications may sub-class {@link AbstractQuantity} for particular quantity types.
- * All instances of this class shall be immutable.
- * [pair] = [left] x [right]
).
- */
- public static final class Pair extends AbstractConverter implements Serializable {
-
+ */
+ private static final long serialVersionUID = -4460463244427587361L;
+
+ @Override
+ public boolean isIdentity() {
+ return true;
+ }
+
+ @Override
+ public Identity inverse() {
+ return this;
+ }
+
+ @Override
+ public double convert(double value) {
+ return value;
+ }
+
+ @Override
+ public BigDecimal convert(BigDecimal value, MathContext ctx) {
+ return value;
+ }
+
+ @Override
+ public UnitConverter concatenate(UnitConverter converter) {
+ return converter;
+ }
+
+ @Override
+ public boolean equals(Object cvtr) {
+ return (cvtr instanceof Identity);
+ }
+
+ @Override
+ public int hashCode() {
+ return 0;
+ }
+
+ @Override
+ public boolean isLinear() {
+ return true;
+ }
+ }
+
+ /**
+ * This class represents converters made up of two or more separate converters (in matrix notation [pair] = [left] x [right]
).
+ */
+ public static final class Pair extends AbstractConverter implements Serializable {
+
/**
*
- */
- private static final long serialVersionUID = -123063827821728331L;
-
- /**
- * Holds the first converter.
- */
- private final UnitConverter left;
-
- /**
- * Holds the second converter.
- */
- private final UnitConverter right;
-
- /**
- * Creates a pair converter resulting from the combined transformation of the specified converters.
- *
- * @param left
- * the left converter, not null
.
- * @param right
- * the right converter.
- * @throws IllegalArgumentException
- * if either the left or right converter are null
- */
- public Pair(UnitConverter left, UnitConverter right) {
- if (left != null && right != null) {
- this.left = left;
- this.right = right;
- } else {
- throw new IllegalArgumentException("Converters cannot be null");
- }
- }
-
- @Override
- public boolean isLinear() {
- return left.isLinear() && right.isLinear();
- }
-
- @Override
- public boolean isIdentity() {
- return false;
- }
-
- @Override
- public Listnull
.
+ * @param right
+ * the right converter.
+ * @throws IllegalArgumentException
+ * if either the left or right converter are null
+ */
+ public Pair(UnitConverter left, UnitConverter right) {
+ if (left != null && right != null) {
+ this.left = left;
+ this.right = right;
+ } else {
+ throw new IllegalArgumentException("Converters cannot be null");
+ }
+ }
+
+ @Override
+ public boolean isLinear() {
+ return left.isLinear() && right.isLinear();
+ }
+
+ @Override
+ public boolean isIdentity() {
+ return false;
+ }
+
+ @Override
+ public Listdouble
constants but from
- * their decimal representation.
+/*
+ * Units of Measurement Implementation for Java SE
+ * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
+ * and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package tec.uom.se;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.math.MathContext;
+import java.util.Comparator;
+import java.util.Objects;
+
+import javax.measure.Quantity;
+import javax.measure.Unit;
+import javax.measure.UnitConverter;
+import javax.measure.quantity.Dimensionless;
+
+import tec.uom.lib.common.function.UnitSupplier;
+import tec.uom.lib.common.function.ValueSupplier;
+import tec.uom.se.format.QuantityFormat;
+import tec.uom.se.function.NaturalOrder;
+import tec.uom.se.quantity.Quantities;
+
+/**
+ * double
constants but from
+ * their decimal representation.
*
* public static final Quantity<Velocity> C = NumberQuantity.parse("299792458 m/s").asType(Velocity.class);
* // Speed of Light (exact).
- *
- *
+ *
+ *
*
* Quantity<Velocity> milesPerHour = C.to(MILES_PER_HOUR); // Use double implementation (fast).
* System.out.println(milesPerHour);
*
* > 670616629.3843951 m/h
- *
- *
+ *
+ *
*
* // Quantity of type Mass based on double primitive types.
- *
* public class MassAmount extends AbstractQuantity<Mass> {
@@ -96,363 +96,363 @@
* // Specializations of complex numbers measurements.
* public final class Current extends ComplexQuantity<ElectricCurrent> {...}
* public final class Tension extends ComplexQuantity<ElectricPotential> {...}
- * > implements ComparableQuantity
, UnitSupplier
, ValueSupplier
+ * All instances of this class shall be immutable. + *
+ * + * @author Werner Keil + * @version 1.0.6, August 7, 2017 + * @since 1.0 + */ +@SuppressWarnings("unchecked") +public abstract class AbstractQuantity> implements ComparableQuantity, UnitSupplier, ValueSupplier{ + /** * - */ - private static final long serialVersionUID = 293852425369811882L; - - private final Unit unit; - - /** - * Holds a dimensionless quantity of none (exact). - */ - public static final QuantityNONE = Quantities.getQuantity(0, AbstractUnit.ONE); - - /** - * Holds a dimensionless quantity of one (exact). - */ - public static final Quantity ONE = Quantities.getQuantity(1, AbstractUnit.ONE); - - /** - * constructor. - */ - protected AbstractQuantity(Unit unit) { - this.unit = unit; - } - - /** - * Returns the numeric value of the quantity. - * - * @return the quantity value. - */ - @Override - public abstract Number getValue(); - - /** - * Returns the measurement unit. - * - * @return the measurement unit. - */ - @Override - public UnitgetUnit() { - return unit; - } - - /** - * Convenient method equivalent to {@link #to(javax.measure.unit.Unit) to(this.getUnit().toSI())}. - * - * @return this measure or a new measure equivalent to this measure but stated in SI units. - * @throws ArithmeticException - * if the result is inexact and the quotient has a non-terminating decimal expansion. - */ - public QuantitytoSI() { - return to(this.getUnit().getSystemUnit()); - } - - /** - * Returns this measure after conversion to specified unit. The default implementation returnsMeasure.valueOf(doubleValue(unit), unit)
- * . If this measure is already stated in the specified unit, then this measure is returned and no conversion is performed. - * - * @param unit - * the unit in which the returned measure is stated. - * @return this measure or a new measure equivalent to this measure but stated in the specified unit. - * @throws ArithmeticException - * if the result is inexact and the quotient has a non-terminating decimal expansion. - */ - @Override - public ComparableQuantityto(Unitunit) { - if (unit.equals(this.getUnit())) { - return this; - } - UnitConverter t = getUnit().getConverterTo(unit); - Number convertedValue = t.convert(getValue()); - return Quantities.getQuantity(convertedValue, unit); - } - - /** - * Returns this measure after conversion to specified unit. The default implementation returns - *Measure.valueOf(decimalValue(unit, ctx), unit)
. If this measure is already stated in the specified unit, then this measure is - * returned and no conversion is performed. - * - * @param unit - * the unit in which the returned measure is stated. - * @param ctx - * the math context to use for conversion. - * @return this measure or a new measure equivalent to this measure but stated in the specified unit. - * @throws ArithmeticException - * if the result is inexact but the rounding mode isUNNECESSARY
ormathContext.precision == 0
and the quotient - * has a non-terminating decimal expansion. - */ - public Quantityto(Unitunit, MathContext ctx) { - if (unit.equals(this.getUnit())) { - return this; - } - return Quantities.getQuantity(decimalValue(unit, ctx), unit); - } - - @Override - public boolean isGreaterThan(Quantitythat) { - return this.compareTo(that) > 0; - } - - @Override - public boolean isGreaterThanOrEqualTo(Quantitythat) { - return this.compareTo(that) >= 0; - } - - @Override - public boolean isLessThan(Quantitythat) { - return this.compareTo(that) < 0; - } - - @Override - public boolean isLessThanOrEqualTo(Quantitythat) { - return this.compareTo(that) <= 0; - } - - @Override - public boolean isEquivalentOf(Quantitythat) { - return this.compareTo(that) == 0; - } - - @Override - public boolean isEquivalentTo(Quantitythat) { - return isEquivalentOf(that); - } - - /** - * Compares this measure to the specified Measurement quantity. The default implementation compares the {@link AbstractQuantity#doubleValue(Unit)} - * of both this measure and the specified Measurement stated in the same unit (this measure's {@link #getUnit() unit}). - * - * @return a negative integer, zero, or a positive integer as this measure is less than, equal to, or greater than the specified Measurement - * quantity. - * @see {@link NaturalOrder} - */ - @Override - public int compareTo(Quantitythat) { - final Comparator> comparator = new NaturalOrder<>(); - return comparator.compare(this, that); - } - - /** - * Compares this quantity against the specified object for strict equality (same unit and same amount). - * - * - * Similarly to the {@link BigDecimal#equals} method which consider 2.0 and 2.00 as different objects because of different internal scales, - * quantities such as
- * - *Quantities.getQuantity(3.0, KILOGRAM)
Quantities.getQuantity(3, KILOGRAM)
and - *Quantities.getQuantity("3 kg")
might not be considered equals because of possible differences in their implementations. - *- * To compare quantities stated using different units or using different amount implementations the {@link #compareTo compareTo} or - * {@link #equals(javax.measure.Quantity, double, javax.measure.unit.Unit) equals(Quantity, epsilon, epsilonUnit)} methods should be used. - *
- * - * @param obj - * the object to compare with. + */ + private static final long serialVersionUID = 293852425369811882L; + + private final Unitunit; + + /** + * Holds a dimensionless quantity of none (exact). + */ + public static final QuantityNONE = Quantities.getQuantity(0, AbstractUnit.ONE); + + /** + * Holds a dimensionless quantity of one (exact). + */ + public static final Quantity ONE = Quantities.getQuantity(1, AbstractUnit.ONE); + + /** + * constructor. + */ + protected AbstractQuantity(Unit unit) { + this.unit = unit; + } + + /** + * Returns the numeric value of the quantity. + * + * @return the quantity value. + */ + @Override + public abstract Number getValue(); + + /** + * Returns the measurement unit. + * + * @return the measurement unit. + */ + @Override + public UnitgetUnit() { + return unit; + } + + /** + * Convenient method equivalent to {@link #to(javax.measure.unit.Unit) to(this.getUnit().toSI())}. + * + * @return this measure or a new measure equivalent to this measure but stated in SI units. + * @throws ArithmeticException + * if the result is inexact and the quotient has a non-terminating decimal expansion. + */ + public QuantitytoSI() { + return to(this.getUnit().getSystemUnit()); + } + + /** + * Returns this measure after conversion to specified unit. The default implementation returnsMeasure.valueOf(doubleValue(unit), unit)
+ * . If this measure is already stated in the specified unit, then this measure is returned and no conversion is performed. + * + * @param unit + * the unit in which the returned measure is stated. + * @return this measure or a new measure equivalent to this measure but stated in the specified unit. + * @throws ArithmeticException + * if the result is inexact and the quotient has a non-terminating decimal expansion. + */ + @Override + public ComparableQuantityto(Unitunit) { + if (unit.equals(this.getUnit())) { + return this; + } + UnitConverter t = getUnit().getConverterTo(unit); + Number convertedValue = t.convert(getValue()); + return Quantities.getQuantity(convertedValue, unit); + } + + /** + * Returns this measure after conversion to specified unit. The default implementation returns + *Measure.valueOf(decimalValue(unit, ctx), unit)
. If this measure is already stated in the specified unit, then this measure is + * returned and no conversion is performed. + * + * @param unit + * the unit in which the returned measure is stated. + * @param ctx + * the math context to use for conversion. + * @return this measure or a new measure equivalent to this measure but stated in the specified unit. + * @throws ArithmeticException + * if the result is inexact but the rounding mode isUNNECESSARY
ormathContext.precision == 0
and the quotient + * has a non-terminating decimal expansion. + */ + public Quantityto(Unitunit, MathContext ctx) { + if (unit.equals(this.getUnit())) { + return this; + } + return Quantities.getQuantity(decimalValue(unit, ctx), unit); + } + + @Override + public boolean isGreaterThan(Quantitythat) { + return this.compareTo(that) > 0; + } + + @Override + public boolean isGreaterThanOrEqualTo(Quantitythat) { + return this.compareTo(that) >= 0; + } + + @Override + public boolean isLessThan(Quantitythat) { + return this.compareTo(that) < 0; + } + + @Override + public boolean isLessThanOrEqualTo(Quantitythat) { + return this.compareTo(that) <= 0; + } + + @Override + public boolean isEquivalentOf(Quantitythat) { + return this.compareTo(that) == 0; + } + + @Override + public boolean isEquivalentTo(Quantitythat) { + return isEquivalentOf(that); + } + + /** + * Compares this measure to the specified Measurement quantity. The default implementation compares the {@link AbstractQuantity#doubleValue(Unit)} + * of both this measure and the specified Measurement stated in the same unit (this measure's {@link #getUnit() unit}). + * + * @return a negative integer, zero, or a positive integer as this measure is less than, equal to, or greater than the specified Measurement + * quantity. + * @see {@link NaturalOrder} + */ + @Override + public int compareTo(Quantitythat) { + final Comparator> comparator = new NaturalOrder<>(); + return comparator.compare(this, that); + } + + /** + * Compares this quantity against the specified object for strict equality (same unit and same amount). + * + * + * Similarly to the {@link BigDecimal#equals} method which consider 2.0 and 2.00 as different objects because of different internal scales, + * quantities such as
+ * + *Quantities.getQuantity(3.0, KILOGRAM)
Quantities.getQuantity(3, KILOGRAM)
and + *Quantities.getQuantity("3 kg")
might not be considered equals because of possible differences in their implementations. + *+ * To compare quantities stated using different units or using different amount implementations the {@link #compareTo compareTo} or + * {@link #equals(javax.measure.Quantity, double, javax.measure.unit.Unit) equals(Quantity, epsilon, epsilonUnit)} methods should be used. + *
+ * + * @param obj + * the object to compare with. * @returnthis.getUnit.equals(obj.getUnit()) - * && this.getValue().equals(obj.getValue())
- */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof AbstractQuantity>) { - AbstractQuantity> that = (AbstractQuantity>) obj; - return Objects.equals(getUnit(), that.getUnit()) && Objects.equals(getValue(), that.getValue()); - } - return false; - } - - /** - * Compares this quantity and the specified quantity to the given accuracy. Quantities are considered approximately equals if their absolute - * differences when stated in the same specified unit is less than the specified epsilon. - * - * @param that - * the quantity to compare with. - * @param epsilon - * the absolute error stated in epsilonUnit. - * @param epsilonUnit - * the epsilon unit. - * @returnabs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilon
- */ - public boolean equals(AbstractQuantitythat, double epsilon, UnitepsilonUnit) { - return Math.abs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilon; - } - - /** - * Returns the hash code for this quantity. - * - * @return the hash code value. - */ - @Override - public int hashCode() { - return Objects.hash(getUnit(), getValue()); - } - - public abstract boolean isBig(); - - /** - * Returns theString
representation of this quantity. The string produced for a given quantity is always the same; it is not affected - * by locale. This means that it can be used as a canonical string representation for exchanging quantity, or as a key for a Hashtable, etc. - * Locale-sensitive quantity formatting and parsing is handled by the {@link MeasurementFormat} class and its subclasses. - * - * @returnUnitFormat.getInternational().format(this)
- */ - @Override - public String toString() { - return String.valueOf(getValue()) + " " + String.valueOf(getUnit()); - } - - public abstract BigDecimal decimalValue(Unitunit, MathContext ctx) throws ArithmeticException; - - public abstract double doubleValue(Unitunit) throws ArithmeticException; - - public final int intValue(Unitunit) throws ArithmeticException { - long longValue = longValue(unit); - if ((longValue < Integer.MIN_VALUE) || (longValue > Integer.MAX_VALUE)) { - throw new ArithmeticException("Cannot convert " + longValue + " to int (overflow)"); - } - return (int) longValue; - } - - protected long longValue(Unitunit) throws ArithmeticException { - double result = doubleValue(unit); - if ((result < Long.MIN_VALUE) || (result > Long.MAX_VALUE)) { - throw new ArithmeticException("Overflow (" + result + ")"); - } - return (long) result; - } - - protected final float floatValue(Unitunit) { - return (float) doubleValue(unit); - } - - @Override - public, E extends Quantity > ComparableQuantity divide(Quantity that, Class asTypeQuantity) { - - return divide(Objects.requireNonNull(that)).asType(Objects.requireNonNull(asTypeQuantity)); - - } - - @Override - public , E extends Quantity > ComparableQuantity multiply(Quantity that, Class asTypeQuantity) { - return multiply(Objects.requireNonNull(that)).asType(Objects.requireNonNull(asTypeQuantity)); - } - - @Override - public > ComparableQuantity inverse(Class quantityClass) { - return inverse().asType(quantityClass); - } - - /** - * Casts this quantity to a parameterized quantity of specified nature or throw a ClassCastException
if the dimension of the specified - * quantity and its unit's dimension do not match. For example:
+ * && this.getValue().equals(obj.getValue()) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof AbstractQuantity>) { + AbstractQuantity> that = (AbstractQuantity>) obj; + return Objects.equals(getUnit(), that.getUnit()) && Objects.equals(getValue(), that.getValue()); + } + return false; + } + + /** + * Compares this quantity and the specified quantity to the given accuracy. Quantities are considered approximately equals if their absolute + * differences when stated in the same specified unit is less than the specified epsilon. + * + * @param that + * the quantity to compare with. + * @param epsilon + * the absolute error stated in epsilonUnit. + * @param epsilonUnit + * the epsilon unit. + * @returnabs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilon
+ */ + public boolean equals(AbstractQuantitythat, double epsilon, UnitepsilonUnit) { + return Math.abs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilon; + } + + /** + * Returns the hash code for this quantity. + * + * @return the hash code value. + */ + @Override + public int hashCode() { + return Objects.hash(getUnit(), getValue()); + } + + public abstract boolean isBig(); + + /** + * Returns theString
representation of this quantity. The string produced for a given quantity is always the same; it is not affected + * by locale. This means that it can be used as a canonical string representation for exchanging quantity, or as a key for a Hashtable, etc. + * Locale-sensitive quantity formatting and parsing is handled by the {@link MeasurementFormat} class and its subclasses. + * + * @returnUnitFormat.getInternational().format(this)
+ */ + @Override + public String toString() { + return String.valueOf(getValue()) + " " + String.valueOf(getUnit()); + } + + public abstract BigDecimal decimalValue(Unitunit, MathContext ctx) throws ArithmeticException; + + public abstract double doubleValue(Unitunit) throws ArithmeticException; + + public final int intValue(Unitunit) throws ArithmeticException { + long longValue = longValue(unit); + if ((longValue < Integer.MIN_VALUE) || (longValue > Integer.MAX_VALUE)) { + throw new ArithmeticException("Cannot convert " + longValue + " to int (overflow)"); + } + return (int) longValue; + } + + protected long longValue(Unitunit) throws ArithmeticException { + double result = doubleValue(unit); + if ((result < Long.MIN_VALUE) || (result > Long.MAX_VALUE)) { + throw new ArithmeticException("Overflow (" + result + ")"); + } + return (long) result; + } + + protected final float floatValue(Unitunit) { + return (float) doubleValue(unit); + } + + @Override + public, E extends Quantity > ComparableQuantity divide(Quantity that, Class asTypeQuantity) { + + return divide(Objects.requireNonNull(that)).asType(Objects.requireNonNull(asTypeQuantity)); + + } + + @Override + public , E extends Quantity > ComparableQuantity multiply(Quantity that, Class asTypeQuantity) { + return multiply(Objects.requireNonNull(that)).asType(Objects.requireNonNull(asTypeQuantity)); + } + + @Override + public > ComparableQuantity inverse(Class quantityClass) { + return inverse().asType(quantityClass); + } + + /** + * Casts this quantity to a parameterized quantity of specified nature or throw a ClassCastException
if the dimension of the specified + * quantity and its unit's dimension do not match. For example:
** Quantity
- * - * @param type - * the quantity class identifying the nature of the quantity. - * @return this quantity parameterized with the specified type. - * @throws ClassCastException - * if the dimension of this unit is different from the specified quantity dimension. - * @throws UnsupportedOperationException - * if the specified quantity class does not have a public static field named "UNIT" holding the SI unit for the quantity. - * @see Unit#asType(Class) - */ - public finallength = AbstractQuantity.parse("2 km").asType(Length.class); - * > ComparableQuantity asType(Class type) throws ClassCastException { - this.getUnit().asType(type); // Raises ClassCastException if dimension - // mismatches. - return (ComparableQuantity ) this; - } - - /** - * Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless quantities.
+ * + * + * @param type + * the quantity class identifying the nature of the quantity. + * @return this quantity parameterized with the specified type. + * @throws ClassCastException + * if the dimension of this unit is different from the specified quantity dimension. + * @throws UnsupportedOperationException + * if the specified quantity class does not have a public static field named "UNIT" holding the SI unit for the quantity. + * @see Unit#asType(Class) + */ + public final> ComparableQuantity asType(Class type) throws ClassCastException { + this.getUnit().asType(type); // Raises ClassCastException if dimension + // mismatches. + return (ComparableQuantity ) this; + } + + /** + * Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless quantities.
** Quatity
- * - *proportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class); - * - * Note: This method handles only {@link SimpleUnitFormat#getStandard standard} unit format. Locale-sensitive quantity parsing is currently not - * supported. - *
- * - * @param csq - * the decimal value and its unit (if any) separated by space(s). - * @returnQuantityFormat.getInstance().parse(csq)
- */ - public static Quantity> parse(CharSequence csq) { - return QuantityFormat.getInstance().parse(csq); - } - - /** - * Utility class for number comparison and equality - */ - protected static final class Equalizer { - - /** - * Converts a number to {@link BigDecimal} - * - * @param value - * the value to be converted - * @return the value converted - */ - public static BigDecimal toBigDecimal(Number value) { - if (BigDecimal.class.isInstance(value)) { - return BigDecimal.class.cast(value); - } else if (BigInteger.class.isInstance(value)) { - return new BigDecimal(BigInteger.class.cast(value)); - } - return BigDecimal.valueOf(value.doubleValue()); - } - - /** - * Check if the both value has equality number, in other words, 1 is equals to 1.0000 and 1.0. - * - * If the first value is aNumber of eitherDouble ,Float ,Integer ,Long , - *Short orByte it is compared using the respective*value()
method ofNumber . Otherwise it - * is checked, if {@link BigDecimal#compareTo(Object)} is equal to zero. - * - * @param valueA - * the value a - * @param valueB - * the value B - * @return {@link BigDecimal#compareTo(Object)} == zero - */ - public static boolean hasEquality(Number valueA, Number valueB) { - Objects.requireNonNull(valueA); - Objects.requireNonNull(valueB); - - if (valueA instanceof Double && valueB instanceof Double) { - return valueA.doubleValue() == valueB.doubleValue(); - } else if (valueA instanceof Float && valueB instanceof Float) { - return valueA.floatValue() == valueB.floatValue(); - } else if (valueA instanceof Integer && valueB instanceof Integer) { - return valueA.intValue() == valueB.intValue(); - } else if (valueA instanceof Long && valueB instanceof Long) { - return valueA.longValue() == valueB.longValue(); - } else if (valueA instanceof Short && valueB instanceof Short) { - return valueA.shortValue() == valueB.shortValue(); - } else if (valueA instanceof Byte && valueB instanceof Byte) { - return valueA.byteValue() == valueB.byteValue(); - } - return toBigDecimal(valueA).compareTo(toBigDecimal(valueB)) == 0; - } - } -} + * + * + *+ * Note: This method handles only {@link SimpleUnitFormat#getStandard standard} unit format. Locale-sensitive quantity parsing is currently not + * supported. + *
+ * + * @param csq + * the decimal value and its unit (if any) separated by space(s). + * @returnQuantityFormat.getInstance().parse(csq)
+ */ + public static Quantity> parse(CharSequence csq) { + return QuantityFormat.getInstance().parse(csq); + } + + /** + * Utility class for number comparison and equality + */ + protected static final class Equalizer { + + /** + * Converts a number to {@link BigDecimal} + * + * @param value + * the value to be converted + * @return the value converted + */ + public static BigDecimal toBigDecimal(Number value) { + if (BigDecimal.class.isInstance(value)) { + return BigDecimal.class.cast(value); + } else if (BigInteger.class.isInstance(value)) { + return new BigDecimal(BigInteger.class.cast(value)); + } + return BigDecimal.valueOf(value.doubleValue()); + } + + /** + * Check if the both value has equality number, in other words, 1 is equals to 1.0000 and 1.0. + * + * If the first value is aNumber of eitherDouble ,Float ,Integer ,Long , + *Short orByte it is compared using the respective*value()
method ofNumber . Otherwise it + * is checked, if {@link BigDecimal#compareTo(Object)} is equal to zero. + * + * @param valueA + * the value a + * @param valueB + * the value B + * @return {@link BigDecimal#compareTo(Object)} == zero + */ + public static boolean hasEquality(Number valueA, Number valueB) { + Objects.requireNonNull(valueA); + Objects.requireNonNull(valueB); + + if (valueA instanceof Double && valueB instanceof Double) { + return valueA.doubleValue() == valueB.doubleValue(); + } else if (valueA instanceof Float && valueB instanceof Float) { + return valueA.floatValue() == valueB.floatValue(); + } else if (valueA instanceof Integer && valueB instanceof Integer) { + return valueA.intValue() == valueB.intValue(); + } else if (valueA instanceof Long && valueB instanceof Long) { + return valueA.longValue() == valueB.longValue(); + } else if (valueA instanceof Short && valueB instanceof Short) { + return valueA.shortValue() == valueB.shortValue(); + } else if (valueA instanceof Byte && valueB instanceof Byte) { + return valueA.byteValue() == valueB.byteValue(); + } + return toBigDecimal(valueA).compareTo(toBigDecimal(valueB)) == 0; + } + } +} diff --git a/src/main/java/tec/uom/se/AbstractSystemOfUnits.java b/src/main/java/tec/uom/se/AbstractSystemOfUnits.java index 15df4627..65e92b0a 100644 --- a/src/main/java/tec/uom/se/AbstractSystemOfUnits.java +++ b/src/main/java/tec/uom/se/AbstractSystemOfUnits.java @@ -1,121 +1,121 @@ -/* - * Units of Measurement Implementation for Java SE - * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package tec.uom.se; - -import javax.measure.Dimension; -import javax.measure.Quantity; -import javax.measure.Unit; -import javax.measure.spi.SystemOfUnits; - -import tec.uom.lib.common.function.Nameable; -import tec.uom.se.format.SimpleUnitFormat; -import tec.uom.se.format.UnitStyle; - -import static tec.uom.se.format.UnitStyle.*; - -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -/** - *- * An abstract base class for unit systems. - *
- * - * @author Werner Keil - * @version 1.0.2, November 3, 2016 - * @since 1.0 - */ -public abstract class AbstractSystemOfUnits implements SystemOfUnits, Nameable { - /** - * Holds the units. - */ - protected final Set> units = new HashSet<>(); - - /** - * Holds the mapping quantity to unit. - */ - @SuppressWarnings("rawtypes") - protected final Map , Unit> quantityToUnit = new HashMap<>(); - - protected static final Logger logger = Logger.getLogger(AbstractSystemOfUnits.class.getName()); - - /** - * Adds a new named unit to the collection. - * - * @param unit - * the unit being added. - * @param name - * the name of the unit. - * @return unit
. - */ - /* - * @SuppressWarnings("unchecked") private > U addUnit(U - * unit, String name) { if (name != null && unit instanceof AbstractUnit) { - * AbstractUnit> aUnit = (AbstractUnit>) unit; aUnit.setName(name); - * units.add(aUnit); return (U) aUnit; } units.add(unit); return unit; } - */ - /** - * The natural logarithm. - **/ - protected static final double E = 2.71828182845904523536028747135266; - - /* - * (non-Javadoc) - * - * @see SystemOfUnits#getName() - */ - public abstract String getName(); - - // /////////////////// - // Collection View // - // /////////////////// - @Override - public Set> getUnits() { - return Collections.unmodifiableSet(units); - } - +/* + * Units of Measurement Implementation for Java SE + * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package tec.uom.se; + +import javax.measure.Dimension; +import javax.measure.Quantity; +import javax.measure.Unit; +import javax.measure.spi.SystemOfUnits; + +import tec.uom.lib.common.function.Nameable; +import tec.uom.se.format.SimpleUnitFormat; +import tec.uom.se.format.UnitStyle; + +import static tec.uom.se.format.UnitStyle.*; + +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +/** + * + * An abstract base class for unit systems. + *
+ * + * @author Werner Keil + * @version 1.0.2, November 3, 2016 + * @since 1.0 + */ +public abstract class AbstractSystemOfUnits implements SystemOfUnits, Nameable { + /** + * Holds the units. + */ + protected final Set> units = new HashSet<>(); + + /** + * Holds the mapping quantity to unit. + */ + @SuppressWarnings("rawtypes") + protected final Map , Unit> quantityToUnit = new HashMap<>(); + + protected static final Logger logger = Logger.getLogger(AbstractSystemOfUnits.class.getName()); + + /** + * Adds a new named unit to the collection. + * + * @param unit + * the unit being added. + * @param name + * the name of the unit. + * @return unit
. + */ + /* + * @SuppressWarnings("unchecked") private > U addUnit(U + * unit, String name) { if (name != null && unit instanceof AbstractUnit) { + * AbstractUnit> aUnit = (AbstractUnit>) unit; aUnit.setName(name); + * units.add(aUnit); return (U) aUnit; } units.add(unit); return unit; } + */ + /** + * The natural logarithm. + **/ + protected static final double E = 2.71828182845904523536028747135266; + + /* + * (non-Javadoc) + * + * @see SystemOfUnits#getName() + */ + public abstract String getName(); + + // /////////////////// + // Collection View // + // /////////////////// + @Override + public Set> getUnits() { + return Collections.unmodifiableSet(units); + } + @Override public Set extends Unit>> getUnits(Dimension dimension) { return this.getUnits().stream().filter(unit -> dimension.equals(unit.getDimension())) .collect(Collectors.toSet()); } - - @SuppressWarnings("unchecked") - @Override - public > UnitgetUnit(ClassquantityType) { - return quantityToUnit.get(quantityType); - } - - protected static class Helper { + + @SuppressWarnings("unchecked") + @Override + public> UnitgetUnit(ClassquantityType) { + return quantityToUnit.get(quantityType); + } + + protected static class Helper { static Set> getUnitsOfDimension(final Set > units, Dimension dimension) { if (dimension != null) { return units.stream().filter(u -> dimension.equals(u.getDimension())).collect(Collectors.toSet()); @@ -123,158 +123,158 @@ static Set > getUnitsOfDimension(final Set > units, Dimension dime } return null; } - - /** - * Adds a new named unit to the collection. - * - * @param unit - * the unit being added. - * @param name - * the name of the unit. - * @return unit
. - * @since 1.0 - */ - public static > U addUnit(Set> units, U unit, String name) { - return addUnit(units, unit, name, NAME); - } - - /** - * Adds a new named unit to the collection. - * - * @param unit - * the unit being added. - * @param name - * the name of the unit. - * @param name - * the symbol of the unit. - * @return unit
. - * @since 1.0 - */ - @SuppressWarnings("unchecked") - public static > U addUnit(Set> units, U unit, String name, String symbol) { - if (name != null && symbol != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setName(name); - aUnit.setSymbol(symbol); - units.add(aUnit); - return (U) aUnit; - } - if (name != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setName(name); - units.add(aUnit); - return (U) aUnit; - } - units.add(unit); - return unit; - } - - /** - * Adds a new named unit to the collection. - * - * @param unit - * the unit being added. - * @param name - * the name of the unit. - * @param name - * the symbol of the unit. - * @param style - * style of the unit. - * @return unit
. - * @since 1.0.1 - */ - @SuppressWarnings("unchecked") - public static > U addUnit(Set> units, U unit, final String name, final String symbol, UnitStyle style) { - switch (style) { - case NAME: - case SYMBOL: - case SYMBOL_AND_LABEL: - if (name != null && symbol != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setName(name); - if (SYMBOL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { - aUnit.setSymbol(symbol); - } - if (LABEL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { - SimpleUnitFormat.getInstance().label(unit, symbol); - } - units.add(aUnit); - return (U) aUnit; - } - if (name != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setName(name); - units.add(aUnit); - return (U) aUnit; - } - break; - default: - if (logger.isLoggable(Level.FINEST)) { - logger.log(Level.FINEST, "Unknown style " + style + "; unit " + unit + " can't be rendered with '" + symbol + "'."); - } - break; - } - if (LABEL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { - SimpleUnitFormat.getInstance().label(unit, symbol); - } - units.add(unit); - return unit; - } - - /** - * Adds a new labeled unit to the set. - * - * @param units - * the set to add to. - * - * @param unit - * the unit being added. - * @param text - * the text for the unit. - * @param style - * style of the unit. - * @return unit
. - * @since 1.0.1 - */ - @SuppressWarnings("unchecked") - public static > U addUnit(Set> units, U unit, String text, UnitStyle style) { - switch (style) { - case NAME: - if (text != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setName(text); - units.add(aUnit); - return (U) aUnit; - } - break; - case SYMBOL: - if (text != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setSymbol(text); - units.add(aUnit); - return (U) aUnit; - } - break; - case SYMBOL_AND_LABEL: - if (text != null && unit instanceof AbstractUnit) { - AbstractUnit> aUnit = (AbstractUnit>) unit; - aUnit.setSymbol(text); - units.add(aUnit); - SimpleUnitFormat.getInstance().label(aUnit, text); - return (U) aUnit; - } else { // label in any case, returning below - SimpleUnitFormat.getInstance().label(unit, text); - } - break; - case LABEL: - SimpleUnitFormat.getInstance().label(unit, text); - break; - default: - logger.log(Level.FINEST, "Unknown style " + style + "; unit " + unit + " can't be rendered with '" + text + "'."); - break; - } - units.add(unit); - return unit; - } - } -} + + /** + * Adds a new named unit to the collection. + * + * @param unit + * the unit being added. + * @param name + * the name of the unit. + * @return unit
. + * @since 1.0 + */ + public static > U addUnit(Set> units, U unit, String name) { + return addUnit(units, unit, name, NAME); + } + + /** + * Adds a new named unit to the collection. + * + * @param unit + * the unit being added. + * @param name + * the name of the unit. + * @param name + * the symbol of the unit. + * @return unit
. + * @since 1.0 + */ + @SuppressWarnings("unchecked") + public static > U addUnit(Set> units, U unit, String name, String symbol) { + if (name != null && symbol != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setName(name); + aUnit.setSymbol(symbol); + units.add(aUnit); + return (U) aUnit; + } + if (name != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setName(name); + units.add(aUnit); + return (U) aUnit; + } + units.add(unit); + return unit; + } + + /** + * Adds a new named unit to the collection. + * + * @param unit + * the unit being added. + * @param name + * the name of the unit. + * @param name + * the symbol of the unit. + * @param style + * style of the unit. + * @return unit
. + * @since 1.0.1 + */ + @SuppressWarnings("unchecked") + public static > U addUnit(Set> units, U unit, final String name, final String symbol, UnitStyle style) { + switch (style) { + case NAME: + case SYMBOL: + case SYMBOL_AND_LABEL: + if (name != null && symbol != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setName(name); + if (SYMBOL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { + aUnit.setSymbol(symbol); + } + if (LABEL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { + SimpleUnitFormat.getInstance().label(unit, symbol); + } + units.add(aUnit); + return (U) aUnit; + } + if (name != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setName(name); + units.add(aUnit); + return (U) aUnit; + } + break; + default: + if (logger.isLoggable(Level.FINEST)) { + logger.log(Level.FINEST, "Unknown style " + style + "; unit " + unit + " can't be rendered with '" + symbol + "'."); + } + break; + } + if (LABEL.equals(style) || SYMBOL_AND_LABEL.equals(style)) { + SimpleUnitFormat.getInstance().label(unit, symbol); + } + units.add(unit); + return unit; + } + + /** + * Adds a new labeled unit to the set. + * + * @param units + * the set to add to. + * + * @param unit + * the unit being added. + * @param text + * the text for the unit. + * @param style + * style of the unit. + * @return unit
. + * @since 1.0.1 + */ + @SuppressWarnings("unchecked") + public static > U addUnit(Set> units, U unit, String text, UnitStyle style) { + switch (style) { + case NAME: + if (text != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setName(text); + units.add(aUnit); + return (U) aUnit; + } + break; + case SYMBOL: + if (text != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setSymbol(text); + units.add(aUnit); + return (U) aUnit; + } + break; + case SYMBOL_AND_LABEL: + if (text != null && unit instanceof AbstractUnit) { + AbstractUnit> aUnit = (AbstractUnit>) unit; + aUnit.setSymbol(text); + units.add(aUnit); + SimpleUnitFormat.getInstance().label(aUnit, text); + return (U) aUnit; + } else { // label in any case, returning below + SimpleUnitFormat.getInstance().label(unit, text); + } + break; + case LABEL: + SimpleUnitFormat.getInstance().label(unit, text); + break; + default: + logger.log(Level.FINEST, "Unknown style " + style + "; unit " + unit + " can't be rendered with '" + text + "'."); + break; + } + units.add(unit); + return unit; + } + } +} diff --git a/src/main/java/tec/uom/se/AbstractUnit.java b/src/main/java/tec/uom/se/AbstractUnit.java index 66e404c4..2753dd66 100644 --- a/src/main/java/tec/uom/se/AbstractUnit.java +++ b/src/main/java/tec/uom/se/AbstractUnit.java @@ -1,521 +1,521 @@ -/* - * Units of Measurement Implementation for Java SE - * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package tec.uom.se; - -import tec.uom.se.format.SimpleUnitFormat; -import tec.uom.se.function.AddConverter; -import tec.uom.se.function.MultiplyConverter; -import tec.uom.se.function.RationalConverter; -import tec.uom.se.quantity.QuantityDimension; -import tec.uom.se.spi.DimensionalModel; -import tec.uom.se.unit.AlternateUnit; -import tec.uom.se.unit.AnnotatedUnit; -import tec.uom.se.unit.ProductUnit; -import tec.uom.se.unit.TransformedUnit; - -import javax.measure.*; -import javax.measure.quantity.Dimensionless; - -import java.math.BigInteger; -import java.util.HashMap; -import java.util.Map; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -/** - * - * The class represents units founded on the seven SI base units for seven base quantities assumed to be mutually independent. - *
- * - *- * For all physics units, unit conversions are symmetrical:
- * - * @see Wikipedia: International System of Units - * @author Jean-Marie Dautelle - * @author Werner Keil - * @version 1.0.8, August 8, 2017 - * @since 1.0 - */ -public abstract class AbstractUnitu1.getConverterTo(u2).equals(u2.getConverterTo(u1).inverse())
. Non-physical - * units (e.g. currency units) for which conversion is not symmetrical should have their own separate class hierarchy and are considered distinct - * (e.g. financial units), although they can always be combined with physics units (e.g. "€/Kg", "$/h"). - *> implements ComparableUnit{ - +/* + * Units of Measurement Implementation for Java SE + * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package tec.uom.se; + +import tec.uom.se.format.SimpleUnitFormat; +import tec.uom.se.function.AddConverter; +import tec.uom.se.function.MultiplyConverter; +import tec.uom.se.function.RationalConverter; +import tec.uom.se.quantity.QuantityDimension; +import tec.uom.se.spi.DimensionalModel; +import tec.uom.se.unit.AlternateUnit; +import tec.uom.se.unit.AnnotatedUnit; +import tec.uom.se.unit.ProductUnit; +import tec.uom.se.unit.TransformedUnit; + +import javax.measure.*; +import javax.measure.quantity.Dimensionless; + +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +/** + *+ * The class represents units founded on the seven SI base units for seven base quantities assumed to be mutually independent. + *
+ * + *+ * For all physics units, unit conversions are symmetrical:
+ * + * @see Wikipedia: International System of Units + * @author Jean-Marie Dautelle + * @author Werner Keil + * @version 1.0.8, August 8, 2017 + * @since 1.0 + */ +public abstract class AbstractUnitu1.getConverterTo(u2).equals(u2.getConverterTo(u1).inverse())
. Non-physical + * units (e.g. currency units) for which conversion is not symmetrical should have their own separate class hierarchy and are considered distinct + * (e.g. financial units), although they can always be combined with physics units (e.g. "€/Kg", "$/h"). + *> implements ComparableUnit{ + /** * - */ - private static final long serialVersionUID = -4344589505537030204L; - - /** - * Holds the dimensionless unitONE
. - * - * @see Wikipedia: Natural Units - Choosing constants to - * normalize - * @see Units of Dimension One - */ - public static final UnitONE = new ProductUnit<>(); - - /** - * Holds the name. - */ - protected String name; - - /** - * Holds the symbol. - */ - private String symbol; - - /** - * Holds the unique symbols collection (base units or alternate units). - */ - protected static final Map > SYMBOL_TO_UNIT = new HashMap<>(); - - /** - * DefaultQuantityFactory constructor. - */ - protected AbstractUnit() { - } - - protected Type getActualType() { - ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); - return parameterizedType.getActualTypeArguments()[0].getClass().getGenericInterfaces()[0]; - } - - /** - * Indicates if this unit belongs to the set of coherent SI units (unscaled SI units). - * - * The base and coherent derived units of the SI form a coherent set, designated the set of coherent SI units. The word coherent is used here in the - * following sense: when coherent units are used, equations between the numerical values of quantities take exactly the same form as the equations - * between the quantities themselves. Thus if only units from a coherent set are used, conversion factors between units are never required. - * - * @return equals(toSystemUnit())
- */ - public boolean isSystemUnit() { - Unitsi = this.toSystemUnit(); - return (this == si) || this.equals(si); - } - - /** - * Returns the unscaled {@link SI} unit from which this unit is derived. - * + */ + private static final long serialVersionUID = -4344589505537030204L; + + /** + * Holds the dimensionless unitONE
. + * + * @see Wikipedia: Natural Units - Choosing constants to + * normalize + * @see Units of Dimension One + */ + public static final UnitONE = new ProductUnit<>(); + + /** + * Holds the name. + */ + protected String name; + + /** + * Holds the symbol. + */ + private String symbol; + + /** + * Holds the unique symbols collection (base units or alternate units). + */ + protected static final Map > SYMBOL_TO_UNIT = new HashMap<>(); + + /** + * DefaultQuantityFactory constructor. + */ + protected AbstractUnit() { + } + + protected Type getActualType() { + ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); + return parameterizedType.getActualTypeArguments()[0].getClass().getGenericInterfaces()[0]; + } + + /** + * Indicates if this unit belongs to the set of coherent SI units (unscaled SI units). + * + * The base and coherent derived units of the SI form a coherent set, designated the set of coherent SI units. The word coherent is used here in the + * following sense: when coherent units are used, equations between the numerical values of quantities take exactly the same form as the equations + * between the quantities themselves. Thus if only units from a coherent set are used, conversion factors between units are never required. + * + * @return equals(toSystemUnit())
+ */ + public boolean isSystemUnit() { + Unitsi = this.toSystemUnit(); + return (this == si) || this.equals(si); + } + + /** + * Returns the unscaled {@link SI} unit from which this unit is derived. + * * The SI unit can be be used to identify a quantity given the unit. For example:static boolean isAngularVelocity(AbstractUnit> unit) { - * return unit.toSystemUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true.
- * - * @return the unscaled metric unit from which this unit is derived. - */ - protected abstract UnittoSystemUnit(); - - /** - * Returns the converter from this unit to its unscaled {@link #toSysemUnit System Unit} unit. - * - * @returngetConverterTo(this.toSystemUnit())
- * @see #toSI - */ - public abstract UnitConverter getSystemConverter(); - - /** - * Annotates the specified unit. Annotation does not change the unit semantic. Annotations are often written between curly braces behind units. For - * example: + * return unit.toSystemUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true. + * + * @return the unscaled metric unit from which this unit is derived. + */ + protected abstract UnittoSystemUnit(); + + /** + * Returns the converter from this unit to its unscaled {@link #toSysemUnit System Unit} unit. + * + * @returngetConverterTo(this.toSystemUnit())
+ * @see #toSI + */ + public abstract UnitConverter getSystemConverter(); + + /** + * Annotates the specified unit. Annotation does not change the unit semantic. Annotations are often written between curly braces behind units. For + * example: *AbstractUnit
- * - * Note: Annotation of system units are not considered themselves as system units. - * - * @param annotation - * the unit annotation. - * @return the annotated unit. - */ - public AnnotatedUnitPERCENT_VOL = Units.PERCENT.annotate("vol"); // "%{vol}" AbstractUnit KG_TOTAL = - * Units.KILOGRAM.annotate("total"); // "kg{total}" AbstractUnit RED_BLOOD_CELLS = Units.ONE.annotate("RBC"); // "{RBC}" annotate(String annotation) { - return new AnnotatedUnit<>(this, annotation); - } - - /** - * Returns the abstract unit represented by the specified characters as per default format. - * - * Locale-sensitive unit parsing could be handled using {@link LocalUnitFormat} in subclasses of AbstractUnit. - * - *+ * Units.KILOGRAM.annotate("total"); // "kg{total}" AbstractUnit
+ * + * @param charSequence + * the character sequence to parse. + * @returnRED_BLOOD_CELLS = Units.ONE.annotate("RBC"); // "{RBC}" + * + * Note: Annotation of system units are not considered themselves as system units. + * + * @param annotation + * the unit annotation. + * @return the annotated unit. + */ + public AnnotatedUnit annotate(String annotation) { + return new AnnotatedUnit<>(this, annotation); + } + + /** + * Returns the abstract unit represented by the specified characters as per default format. + * + * Locale-sensitive unit parsing could be handled using {@link LocalUnitFormat} in subclasses of AbstractUnit. + * + ** Note: The standard format supports dimensionless units.
- * - * @param charSequence - * the character sequence to parse. - * @returnAbstractUnit
- *PERCENT = - * AbstractUnit.parse("100").inverse().asType(Dimensionless.class); SimpleUnitFormat.getInstance().parse(csq, new ParsePosition(0))
- * @throws ParserException - * if the specified character sequence cannot be correctly parsed (e.g. not UCUM compliant). - */ - public static Unit> parse(CharSequence charSequence) { - return SimpleUnitFormat.getInstance().parse(charSequence); - } - - /** - * Returns the standard representation of this physics unit. The string produced for a given unit is always the same; it is not affected by the - * locale. It can be used as a canonical string representation for exchanging units, or as a key for a Hashtable, etc. - * - * Locale-sensitive unit parsing could be handled using {@link LocalUnitFormat} in subclasses of AbstractUnit. - * - * @returnSimpleUnitFormat.getInstance().format(this)
- */ - @Override - public String toString() { - return SimpleUnitFormat.getInstance().format(this); - } - - // /////////////////////////////////////////////////////// - // Implements org.unitsofmeasurement.Unitinterface // - // /////////////////////////////////////////////////////// - - /** - * Returns the system unit (unscaled SI unit) from which this unit is derived. They can be be used to identify a quantity given the unit. For - * example:
+ * AbstractUnit.parse("100").inverse().asType(Dimensionless.class); + *SimpleUnitFormat.getInstance().parse(csq, new ParsePosition(0))
+ * @throws ParserException + * if the specified character sequence cannot be correctly parsed (e.g. not UCUM compliant). + */ + public static Unit> parse(CharSequence charSequence) { + return SimpleUnitFormat.getInstance().parse(charSequence); + } + + /** + * Returns the standard representation of this physics unit. The string produced for a given unit is always the same; it is not affected by the + * locale. It can be used as a canonical string representation for exchanging units, or as a key for a Hashtable, etc. + * + * Locale-sensitive unit parsing could be handled using {@link LocalUnitFormat} in subclasses of AbstractUnit. + * + * @returnSimpleUnitFormat.getInstance().format(this)
+ */ + @Override + public String toString() { + return SimpleUnitFormat.getInstance().format(this); + } + + // /////////////////////////////////////////////////////// + // Implements org.unitsofmeasurement.Unitinterface // + // /////////////////////////////////////////////////////// + + /** + * Returns the system unit (unscaled SI unit) from which this unit is derived. They can be be used to identify a quantity given the unit. For + * example:
*static boolean isAngularVelocity(AbstractUnit> unit) {
- * - * @return the unscaled metric unit from which this unit is derived. - */ - @Override - public final Unit
return unit.getSystemUnit().equals(RADIAN.divide(SECOND));
} - *
assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true.getSystemUnit() { - return toSystemUnit(); - } - - /** - * Indicates if this unit is compatible with the unit specified. To be compatible both units must be physics units having the same fundamental - * dimension. - * - * @param that - * the other unit. - * @returntrue
if this unit and that unit have equals fundamental dimension according to the current physics model;false
- * otherwise. - */ - @Override - public final boolean isCompatible(Unit> that) { - if ((this == that) || this.equals(that)) - return true; - if (!(that instanceof AbstractUnit)) - return false; - Dimension thisDimension = this.getDimension(); - Dimension thatDimension = that.getDimension(); - if (thisDimension.equals(thatDimension)) - return true; - DimensionalModel model = DimensionalModel.current(); // Use - // dimensional - // analysis - // model. - return model.getFundamentalDimension(thisDimension).equals(model.getFundamentalDimension(thatDimension)); - } - - public boolean isEquivalentOf(Unitthat) { - if (this.compareTo(that) == 0) - return true; - return this.getConverterTo(that).equals(that.getConverterTo(this)); - } - - /** - * Casts this unit to a parameterized unit of specified nature or throw a ClassCastException if the dimension of the specified quantity and this - * unit's dimension do not match (regardless whether or not the dimensions are independent or not). - * - * @param type - * the quantity class identifying the nature of the unit. - * @throws ClassCastException - * if the dimension of this unit is different from the SI dimension of the specified type. - * @see Units#getUnit(Class) - */ - @SuppressWarnings("unchecked") - @Override - public final> AbstractUnit asType(Class type) { - Dimension typeDimension = QuantityDimension.of(type); - if ((typeDimension != null) && (!typeDimension.equals(this.getDimension()))) - throw new ClassCastException("The unit: " + this + " is not compatible with quantities of type " + type); - return (AbstractUnit ) this; - } - - @Override - public abstract Map extends Unit>, Integer> getBaseUnits(); - - @Override - public abstract Dimension getDimension(); - - protected void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public String getSymbol() { - return symbol; - } - - protected void setSymbol(String s) { - this.symbol = s; - } - - @Override - public final UnitConverter getConverterTo(Unit that) throws UnconvertibleException { - if ((this == that) || this.equals(that)) - return AbstractConverter.IDENTITY; // Shortcut. - UnitthisSystemUnit = this.getSystemUnit(); - UnitthatSystemUnit = that.getSystemUnit(); - if (!thisSystemUnit.equals(thatSystemUnit)) - try { - return getConverterToAny(that); - } catch (IncommensurableException e) { - throw new UnconvertibleException(e); - } - UnitConverter thisToSI = this.getSystemConverter(); - UnitConverter thatToSI = that.getConverterTo(thatSystemUnit); - return thatToSI.inverse().concatenate(thisToSI); - } - - @SuppressWarnings("rawtypes") - @Override - public final UnitConverter getConverterToAny(Unit> that) throws IncommensurableException, UnconvertibleException { - if (!isCompatible(that)) - throw new IncommensurableException(this + " is not compatible with " + that); - AbstractUnit thatAbstr = (AbstractUnit) that; // Since both units are - // compatible they must - // be both physics - // units. - DimensionalModel model = DimensionalModel.current(); - Unit thisSystemUnit = this.getSystemUnit(); - UnitConverter thisToDimension = model.getDimensionalTransform(thisSystemUnit.getDimension()).concatenate(this.getSystemConverter()); - Unit thatSystemUnit = thatAbstr.getSystemUnit(); - UnitConverter thatToDimension = model.getDimensionalTransform(thatSystemUnit.getDimension()).concatenate(thatAbstr.getSystemConverter()); - return thatToDimension.inverse().concatenate(thisToDimension); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public final Unitalternate(String symbol) { - return new AlternateUnit(this, symbol); - } - - @Override - public final Unittransform(UnitConverter operation) { - UnitsystemUnit = this.getSystemUnit(); - UnitConverter cvtr; - if (this.isSystemUnit()) { - cvtr = this.getSystemConverter().concatenate(operation); - } else { - cvtr = operation; - } - if (cvtr.equals(AbstractConverter.IDENTITY)) { - return systemUnit; - } else { - return new TransformedUnit<>(null, this, systemUnit, cvtr); - } - } - - @Override - public final Unitshift(double offset) { - if (offset == 0) - return this; - return transform(new AddConverter(offset)); - } - - @Override - public final Unitmultiply(double factor) { - if (factor == 1) - return this; - if (isLongValue(factor)) - return transform(new RationalConverter(BigInteger.valueOf((long) factor), BigInteger.ONE)); - return transform(new MultiplyConverter(factor)); - } - - private static boolean isLongValue(double value) { - return !((value < Long.MIN_VALUE) || (value > Long.MAX_VALUE)) && Math.floor(value) == value; - } - - /** - * Returns the product of this unit with the one specified. - * - *- * Note: If the specified unit (that) is not a physical unit, then
- * - * @param that - * the unit multiplicand. - * @returnthat.multiply(this)
is returned. - *this * that
- */ - @Override - public final Unit> multiply(Unit> that) { - if (that instanceof AbstractUnit) - return multiply((AbstractUnit>) that); - // return that.multiply(this); // Commutatif. - return ProductUnit.getProductInstance(this, that); - } - - /** - * Returns the product of this physical unit with the one specified. - * - * @param that - * the physical unit multiplicand. - * @returnthis * that
- */ - protected final Unit> multiply(AbstractUnit> that) { - if (this.equals(ONE)) - return that; - if (that.equals(ONE)) - return this; - return ProductUnit.getProductInstance(this, that); - } - - /** - * Returns the inverse of this physical unit. - * - * @return1 / this
- */ - @Override - public final Unit> inverse() { - if (this.equals(ONE)) - return this; - return ProductUnit.getQuotientInstance(ONE, this); - } - - /** - * Returns the result of dividing this unit by the specifified divisor. If the factor is an integer value, the division is exact. For example: - * - *+ *+ * + * @param divisor + * the divisor value. + * @return this unit divided by the specified divisor. + */ + @Override + public final Unit
assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true. + * + * @return the unscaled metric unit from which this unit is derived. + */ + @Override + public final UnitgetSystemUnit() { + return toSystemUnit(); + } + + /** + * Indicates if this unit is compatible with the unit specified. To be compatible both units must be physics units having the same fundamental + * dimension. + * + * @param that + * the other unit. + * @returntrue
if this unit and that unit have equals fundamental dimension according to the current physics model;false
+ * otherwise. + */ + @Override + public final boolean isCompatible(Unit> that) { + if ((this == that) || this.equals(that)) + return true; + if (!(that instanceof AbstractUnit)) + return false; + Dimension thisDimension = this.getDimension(); + Dimension thatDimension = that.getDimension(); + if (thisDimension.equals(thatDimension)) + return true; + DimensionalModel model = DimensionalModel.current(); // Use + // dimensional + // analysis + // model. + return model.getFundamentalDimension(thisDimension).equals(model.getFundamentalDimension(thatDimension)); + } + + public boolean isEquivalentOf(Unitthat) { + if (this.compareTo(that) == 0) + return true; + return this.getConverterTo(that).equals(that.getConverterTo(this)); + } + + /** + * Casts this unit to a parameterized unit of specified nature or throw a ClassCastException if the dimension of the specified quantity and this + * unit's dimension do not match (regardless whether or not the dimensions are independent or not). + * + * @param type + * the quantity class identifying the nature of the unit. + * @throws ClassCastException + * if the dimension of this unit is different from the SI dimension of the specified type. + * @see Units#getUnit(Class) + */ + @SuppressWarnings("unchecked") + @Override + public final> AbstractUnit asType(Class type) { + Dimension typeDimension = QuantityDimension.of(type); + if ((typeDimension != null) && (!typeDimension.equals(this.getDimension()))) + throw new ClassCastException("The unit: " + this + " is not compatible with quantities of type " + type); + return (AbstractUnit ) this; + } + + @Override + public abstract Map extends Unit>, Integer> getBaseUnits(); + + @Override + public abstract Dimension getDimension(); + + protected void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public String getSymbol() { + return symbol; + } + + protected void setSymbol(String s) { + this.symbol = s; + } + + @Override + public final UnitConverter getConverterTo(Unit that) throws UnconvertibleException { + if ((this == that) || this.equals(that)) + return AbstractConverter.IDENTITY; // Shortcut. + UnitthisSystemUnit = this.getSystemUnit(); + UnitthatSystemUnit = that.getSystemUnit(); + if (!thisSystemUnit.equals(thatSystemUnit)) + try { + return getConverterToAny(that); + } catch (IncommensurableException e) { + throw new UnconvertibleException(e); + } + UnitConverter thisToSI = this.getSystemConverter(); + UnitConverter thatToSI = that.getConverterTo(thatSystemUnit); + return thatToSI.inverse().concatenate(thisToSI); + } + + @SuppressWarnings("rawtypes") + @Override + public final UnitConverter getConverterToAny(Unit> that) throws IncommensurableException, UnconvertibleException { + if (!isCompatible(that)) + throw new IncommensurableException(this + " is not compatible with " + that); + AbstractUnit thatAbstr = (AbstractUnit) that; // Since both units are + // compatible they must + // be both physics + // units. + DimensionalModel model = DimensionalModel.current(); + Unit thisSystemUnit = this.getSystemUnit(); + UnitConverter thisToDimension = model.getDimensionalTransform(thisSystemUnit.getDimension()).concatenate(this.getSystemConverter()); + Unit thatSystemUnit = thatAbstr.getSystemUnit(); + UnitConverter thatToDimension = model.getDimensionalTransform(thatSystemUnit.getDimension()).concatenate(thatAbstr.getSystemConverter()); + return thatToDimension.inverse().concatenate(thisToDimension); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public final Unitalternate(String symbol) { + return new AlternateUnit(this, symbol); + } + + @Override + public final Unittransform(UnitConverter operation) { + UnitsystemUnit = this.getSystemUnit(); + UnitConverter cvtr; + if (this.isSystemUnit()) { + cvtr = this.getSystemConverter().concatenate(operation); + } else { + cvtr = operation; + } + if (cvtr.equals(AbstractConverter.IDENTITY)) { + return systemUnit; + } else { + return new TransformedUnit<>(null, this, systemUnit, cvtr); + } + } + + @Override + public final Unitshift(double offset) { + if (offset == 0) + return this; + return transform(new AddConverter(offset)); + } + + @Override + public final Unitmultiply(double factor) { + if (factor == 1) + return this; + if (isLongValue(factor)) + return transform(new RationalConverter(BigInteger.valueOf((long) factor), BigInteger.ONE)); + return transform(new MultiplyConverter(factor)); + } + + private static boolean isLongValue(double value) { + return !((value < Long.MIN_VALUE) || (value > Long.MAX_VALUE)) && Math.floor(value) == value; + } + + /** + * Returns the product of this unit with the one specified. + * + *+ * Note: If the specified unit (that) is not a physical unit, then
+ * + * @param that + * the unit multiplicand. + * @returnthat.multiply(this)
is returned. + *this * that
+ */ + @Override + public final Unit> multiply(Unit> that) { + if (that instanceof AbstractUnit) + return multiply((AbstractUnit>) that); + // return that.multiply(this); // Commutatif. + return ProductUnit.getProductInstance(this, that); + } + + /** + * Returns the product of this physical unit with the one specified. + * + * @param that + * the physical unit multiplicand. + * @returnthis * that
+ */ + protected final Unit> multiply(AbstractUnit> that) { + if (this.equals(ONE)) + return that; + if (that.equals(ONE)) + return this; + return ProductUnit.getProductInstance(this, that); + } + + /** + * Returns the inverse of this physical unit. + * + * @return1 / this
+ */ + @Override + public final Unit> inverse() { + if (this.equals(ONE)) + return this; + return ProductUnit.getQuotientInstance(ONE, this); + } + + /** + * Returns the result of dividing this unit by the specifified divisor. If the factor is an integer value, the division is exact. For example: + * + **- * - * @param divisor - * the divisor value. - * @return this unit divided by the specified divisor. - */ - @Override - public final Unit* QUART = GALLON_LIQUID_US.divide(4); // Exact definition. - *
- *divide(double divisor) { - if (divisor == 1) - return this; - if (isLongValue(divisor)) - return transform(new RationalConverter(BigInteger.ONE, BigInteger.valueOf((long) divisor))); - return transform(new MultiplyConverter(1.0 / divisor)); - } - - /** - * Returns the quotient of this unit with the one specified. - * - * @param that - * the unit divisor. - * @returnthis.multiply(that.inverse())
- */ - @Override - public final Unit> divide(Unit> that) { - return this.multiply(that.inverse()); - } - - /** - * Returns the quotient of this physical unit with the one specified. - * - * @param that - * the physical unit divisor. - * @returnthis.multiply(that.inverse())
- */ - protected final Unit> divide(AbstractUnit> that) { - return this.multiply(that.inverse()); - } - - /** - * Returns a unit equals to the given root of this unit. - * - * @param n - * the root's order. - * @return the result of taking the given root of this unit. - * @throws ArithmeticException - * ifn == 0
or if this operation would result in an unit with a fractional exponent. - */ - @Override - public final Unit> root(int n) { - if (n > 0) - return ProductUnit.getRootInstance(this, n); - else if (n == 0) - throw new ArithmeticException("Root's order of zero"); - else - // n < 0 - return ONE.divide(this.root(-n)); - } - - /** - * Returns a unit equals to this unit raised to an exponent. - * - * @param n - * the exponent. - * @return the result of raising this unit to the exponent. - */ - @Override - public final Unit> pow(int n) { - if (n > 0) - return this.multiply(this.pow(n - 1)); - else if (n == 0) - return ONE; - else - // n < 0 - return ONE.divide(this.pow(-n)); - } - - /** - * Compares this unit to the specified unit. The default implementation compares the name and symbol of both this unit and the specified unit. - * - * @return a negative integer, zero, or a positive integer as this unit is less than, equal to, or greater than the specified unit. - */ - public int compareTo(Unitthat) { - if (name != null && getSymbol() != null) { - return name.compareTo(that.getName()) + getSymbol().compareTo(that.getSymbol()); - } else if (name == null) { - if (getSymbol() != null && that.getSymbol() != null) { - return getSymbol().compareTo(that.getSymbol()); - } else { - return -1; - } - } else if (getSymbol() == null) { - if (name != null) { - return name.compareTo(that.getName()); - } else { - return -1; - } - } else { - return -1; - } - } - - // ////////////////////////////////////////////////////////////// - // Ensures that sub-classes implements hashCode/equals method. - // ////////////////////////////////////////////////////////////// - - @Override - public abstract int hashCode(); - - @Override - public abstract boolean equals(Object that); -} + * + *divide(double divisor) { + if (divisor == 1) + return this; + if (isLongValue(divisor)) + return transform(new RationalConverter(BigInteger.ONE, BigInteger.valueOf((long) divisor))); + return transform(new MultiplyConverter(1.0 / divisor)); + } + + /** + * Returns the quotient of this unit with the one specified. + * + * @param that + * the unit divisor. + * @returnthis.multiply(that.inverse())
+ */ + @Override + public final Unit> divide(Unit> that) { + return this.multiply(that.inverse()); + } + + /** + * Returns the quotient of this physical unit with the one specified. + * + * @param that + * the physical unit divisor. + * @returnthis.multiply(that.inverse())
+ */ + protected final Unit> divide(AbstractUnit> that) { + return this.multiply(that.inverse()); + } + + /** + * Returns a unit equals to the given root of this unit. + * + * @param n + * the root's order. + * @return the result of taking the given root of this unit. + * @throws ArithmeticException + * ifn == 0
or if this operation would result in an unit with a fractional exponent. + */ + @Override + public final Unit> root(int n) { + if (n > 0) + return ProductUnit.getRootInstance(this, n); + else if (n == 0) + throw new ArithmeticException("Root's order of zero"); + else + // n < 0 + return ONE.divide(this.root(-n)); + } + + /** + * Returns a unit equals to this unit raised to an exponent. + * + * @param n + * the exponent. + * @return the result of raising this unit to the exponent. + */ + @Override + public final Unit> pow(int n) { + if (n > 0) + return this.multiply(this.pow(n - 1)); + else if (n == 0) + return ONE; + else + // n < 0 + return ONE.divide(this.pow(-n)); + } + + /** + * Compares this unit to the specified unit. The default implementation compares the name and symbol of both this unit and the specified unit. + * + * @return a negative integer, zero, or a positive integer as this unit is less than, equal to, or greater than the specified unit. + */ + public int compareTo(Unitthat) { + if (name != null && getSymbol() != null) { + return name.compareTo(that.getName()) + getSymbol().compareTo(that.getSymbol()); + } else if (name == null) { + if (getSymbol() != null && that.getSymbol() != null) { + return getSymbol().compareTo(that.getSymbol()); + } else { + return -1; + } + } else if (getSymbol() == null) { + if (name != null) { + return name.compareTo(that.getName()); + } else { + return -1; + } + } else { + return -1; + } + } + + // ////////////////////////////////////////////////////////////// + // Ensures that sub-classes implements hashCode/equals method. + // ////////////////////////////////////////////////////////////// + + @Override + public abstract int hashCode(); + + @Override + public abstract boolean equals(Object that); +} diff --git a/src/main/java/tec/uom/se/ComparableQuantity.java b/src/main/java/tec/uom/se/ComparableQuantity.java index fc46a43c..d310f039 100644 --- a/src/main/java/tec/uom/se/ComparableQuantity.java +++ b/src/main/java/tec/uom/se/ComparableQuantity.java @@ -1,192 +1,192 @@ -/* - * Units of Measurement Implementation for Java SE - * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package tec.uom.se; - -import java.io.Serializable; - -import javax.measure.Quantity; -import javax.measure.Unit; - -/** - * Quantity specialized for the Java SE platform. It extends {@link Quantity} with {@linkplain Comparable} and {@linkplain Serializable } - * - * @see {@link Quantity} - * @author otaviojava - * @author werner - * @param- * @since 1.0 - */ -public interface ComparableQuantity> extends Quantity, Comparable>, Serializable { - - /** - * @see Quantity#add(Quantity) - */ - ComparableQuantity add(Quantitythat); - - /** - * @see Quantity#subtract(Quantity) - */ - ComparableQuantitysubtract(Quantitythat); - - /** - * @see Quantity#divide(Quantity) - */ - ComparableQuantity> divide(Quantity> that); - - /** - * @see Quantity#divide(Number) - */ - ComparableQuantitydivide(Number that); - - /** - * @see Quantity#multiply(Quantity) - */ - ComparableQuantity> multiply(Quantity> multiplier); - - /** - * @see Quantity#multiply(Number) - */ - ComparableQuantitymultiply(Number multiplier); - - /** - * @see Quantity#inverse() - */ - ComparableQuantity> inverse(); - - /** - * invert and already cast to defined quantityClass - * - * @param quantityClass - * Quantity to be converted - * @see Quantity#inverse() - * @see Quantity#asType(Class) - */ -> ComparableQuantity inverse(Class quantityClass); - - /** - * @see Quantity#to(Unit) - */ - ComparableQuantity to(Unitunit); - - /** - * @see Quantity#asType(Class) - */ -> ComparableQuantity asType(Class type) throws ClassCastException; - - /** - * Compares two instances of {@link Quantity }. Conversion of unit can happen if necessary - * - * @param that - * the {@code quantity} to be compared with this instance. - * @return {@code true} if {@code that > this}. - * @throws NullPointerException - * if the that is null - */ - boolean isGreaterThan(Quantitythat); - - /** - * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. - * - * @param that - * the {@code quantity} to be compared with this instance. - * @return {@code true} if {@code that >= this}. - * @throws NullPointerException - * if the that is null - */ - boolean isGreaterThanOrEqualTo(Quantitythat); - - /** - * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. - * - * @param that - * the {@code quantity} to be compared with this instance. - * @return {@code true} if {@code that < this}. - * @throws NullPointerException - * if the quantity is null - */ - boolean isLessThan(Quantitythat); - - /** - * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. - * - * @param that - * the {@code quantity} to be compared with this instance. - * @return {@code true} if {@code that < this}. - * @throws NullPointerException - * if the quantity is null - */ - boolean isLessThanOrEqualTo(Quantitythat); - - /** - * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. - * - * @param that - * the {@code quantity} to be compared with this instance. - * @return {@code true} if {@code that < this}. - * @throws NullPointerException - * if the quantity is null - */ - boolean isEquivalentOf(Quantitythat); - - /** - * @deprecated use #isEquivalentOf - */ - boolean isEquivalentTo(Quantitythat); - - /** - * Multiply and cast the {@link ComparableQuantity} - * - * @param that - * quantity to be multiplied - * @param asTypeQuantity - * quantity to be converted - * @return the QuantityOperations multiplied and converted - * @see Quantity#divide(Quantity) - * @see Quantity#asType(Class) - * @exception NullPointerException - */ -, E extends Quantity > ComparableQuantity divide(Quantity that, Class asTypeQuantity); - - /** - * Divide and cast the {@link ComparableQuantity} - * - * @param that - * quantity to be divided - * @param asTypeQuantity - * quantity to be converted - * @return the QuantityOperations multiplied and converted - * @see QuantityOperations - * @see QuantityOperations#of(Quantity, Class) - * @see Quantity#asType(Class) - * @see Quantity#multiply(Quantity) - * @exception NullPointerException - */ - , E extends Quantity > ComparableQuantity multiply(Quantity that, Class asTypeQuantity); -} +/* + * Units of Measurement Implementation for Java SE + * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package tec.uom.se; + +import java.io.Serializable; + +import javax.measure.Quantity; +import javax.measure.Unit; + +/** + * Quantity specialized for the Java SE platform. It extends {@link Quantity} with {@linkplain Comparable} and {@linkplain Serializable } + * + * @see {@link Quantity} + * @author otaviojava + * @author werner + * @param + * @since 1.0 + */ +public interface ComparableQuantity> extends Quantity, Comparable>, Serializable { + + /** + * @see Quantity#add(Quantity) + */ + ComparableQuantity add(Quantitythat); + + /** + * @see Quantity#subtract(Quantity) + */ + ComparableQuantitysubtract(Quantitythat); + + /** + * @see Quantity#divide(Quantity) + */ + ComparableQuantity> divide(Quantity> that); + + /** + * @see Quantity#divide(Number) + */ + ComparableQuantitydivide(Number that); + + /** + * @see Quantity#multiply(Quantity) + */ + ComparableQuantity> multiply(Quantity> multiplier); + + /** + * @see Quantity#multiply(Number) + */ + ComparableQuantitymultiply(Number multiplier); + + /** + * @see Quantity#inverse() + */ + ComparableQuantity> inverse(); + + /** + * invert and already cast to defined quantityClass + * + * @param quantityClass + * Quantity to be converted + * @see Quantity#inverse() + * @see Quantity#asType(Class) + */ +> ComparableQuantity inverse(Class quantityClass); + + /** + * @see Quantity#to(Unit) + */ + ComparableQuantity to(Unitunit); + + /** + * @see Quantity#asType(Class) + */ +> ComparableQuantity asType(Class type) throws ClassCastException; + + /** + * Compares two instances of {@link Quantity }. Conversion of unit can happen if necessary + * + * @param that + * the {@code quantity} to be compared with this instance. + * @return {@code true} if {@code that > this}. + * @throws NullPointerException + * if the that is null + */ + boolean isGreaterThan(Quantitythat); + + /** + * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. + * + * @param that + * the {@code quantity} to be compared with this instance. + * @return {@code true} if {@code that >= this}. + * @throws NullPointerException + * if the that is null + */ + boolean isGreaterThanOrEqualTo(Quantitythat); + + /** + * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. + * + * @param that + * the {@code quantity} to be compared with this instance. + * @return {@code true} if {@code that < this}. + * @throws NullPointerException + * if the quantity is null + */ + boolean isLessThan(Quantitythat); + + /** + * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. + * + * @param that + * the {@code quantity} to be compared with this instance. + * @return {@code true} if {@code that < this}. + * @throws NullPointerException + * if the quantity is null + */ + boolean isLessThanOrEqualTo(Quantitythat); + + /** + * Compares two instances of {@link Quantity}, doing the conversion of unit if necessary. + * + * @param that + * the {@code quantity} to be compared with this instance. + * @return {@code true} if {@code that < this}. + * @throws NullPointerException + * if the quantity is null + */ + boolean isEquivalentOf(Quantitythat); + + /** + * @deprecated use #isEquivalentOf + */ + boolean isEquivalentTo(Quantitythat); + + /** + * Multiply and cast the {@link ComparableQuantity} + * + * @param that + * quantity to be multiplied + * @param asTypeQuantity + * quantity to be converted + * @return the QuantityOperations multiplied and converted + * @see Quantity#divide(Quantity) + * @see Quantity#asType(Class) + * @exception NullPointerException + */ +, E extends Quantity > ComparableQuantity divide(Quantity that, Class asTypeQuantity); + + /** + * Divide and cast the {@link ComparableQuantity} + * + * @param that + * quantity to be divided + * @param asTypeQuantity + * quantity to be converted + * @return the QuantityOperations multiplied and converted + * @see QuantityOperations + * @see QuantityOperations#of(Quantity, Class) + * @see Quantity#asType(Class) + * @see Quantity#multiply(Quantity) + * @exception NullPointerException + */ + , E extends Quantity > ComparableQuantity multiply(Quantity that, Class asTypeQuantity); +} diff --git a/src/main/java/tec/uom/se/ComparableUnit.java b/src/main/java/tec/uom/se/ComparableUnit.java index b05b6992..e647560e 100644 --- a/src/main/java/tec/uom/se/ComparableUnit.java +++ b/src/main/java/tec/uom/se/ComparableUnit.java @@ -1,68 +1,68 @@ -/* - * Units of Measurement Implementation for Java SE - * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package tec.uom.se; - -import java.io.Serializable; - -import javax.measure.Quantity; -import javax.measure.Unit; - -/** - * Unit specialized for the Java SE platform. It extends {@link Unit} with {@linkplain Comparable} and {@linkplain Serializable } - * - * @see {@link Unit} - * @author werner - * @param - * @since 1.0.9 - */ -public interface ComparableUnit> extends Unit, Comparable>, Serializable { - - /** - * Compares two instances of {@link Unit }, doing the conversion of unit if necessary. - * - * @param that - * the {@code Unit} to be compared with this instance. - * @return {@code true} if {@code that < this}. - * @throws NullPointerException - * if the unit is null - */ - boolean isEquivalentOf(Unitthat); - - /** - * Indicates if this unit belongs to the set of coherent SI units (unscaled SI units). - * - * The base and coherent derived units of the SI form a coherent set, designated the set of coherent SI units. The word coherent is used here in the - * following sense: when coherent units are used, equations between the numerical values of quantities take exactly the same form as the equations - * between the quantities themselves. Thus if only units from a coherent set are used, conversion factors between units are never required. - * - * @returnequals(toSystemUnit())
- */ - boolean isSystemUnit(); -} +/* + * Units of Measurement Implementation for Java SE + * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package tec.uom.se; + +import java.io.Serializable; + +import javax.measure.Quantity; +import javax.measure.Unit; + +/** + * Unit specialized for the Java SE platform. It extends {@link Unit} with {@linkplain Comparable} and {@linkplain Serializable } + * + * @see {@link Unit} + * @author werner + * @param+ * @since 1.0.9 + */ +public interface ComparableUnit> extends Unit, Comparable>, Serializable { + + /** + * Compares two instances of {@link Unit }, doing the conversion of unit if necessary. + * + * @param that + * the {@code Unit} to be compared with this instance. + * @return {@code true} if {@code that < this}. + * @throws NullPointerException + * if the unit is null + */ + boolean isEquivalentOf(Unitthat); + + /** + * Indicates if this unit belongs to the set of coherent SI units (unscaled SI units). + * + * The base and coherent derived units of the SI form a coherent set, designated the set of coherent SI units. The word coherent is used here in the + * following sense: when coherent units are used, equations between the numerical values of quantities take exactly the same form as the equations + * between the quantities themselves. Thus if only units from a coherent set are used, conversion factors between units are never required. + * + * @returnequals(toSystemUnit())
+ */ + boolean isSystemUnit(); +} diff --git a/src/main/java/tec/uom/se/format/AbstractUnitFormat.java b/src/main/java/tec/uom/se/format/AbstractUnitFormat.java index c1f42b7d..3aef8c48 100644 --- a/src/main/java/tec/uom/se/format/AbstractUnitFormat.java +++ b/src/main/java/tec/uom/se/format/AbstractUnitFormat.java @@ -1,156 +1,156 @@ -/* - * Units of Measurement Implementation for Java SE - * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions - * and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package tec.uom.se.format; - -import java.io.IOException; -import java.text.ParsePosition; - -import javax.measure.Unit; -import javax.measure.format.ParserException; -import javax.measure.format.UnitFormat; - -import tec.uom.se.AbstractUnit; - -/** - *- * This class provides the interface for formatting and parsing {@link Unit units}. - *
- * - *+/* + * Units of Measurement Implementation for Java SE + * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package tec.uom.se.format; + +import java.io.IOException; +import java.text.ParsePosition; + +import javax.measure.Unit; +import javax.measure.format.ParserException; +import javax.measure.format.UnitFormat; + +import tec.uom.se.AbstractUnit; + +/** + *
+ * This class provides the interface for formatting and parsing {@link Unit units}. + *
+ * + ** For all metric units, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized. For example: