|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.measure.Measure<Q>
public abstract class Measure<Q extends Quantity>
This class represents the immutable result of a scalar measurement stated in a known unit.
To avoid any lost of precision, known exact measure (e.g. physical
constants) should not be created from double constants but
from their decimal representation.
public static final Measure<Velocity> C = Measure.valueOf("299792458 m/s", Velocity.class); // Speed of Light (exact).
Measures can be converted to different units, the conversion precision is
determined by the specified MathContext.
If no precision is specified
Measure<Velocity> milesPerHour = C.to(MILES_PER_HOUR, MathContext.DECIMAL128); // Use BigDecimal implementation.
System.out.println(milesPerHour);
> 670616629.3843951324266284896206156 [mi_i]/h
double precision is assumed.
Measure<Velocity> milesPerHour = C.to(MILES_PER_HOUR); // Use double implementation (fast).
System.out.println(milesPerHour);
> 670616629.3843951 [mi_i]/h
Applications may sub-class Measure for particular measurements
types.
// Measurement of type Mass based on <code>double</code> primitive types.
public class Weight extends Measure<Mass> {
private final double _kilograms; // Internal SI representation.
private Weight(double kilograms) { _kilograms = kilograms; }
public static Weight valueOf(double value, Unit<Mass> unit) {
return new Weight(unit.getConverterTo(SI.KILOGRAM).convert(value));
}
public Unit<Mass> getUnit() { return SI.KILOGRAM; }
public Double getValue() { return _kilograms; }
...
}
// Complex numbers measurements.
public class ComplexMeasure<Q extends Quantity> extends Measure<Q> {
public Complex getValue() { ... } // Assuming Complex is a Number.
...
}
// Specializations of complex numbers measurements.
public class Current extends ComplexMeasure<ElectricCurrent> {...}
public class Tension extends ComplexMeasure<ElectricPotential> {...}
All instances of this class shall be immutable.
| Constructor Summary | |
|---|---|
protected |
Measure()
Default constructor. |
| Method Summary | ||
|---|---|---|
boolean |
approximates(Measurable<Q> that,
double accuracy)
Compares this measure and the specified measurable to the given accuracy. |
|
|
asType(java.lang.Class<T> type)
Casts this measure 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(Measurable<Q> that)
Compares this measure to the specified measurable quantity. |
|
boolean |
equals(java.lang.Object obj)
Compares this measure against the specified object for strict equality (same unit and same amount). |
|
float |
floatValue(Unit<Q> unit)
Returns the float value of this measurable
when stated in the specified unit. |
|
abstract Unit<Q> |
getUnit()
Returns the measurement unit. |
|
abstract java.lang.Number |
getValue()
Returns the measurement numeric value. |
|
int |
hashCode()
Returns the hash code for this measure. |
|
int |
intValue(Unit<Q> unit)
Returns the integral int value of this measurable when
stated in the specified unit. |
|
long |
longValue(Unit<Q> unit)
Returns the integral long value of this measurable when
stated in the specified unit. |
|
Measure<Q> |
to(Unit<Q> unit)
Returns this measure after conversion to specified unit. |
|
Measure<Q> |
to(Unit<Q> unit,
java.math.MathContext ctx)
Returns this measure after conversion to specified unit. |
|
Measure<Q> |
toSI()
Convenient method equivalent to to(this.getUnit().toSI()). |
|
java.lang.String |
toString()
Returns the String representation of this measure. |
|
static
|
valueOf(java.math.BigDecimal decimalValue,
Unit<Q> unit)
Returns the scalar measure for the specified BigDecimal
stated in the specified unit. |
|
static Measure<?> |
valueOf(java.lang.CharSequence csq)
Returns the decimal
measure of unknown type corresponding to the specified representation. |
|
static
|
valueOf(double doubleValue,
Unit<Q> unit)
Returns the scalar measure for the specified double stated
in the specified unit. |
|
static
|
valueOf(float floatValue,
Unit<Q> unit)
Returns the scalar measure for the specified float stated in
the specified unit. |
|
static
|
valueOf(int intValue,
Unit<Q> unit)
Returns the scalar measure for the specified int stated in
the specified unit. |
|
static
|
valueOf(long longValue,
Unit<Q> unit)
Returns the scalar measure for the specified long stated in
the specified unit. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface javax.measure.Measurable |
|---|
decimalValue, doubleValue |
| Constructor Detail |
|---|
protected Measure()
| Method Detail |
|---|
public abstract java.lang.Number getValue()
public abstract Unit<Q> getUnit()
public Measure<Q> toSI()
to(this.getUnit().toSI()).
java.lang.ArithmeticException - if the result is inexact and the quotient
has a non-terminating decimal expansion.public Measure<Q> to(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.
unit - the unit in which the returned measure is stated.
java.lang.ArithmeticException - if the result is inexact and the quotient has
a
non-terminating decimal expansion.
public Measure<Q> to(Unit<Q> unit,
java.math.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.
java.lang.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 approximates(Measurable<Q> that,
double accuracy)
that - the measurable to compare with.accuracy - the absolute error allowed.
abs(this.doubleValue(getUnit())- that.doubleValue(getUnit())) <= accuracypublic int compareTo(Measurable<Q> that)
Measurable.doubleValue(Unit) of both
this measure and the specified measurable stated in the same unit (this
measure's unit).
compareTo in interface java.lang.Comparable<Measurable<Q extends Quantity>>public boolean equals(java.lang.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,
measurements such as Measure.valueOf(3.0, KILOGRAM)
Measure.valueOf(3, KILOGRAM) and
Measure.valueOf("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(javax.measure.Measurable or
)
approximates(javax.measure.Measurable methods should be used., double)
equals in class java.lang.Objectobj - the object to compare with.
this.getUnit.equals(obj.getUnit())
&& this.getValue().equals(obj.getValue())public int hashCode()
hashCode in class java.lang.Objectpublic final java.lang.String toString()
String representation of this measure. The
string produced for a given measure is always the same; it is not
affected by locale. This means that it can be used as a canonical string
representation for exchanging measure, or as a key for a Hashtable, etc.
Locale-sensitive measure formatting and parsing is handled by the
MeasureFormat class and its subclasses.
toString in class java.lang.ObjectUnitFormat.getInternational().format(this)
public final int intValue(Unit<Q> unit)
throws java.lang.ArithmeticException
Measurableint value of this measurable when
stated in the specified unit.
Note: This method differs from the Number.intValue()
in the sense that an ArithmeticException is raised instead
of a bit truncation in case of overflow (safety critical).
intValue in interface Measurable<Q extends Quantity>unit - the unit in which the returned value is stated.
int.
java.lang.ArithmeticException - if this measurable cannot be represented
by a int number in the specified unit.
public long longValue(Unit<Q> unit)
throws java.lang.ArithmeticException
Measurablelong value of this measurable when
stated in the specified unit.
Note: This method differs from the Number.longValue()
in the sense that an ArithmeticException is raised instead
of a bit truncation in case of overflow (safety critical).
longValue in interface Measurable<Q extends Quantity>unit - the unit in which the returned value is stated.
long.
java.lang.ArithmeticException - if this measurable cannot be represented
by a int number in the specified unit.public final float floatValue(Unit<Q> unit)
Measurablefloat value of this measurable
when stated in the specified unit. If the measurable has too great of
a magnitude to be represented as a float,
FLOAT.NEGATIVE_INFINITY or
FLOAT.POSITIVE_INFINITY is returned as appropriate.
floatValue in interface Measurable<Q extends Quantity>unit - the unit in which this returned value is stated.
float.
public final <T extends Quantity> Measure<T> asType(java.lang.Class<T> type)
throws java.lang.ClassCastException
ClassCastException if the dimension of the specified
quantity and this measure unit's dimension do not match. For
example:
Measure<Length> length = Measure.valueOf("2 km").asType(Length.class);
type - the quantity class identifying the nature of the measure.
java.lang.ClassCastException - if the dimension of this unit is different
from the specified quantity dimension.
java.lang.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 Measure<?> valueOf(java.lang.CharSequence csq)
decimal
measure of unknown type corresponding to the specified representation.
This method can be used to parse dimensionless quantities.
Measure<Dimensionless> proportion = Measure.valueOf("0.234").asType(Dimensionless.class);
Note: This method handles only
standard unit format
(UCUM based). Locale-sensitive
measure formatting and parsing are handled by the MeasureFormat
class and its subclasses.
csq - the decimal value and its unit (if any) separated by space(s).
MeasureFormat.getInternational().parse(csq, new ParsePosition(0))
public static <Q extends Quantity> Measure<Q> valueOf(int intValue,
Unit<Q> unit)
int stated in
the specified unit.
intValue - the measurement value.unit - the measurement unit.
int measure.
public static <Q extends Quantity> Measure<Q> valueOf(long longValue,
Unit<Q> unit)
long stated in
the specified unit.
longValue - the measurement value.unit - the measurement unit.
long measure.
public static <Q extends Quantity> Measure<Q> valueOf(float floatValue,
Unit<Q> unit)
float stated in
the specified unit.
floatValue - the measurement value.unit - the measurement unit.
float measure.
public static <Q extends Quantity> Measure<Q> valueOf(double doubleValue,
Unit<Q> unit)
double stated
in the specified unit.
doubleValue - the measurement value.unit - the measurement unit.
double measure.
public static <Q extends Quantity> Measure<Q> valueOf(java.math.BigDecimal decimalValue,
Unit<Q> unit)
BigDecimal
stated in the specified unit.
decimalValue - the measurement value.unit - the measurement unit.
BigDecimal measure.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||