public abstract class AbstractQuantity<Q extends javax.measure.Quantity<Q>> extends Object implements javax.measure.Quantity<Q>, Comparable<javax.measure.Quantity<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.
public static final Quantity
Quantities can be converted to different units.
Quantity
Applications may sub-class AbstractQuantity for particular quantity types.
// Quantity of type Mass based on double primitive types.
public class MassAmount extends AbstractQuantity
private final double kilograms; // Internal SI representation.
private Mass(double kg) { kilograms = kg; }
public static Mass of(double value, Unit
return new Mass(unit.getConverterTo(SI.KILOGRAM).convert(value));
}
public Unit
public Double getValue() { return _kilograms; }
...
}
// Complex numbers measurements.
public class ComplexQuantity
extends AbstractQuantity
{
public Complex getValue() { ... } // Assuming Complex is a Number.
...
}
// Specializations of complex numbers measurements.
public final class Current extends ComplexQuantity
public final class Tension extends ComplexQuantity
All instances of this class shall be immutable.
| 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 unit of specified nature or throw a
ClassCastException if the dimension of the specified
quantity and this measure unit's dimension do not match. |
int |
compareTo(javax.measure.Quantity<Q> that)
Compares this measure to the specified Measurement quantity.
|
abstract double |
doubleValue(javax.measure.Unit<Q> unit) |
boolean |
equals(AbstractQuantity<Q> that,
double epsilon,
javax.measure.Unit<Q> epsilonUnit)
Compares this measure and the specified Measurement to the given accuracy.
|
boolean |
equals(Object obj)
Compares this measure 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 measurement numeric value.
|
int |
hashCode()
Returns the hash code for this quantity.
|
protected long |
longValue(javax.measure.Unit<Q> unit) |
javax.measure.Quantity<Q> |
to(javax.measure.Unit<Q> unit)
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 javax.measure.Quantity<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>>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.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 method which consider 2.0 and 2.00 as different objects because of different internal scales,
measurements such as Measure.valueOf(3.0, KILOGRAM) Measure.valueOf(3, KILOGRAM) and
Quantities.getQuantity("3 kg") might not be considered equals because of possible differences in their implementations.
To compare measures stated using different units or using different amount implementations the compareTo or
equals(Measurement, epsilon, epsilonUnit) methods should be used.
public boolean equals(AbstractQuantity<Q> that, double epsilon, javax.measure.Unit<Q> epsilonUnit)
that - the Measurement 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 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 QuantityFormat class and its subclasses.public abstract double doubleValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticExceptionprotected long longValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticExceptionprotected float floatValue(javax.measure.Unit<Q> unit)
public final <T extends javax.measure.Quantity<T>> AbstractQuantity<T> asType(Class<T> type) throws ClassCastException
ClassCastException if the dimension of the specified
quantity and this measure unit's dimension do not match. For example:
Measure length = Quantities.getQuantity("2 km").asType(Length.class);
asType in interface javax.measure.Quantity<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)Copyright © 2005–2016 Jean-Marie Dautelle, Werner Keil, V2COM. All rights reserved.