Class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
- All Implemented Interfaces:
Serializable,Comparable<javax.measure.Quantity<Q>>,javax.measure.Quantity<Q>,ComparableQuantity<Q>,tech.uom.lib.common.function.QuantityConverter<Q>,tech.uom.lib.common.function.UnitSupplier<Q>,tech.uom.lib.common.function.ValueSupplier<Number>
- Direct Known Subclasses:
BigIntegerQuantity,ByteQuantity,DecimalQuantity,DoubleQuantity,FloatQuantity,IntegerQuantity,LongQuantity,NumberQuantity,ShortQuantity,TemporalQuantity,TimeUnitQuantity
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 quantities.
public final class Current extends ComplexQuantity<ElectricCurrent> {...}
public final class Tension extends ComplexQuantity<ElectricPotential> {...}
All instances of this class shall be immutable.
- Since:
- 1.0
- Version:
- 1.3, April 13, 2018
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classUtility class for number comparison and equality -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final javax.measure.Quantity<javax.measure.quantity.Dimensionless> Holds a dimensionless quantity of none (exact).static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> Holds a dimensionless quantity of one (exact).private static final longprivate final javax.measure.Unit<Q> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractQuantity(javax.measure.Unit<Q> unit) constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends javax.measure.Quantity<T>>
ComparableQuantity<T> Casts this quantity to a parameterized quantity of specified nature or throw aClassCastExceptionif the dimension of the specified quantity and its unit's dimension do not match.intCompares this measure to the specified Measurement quantity.abstract BigDecimaldecimalValue(javax.measure.Unit<Q> unit) <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>>
ComparableQuantity<E> Multiply and cast theComparableQuantityabstract doubledoubleValue(javax.measure.Unit<Q> unit) booleanCompares this quantity against the specified object for strict equality (same unit and same amount).booleanequals(AbstractQuantity<Q> that, double epsilon, javax.measure.Unit<Q> epsilonUnit) Compares this quantity and the specified quantity to the given accuracy.protected final floatfloatValue(javax.measure.Unit<Q> unit) javax.measure.Unit<Q> getUnit()Returns the measurement unit.abstract NumbergetValue()Returns the numeric value of the quantity.protected booleanhasFraction(double value) protected booleanhasFraction(BigDecimal value) inthashCode()Returns the hash code for this quantity.final int<T extends javax.measure.Quantity<T>>
ComparableQuantity<T> invert and already cast to defined quantityClassabstract booleanisBig()booleanisEquivalentOf(javax.measure.Quantity<Q> that) Compares two instances of, doing the conversion of unit if necessary.booleanisGreaterThan(javax.measure.Quantity<Q> that) Compares two instances of.booleanisGreaterThanOrEqualTo(javax.measure.Quantity<Q> that) Compares two instances of, doing the conversion of unit if necessary.booleanisLessThan(javax.measure.Quantity<Q> that) Compares two instances of, doing the conversion of unit if necessary.booleanisLessThanOrEqualTo(javax.measure.Quantity<Q> that) Compares two instances of, doing the conversion of unit if necessary.protected long<T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>>
ComparableQuantity<E> Divide and cast theComparableQuantitystatic javax.measure.Quantity<?> parse(CharSequence csq) Returns the quantity of unknown type corresponding to the specified representation.Returns this quantity after conversion to specified unit.toString()Returns theStringrepresentation of this quantity.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
unit
-
NONE
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> NONEHolds a dimensionless quantity of none (exact). -
ONE
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> ONEHolds a dimensionless quantity of one (exact).
-
-
Constructor Details
-
AbstractQuantity
constructor.
-
-
Method Details
-
getValue
Returns the numeric value of the quantity. -
getUnit
Returns the measurement unit. -
to
Returns this quantity after conversion to specified unit. The default implementation returnsMeasure.valueOf(doubleValue(unit), unit). If this quantity is already stated in the specified unit, then this quantity is returned and no conversion is performed.- Specified by:
toin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Specified by:
toin interfacejavax.measure.Quantity<Q extends javax.measure.Quantity<Q>>- Specified by:
toin interfacetech.uom.lib.common.function.QuantityConverter<Q extends javax.measure.Quantity<Q>>- Parameters:
unit- the unit in which the returned measure is stated.- Returns:
- this quantity or a new quantity equivalent to this quantity stated in the specified unit.
- Throws:
ArithmeticException- if the result is inexact and the quotient has a non-terminating decimal expansion.- See Also:
-
isGreaterThan
Description copied from interface:ComparableQuantityCompares two instances of. Conversion of unit can happen if necessary- Specified by:
isGreaterThanin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat > this.
-
isGreaterThanOrEqualTo
Description copied from interface:ComparableQuantityCompares two instances of, doing the conversion of unit if necessary.- Specified by:
isGreaterThanOrEqualToin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat >= this.
-
isLessThan
Description copied from interface:ComparableQuantityCompares two instances of, doing the conversion of unit if necessary.- Specified by:
isLessThanin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat < this.
-
isLessThanOrEqualTo
Description copied from interface:ComparableQuantityCompares two instances of, doing the conversion of unit if necessary.- Specified by:
isLessThanOrEqualToin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat < this.
-
isEquivalentOf
Description copied from interface:ComparableQuantityCompares two instances of, doing the conversion of unit if necessary.- Specified by:
isEquivalentOfin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat < this.
-
compareTo
Compares this measure to the specified Measurement quantity. The default implementation compares thedoubleValue(Unit)of both this measure and the specified Measurement stated in the same unit (this measure'sunit).- Specified by:
compareToin interfaceComparable<Q extends javax.measure.Quantity<Q>>- Returns:
- a negative integer, zero, or a positive integer as this measure is less than, equal to, or greater than the specified Measurement quantity.
-
equals
Compares this quantity against the specified object for strict equality (same unit and same amount).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 asQuantities.getQuantity(3.0, KILOGRAM)Quantities.getQuantity(3, KILOGRAM)andQuantities.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
compareToormethods should be used.invalid reference
equals(Quantity, epsilon, epsilonUnit) -
equals
Compares this quantity and the specified quantity to the given accuracy. Quantities are considered approximately equals if their absolute differences when stated in the same specified unit is less than the specified epsilon.- Parameters:
that- the quantity to compare with.epsilon- the absolute error stated in epsilonUnit.epsilonUnit- the epsilon unit.- Returns:
abs(this.doubleValue(epsilonUnit) - that.doubleValue(epsilonUnit)) <= epsilon
-
hashCode
public int hashCode()Returns the hash code for this quantity. -
isBig
public abstract boolean isBig() -
toString
Returns theStringrepresentation 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 theQuantityFormatimplementations and its subclasses. -
decimalValue
- Throws:
ArithmeticException
-
doubleValue
- Throws:
ArithmeticException
-
intValue
- Throws:
ArithmeticException
-
longValue
- Throws:
ArithmeticException
-
floatValue
-
divide
public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> divide(javax.measure.Quantity<T> that, Class<E> asTypeQuantity) Description copied from interface:ComparableQuantityMultiply and cast theComparableQuantity- Specified by:
dividein interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- quantity to be multipliedasTypeQuantity- quantity to be converted- Returns:
- the QuantityOperations multiplied and converted
- See Also:
-
multiply
public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> multiply(javax.measure.Quantity<T> that, Class<E> asTypeQuantity) Description copied from interface:ComparableQuantityDivide and cast theComparableQuantity- Specified by:
multiplyin interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
that- quantity to be dividedasTypeQuantity- quantity to be converted- Returns:
- the QuantityOperations multiplied and converted
- See Also:
-
inverse
Description copied from interface:ComparableQuantityinvert and already cast to defined quantityClass- Specified by:
inversein interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Parameters:
quantityClass- Quantity to be converted- See Also:
-
asType
public final <T extends javax.measure.Quantity<T>> ComparableQuantity<T> asType(Class<T> type) throws ClassCastException Casts this quantity to a parameterized quantity of specified nature or throw aClassCastExceptionif the dimension of the specified quantity and its unit's dimension do not match. For example:
Quantitylength = AbstractQuantity.parse("2 km").asType(Length.class); - Specified by:
asTypein interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>- Specified by:
asTypein interfacejavax.measure.Quantity<Q extends javax.measure.Quantity<Q>>- Parameters:
type- the quantity class identifying the nature of the quantity.- Returns:
- this quantity parameterized with the specified type.
- Throws:
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.- See Also:
-
parse
Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless quantities.
Quatityproportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class); Note: This method handles only
unit format. Locale-sensitive quantity parsing is currently not supported.invalid reference
standard- Parameters:
csq- the decimal value and its unit (if any) separated by space(s).- Returns:
QuantityFormat.getInstance().parse(csq)
-
hasFraction
protected boolean hasFraction(double value) -
hasFraction
-