public abstract class AbstractQuantity<Q extends javax.measure.Quantity<Q>> extends Object implements ComparableQuantity<Q>, tec.uom.lib.common.function.UnitSupplier<Q>, tec.uom.lib.common.function.ValueSupplier<Number>
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 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).
Quantities can be converted to different units.
Quantity<Velocity> milesPerHour = C.to(MILES_PER_HOUR); // Use double implementation (fast).
System.out.println(milesPerHour);
> 670616629.3843951 m/h
Applications may sub-class
// Complex numbers measurements.AbstractQuantity for particular quantity types.
// Quantity of type Mass based on double primitive types.
public class MassAmount extends AbstractQuantity<Mass> {
private final double kilograms; // Internal SI representation.
private Mass(double kg) { kilograms = kg; }
public static Mass of(double value, Unit<Mass> unit) {
return new Mass(unit.getConverterTo(SI.KILOGRAM).convert(value));
}
public Unit<Mass> getUnit() { return SI.KILOGRAM; }
public Double getValue() { return kilograms; }
...
}
public class ComplexQuantity
<Q extends Quantity>extends AbstractQuantity
<Q>{
public Complex getValue() { ... } // Assuming Complex is a Number.
...
}
// Specializations of complex numbers measurements.
public final class Current extends ComplexQuantity<ElectricCurrent> {...}
public final class Tension extends ComplexQuantity<ElectricPotential> {...}
All instances of this class shall be immutable.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
AbstractQuantity.Equalizer
Utility class for number comparison and equality
|
| Modifier and Type | Field and Description |
|---|---|
static javax.measure.Quantity<javax.measure.quantity.Dimensionless> |
NONE
Holds a dimensionless quantity of none (exact).
|
static javax.measure.Quantity<javax.measure.quantity.Dimensionless> |
ONE
Holds a dimensionless quantity of one (exact).
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractQuantity(javax.measure.Unit<Q> unit)
constructor.
|
| Modifier and Type | Method and Description |
|---|---|
<T extends javax.measure.Quantity<T>> |
asType(Class<T> type)
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. |
int |
compareTo(javax.measure.Quantity<Q> that)
Compares this measure to the specified Measurement quantity.
|
abstract BigDecimal |
decimalValue(javax.measure.Unit<Q> unit,
MathContext ctx) |
<T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> |
divide(javax.measure.Quantity<T> that,
Class<E> asTypeQuantity)
Multiply and cast the
ComparableQuantity |
abstract double |
doubleValue(javax.measure.Unit<Q> unit) |
boolean |
equals(AbstractQuantity<Q> that,
double epsilon,
javax.measure.Unit<Q> epsilonUnit)
Compares this quantity and the specified quantity to the given accuracy.
|
boolean |
equals(Object obj)
Compares this quantity against the specified object for strict equality (same unit and same amount).
|
protected float |
floatValue(javax.measure.Unit<Q> unit) |
javax.measure.Unit<Q> |
getUnit()
Returns the measurement unit.
|
abstract Number |
getValue()
Returns the numeric value of the quantity.
|
int |
hashCode()
Returns the hash code for this quantity.
|
int |
intValue(javax.measure.Unit<Q> unit) |
<T extends javax.measure.Quantity<T>> |
inverse(Class<T> quantityClass)
invert and already cast to defined quantityClass
|
abstract boolean |
isBig() |
boolean |
isEquivalentTo(javax.measure.Quantity<Q> that)
Compares two instances of
, doing the conversion of unit if necessary. |
boolean |
isGreaterThan(javax.measure.Quantity<Q> that)
Compares two instances of
. |
boolean |
isGreaterThanOrEqualTo(javax.measure.Quantity<Q> that)
Compares two instances of
, doing the conversion of unit if necessary. |
boolean |
isLessThan(javax.measure.Quantity<Q> that)
Compares two instances of
, doing the conversion of unit if necessary. |
boolean |
isLessThanOrEqualTo(javax.measure.Quantity<Q> that)
Compares two instances of
, doing the conversion of unit if necessary. |
protected long |
longValue(javax.measure.Unit<Q> unit) |
<T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> |
multiply(javax.measure.Quantity<T> that,
Class<E> asTypeQuantity)
Divide and cast the
ComparableQuantity |
static javax.measure.Quantity<?> |
parse(CharSequence csq)
Returns the quantity of unknown type corresponding to the specified representation.
|
ComparableQuantity<Q> |
to(javax.measure.Unit<Q> unit)
Returns this measure after conversion to specified unit.
|
javax.measure.Quantity<Q> |
to(javax.measure.Unit<Q> unit,
MathContext ctx)
Returns this measure after conversion to specified unit.
|
javax.measure.Quantity<Q> |
toSI()
Convenient method equivalent to
to(this.getUnit().toSI()). |
String |
toString()
Returns the
String representation of this quantity. |
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> NONE
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> ONE
protected AbstractQuantity(javax.measure.Unit<Q> unit)
public javax.measure.Quantity<Q> toSI()
to(this.getUnit().toSI()).ArithmeticException - if the result is inexact and the quotient has a non-terminating decimal expansion.public ComparableQuantity<Q> to(javax.measure.Unit<Q> unit)
Measure.valueOf(doubleValue(unit), unit)
. If this measure is already stated in the specified unit, then this measure is returned and no conversion is performed.to in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>to in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>unit - the unit in which the returned measure is stated.ArithmeticException - if the result is inexact and the quotient has a non-terminating decimal expansion.Quantity.to(Unit)public javax.measure.Quantity<Q> to(javax.measure.Unit<Q> unit, MathContext ctx)
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.unit - the unit in which the returned measure is stated.ctx - the math context to use for conversion.ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY or mathContext.precision == 0 and the quotient
has a non-terminating decimal expansion.public boolean isGreaterThan(javax.measure.Quantity<Q> that)
ComparableQuantity. Conversion of unit can happen if necessaryisGreaterThan in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - the quantity<Q> to be compared with this instance.true if that > this.public boolean isGreaterThanOrEqualTo(javax.measure.Quantity<Q> that)
ComparableQuantity, doing the conversion of unit if necessary.isGreaterThanOrEqualTo in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - the quantity<Q> to be compared with this instance.true if that >= this.public boolean isLessThan(javax.measure.Quantity<Q> that)
ComparableQuantity, doing the conversion of unit if necessary.isLessThan in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - the quantity<Q> to be compared with this instance.true if that < this.public boolean isLessThanOrEqualTo(javax.measure.Quantity<Q> that)
ComparableQuantity, doing the conversion of unit if necessary.isLessThanOrEqualTo in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - the quantity<Q> to be compared with this instance.true if that < this.public boolean isEquivalentTo(javax.measure.Quantity<Q> that)
ComparableQuantity, doing the conversion of unit if necessary.isEquivalentTo in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - the quantity<Q> to be compared with this instance.true if that < this.public int compareTo(javax.measure.Quantity<Q> that)
doubleValue(Unit)
of both this measure and the specified Measurement stated in the same unit (this measure's unit).compareTo in interface Comparable<javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>>NaturalOrder}public boolean equals(Object obj)
Similarly to the BigDecimal.equals(java.lang.Object) 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 compareTo or
equals(Quantity, epsilon, epsilonUnit) methods should be used.
public boolean equals(AbstractQuantity<Q> that, double epsilon, javax.measure.Unit<Q> epsilonUnit)
that - the quantity to compare with.epsilon - the absolute error stated in epsilonUnit.epsilonUnit - the epsilon unit.abs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilonpublic int hashCode()
public abstract boolean isBig()
public String toString()
String 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 MeasurementFormat class and its subclasses.public abstract BigDecimal decimalValue(javax.measure.Unit<Q> unit, MathContext ctx) throws ArithmeticException
ArithmeticExceptionpublic abstract double doubleValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticExceptionpublic final int intValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticExceptionprotected long longValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticExceptionprotected final float floatValue(javax.measure.Unit<Q> unit)
public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> divide(javax.measure.Quantity<T> that, Class<E> asTypeQuantity)
ComparableQuantityComparableQuantitydivide in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - quantity to be multipliedasTypeQuantity - quantity to be convertedQuantity.divide(Quantity),
Quantity.asType(Class)public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> multiply(javax.measure.Quantity<T> that, Class<E> asTypeQuantity)
ComparableQuantityComparableQuantitymultiply in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>that - quantity to be dividedasTypeQuantity - quantity to be convertedQuantityOperations,
QuantityOperations#of(Quantity, Class),
Quantity.asType(Class),
Quantity.multiply(Quantity)public <T extends javax.measure.Quantity<T>> ComparableQuantity<T> inverse(Class<T> quantityClass)
ComparableQuantityinverse in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>quantityClass - Quantity to be convertedQuantity.inverse(),
Quantity.asType(Class)public final <T extends javax.measure.Quantity<T>> ComparableQuantity<T> asType(Class<T> type) throws ClassCastException
ClassCastException if the dimension of the specified
quantity and its unit's dimension do not match. For example:
Quantity length = AbstractQuantity.parse("2 km").asType(Length.class);
asType in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>asType in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>type - the quantity class identifying the nature of the quantity.ClassCastException - if the dimension of this unit is different from the specified quantity dimension.UnsupportedOperationException - if the specified quantity class does not have a public static field named "UNIT" holding the SI unit for the quantity.Unit.asType(Class)public static javax.measure.Quantity<?> parse(CharSequence csq)
Quatity proportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class);
Note: This method handles only standard unit format. Locale-sensitive quantity parsing is currently not
supported.
csq - the decimal value and its unit (if any) separated by space(s).QuantityFormat.getInstance().parse(csq)Copyright © 2005–2017 Units of Measurement project. All rights reserved.