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<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 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) |
static javax.measure.Quantity<?> |
parse(CharSequence csq)
Returns the quantity of unknown type corresponding to the specified representation.
|
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)) <= epsilon
public 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
ArithmeticException
protected long longValue(javax.measure.Unit<Q> unit) throws ArithmeticException
ArithmeticException
protected float floatValue(javax.measure.Unit<Q> unit)
public final <T extends javax.measure.Quantity<T>> javax.measure.Quantity<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> 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)
public static javax.measure.Quantity<?> parse(CharSequence csq)
Quantity proportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class);
Note: This method handles only standard
unit format.
csq
- the decimal value and its unit (if any) separated by space(s).QuantityFormat.getInstance().parse(csq)
Copyright © 2005–2017 Jean-Marie Dautelle, Werner Keil, V2COM. All rights reserved.