This class represents the immutable result of a scalar quantity 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 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; }
...
}
// Complex number quantities.
public class ComplexQuantity
<Q extends Quantity>extends AbstractQuantity
<Q>{
public Complex getValue() { ... } // Assuming Complex is a Number.
...
}
// Specializations of complex numbers quantities.
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 | Field and Description |
|---|---|
| public static final Quantity | NONE
Holds a dimensionless quantity of none (exact). |
| public static final Quantity | ONE
Holds a dimensionless quantity of one (exact). |
| private final Quantity. | scale
Holds the measurement scale |
| private static final long | |
| private final Unit | unit
Holds the measurement unit |
| Access | Constructor and Description |
|---|---|
| protected | AbstractQuantity(Unit<Q>
a unit unit, Quantity.the scale, absolute or relative scaleConstructor. |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public final <T extends Quantity | Returns: this quantity parameterized with the specified type.the quantity class identifying the nature of the quantity. type)Implements tech. Casts this quantity to a parameterized quantity of specified nature or throw a |
| public int | Returns: a negative integer, zero, or a positive integer as this quantity is less than, equal/equivalent to, or greater than the specified Measurement quantity.Quantity to which this AbstractQuantity is to be compared.Implements java. Compares this quantity to the specified quantity. |
| public <T extends Quantity | divide(Quantity<T>
quantity to be multiplied that, Class<E> quantity to be converted asTypeQuantity)Implements tech. Multiply and cast the |
| public boolean | Returns: this.getUnit.equals(obj.getUnit())
&& this.getScale().equals(obj.getScale()
&& this.getValue().equals(obj.getValue())the object to compare with. obj)Overrides java. Compares this quantity against the specified object for strict equality (same unit and same amount). |
| public Quantity. | Returns: the scale.Implements javax. Returns the absolute or relative scale. |
| public Unit | Returns: the measurement unit.Implements javax. Returns the measurement unit. |
| public abstract Number | Returns: the quantity value.Redeclares javax. Returns the numeric value of the quantity. |
| protected boolean | |
| protected boolean | |
| public int | Returns: the hash code value.Overrides java. Returns the hash code for this quantity. |
| public <T extends Quantity | inverse(Class<T>
Quantity to be converted quantityClass)Implements tech. invert and already cast to defined quantityClass |
| public boolean | isEquivalentTo(Quantity<Q>
the that)quantity<Q> to be compared with this instance.Implements javax. Compares two instances of |
| public boolean | isGreaterThan(Quantity<Q>
the that)quantity<Q> to be compared with this instance.Implements tech. Compares two instances of |
| public boolean | isGreaterThanOrEqualTo(Quantity<Q>
the that)quantity<Q> to be compared with this instance.Implements tech. Compares two instances of |
| public boolean | isLessThan(Quantity<Q>
the that)quantity<Q> to be compared with this instance.Implements tech. Compares two instances of |
| public boolean | isLessThanOrEqualTo(Quantity<Q>
the that)quantity<Q> to be compared with this instance.Implements tech. Compares two instances of |
| public <T extends Quantity | multiply(Quantity<T>
quantity to be divided that, Class<E> quantity to be converted asTypeQuantity)Implements tech. Divide and cast the |
| private NumberSystem | |
| public static Quantity | Returns: SimpleQuantityFormat.getInstance().parse(csq)the decimal value and its unit (if any) separated by space(s). csq)Returns the quantity of unknown type corresponding to the specified representation. |
| public ComparableQuantity | Returns: this quantity or a new quantity equivalent to this quantity stated in the specified unit.the unit in which the returned quantity is stated. anotherUnit)Implements tech. Returns this quantity after conversion to specified unit. |
| public String | Returns: SimpleQuantityFormat.getInstance().format(this)Overrides java. Returns the |
| NONE | back to summary |
|---|---|
| public static final Quantity<Dimensionless> NONE Holds a dimensionless quantity of none (exact). | |
| ONE | back to summary |
|---|---|
| public static final Quantity<Dimensionless> ONE Holds a dimensionless quantity of one (exact). | |
| scale | back to summary |
|---|---|
| private final Quantity. Holds the measurement scale | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| unit | back to summary |
|---|---|
| private final Unit<Q> unit Holds the measurement unit | |
| AbstractQuantity | back to summary |
|---|---|
| protected AbstractQuantity(Unit<Q> unit, Quantity. Constructor. | |
| AbstractQuantity | back to summary |
|---|---|
| protected AbstractQuantity(Unit<Q> unit) Constructor. Applies
| |
| asType | back to summary |
|---|---|
| public final <T extends Quantity Implements tech. Casts this quantity to a parameterized quantity of specified nature or throw a
| |
| compareTo | back to summary |
|---|---|
| public int compareTo(Quantity<Q> that) Implements java. Compares this quantity to the specified quantity. The default implementation compares the value of both this quantity and the specified quantity
stated in the same unit (this quantity's Implementation Note Note: this class uses implicit unit conversion that is inconsistent with
| |
| divide | back to summary |
|---|---|
| public <T extends Quantity Implements tech. Doc from tech. Multiply and cast the
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Compares this quantity against the specified object for strict equality (same unit and same amount).
Similarly to the
To compare quantities stated using different units or using different amount implementations the | |
| getScale | back to summary |
|---|---|
| public Quantity. Implements javax. Returns the absolute or relative scale. | |
| getUnit | back to summary |
|---|---|
| public Unit Implements javax. Returns the measurement unit. | |
| getValue | back to summary |
|---|---|
| public abstract Number getValue() Redeclares javax. Returns the numeric value of the quantity. | |
| hasFraction | back to summary |
|---|---|
| protected boolean hasFraction(double value)
Deprecated seems unused | |
| hasFraction | back to summary |
|---|---|
| protected boolean hasFraction(BigDecimal value)
Deprecated seems unused | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Returns the hash code for this quantity.
| |
| inverse | back to summary |
|---|---|
| public <T extends Quantity Implements tech. Doc from tech. invert and already cast to defined quantityClass | |
| isEquivalentTo | back to summary |
|---|---|
| public boolean isEquivalentTo(Quantity<Q> that) Implements javax. Doc from javax. Compares two instances of | |
| isGreaterThan | back to summary |
|---|---|
| public boolean isGreaterThan(Quantity<Q> that) Implements tech. Doc from tech. Compares two instances of | |
| isGreaterThanOrEqualTo | back to summary |
|---|---|
| public boolean isGreaterThanOrEqualTo(Quantity<Q> that) Implements tech. Doc from tech. Compares two instances of | |
| isLessThan | back to summary |
|---|---|
| public boolean isLessThan(Quantity<Q> that) Implements tech. Doc from tech. Compares two instances of | |
| isLessThanOrEqualTo | back to summary |
|---|---|
| public boolean isLessThanOrEqualTo(Quantity<Q> that) Implements tech. Doc from tech. Compares two instances of | |
| multiply | back to summary |
|---|---|
| public <T extends Quantity Implements tech. Doc from tech. Divide and cast the
| |
| numberSystem | back to summary |
|---|---|
| private NumberSystem numberSystem() | |
| parse | back to summary |
|---|---|
| public static Quantity Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless
quantities. Note This method handles only
| |
| to | back to summary |
|---|---|
| public ComparableQuantity Implements tech. Returns this quantity after conversion to specified unit. The default implementation returns
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns the | |