Class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
- java.lang.Object
-
- tech.units.indriya.AbstractQuantity<Q>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.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<java.lang.Number>
- Direct Known Subclasses:
BigIntegerQuantity
,ByteQuantity
,DecimalQuantity
,DoubleQuantity
,FloatQuantity
,IntegerQuantity
,LongQuantity
,NumberQuantity
,ShortQuantity
,TemporalQuantity
,TimeUnitQuantity
public abstract class AbstractQuantity<Q extends javax.measure.Quantity<Q>> extends java.lang.Object implements ComparableQuantity<Q>, tech.uom.lib.common.function.UnitSupplier<Q>, tech.uom.lib.common.function.ValueSupplier<java.lang.Number>
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
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 numbers measurements.
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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractQuantity.Equalizer
Utility class for number comparison and equality
-
Field Summary
Fields Modifier and Type Field 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).private static long
serialVersionUID
private javax.measure.Unit<Q>
unit
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractQuantity(javax.measure.Unit<Q> unit)
constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <T extends javax.measure.Quantity<T>>
ComparableQuantity<T>asType(java.lang.Class<T> type)
Casts this quantity to a parameterized quantity of specified nature or throw aClassCastException
if the dimension of the specified quantity and its unit's dimension do not match.int
compareTo(javax.measure.Quantity<Q> that)
Compares this measure to the specified Measurement quantity.abstract java.math.BigDecimal
decimalValue(javax.measure.Unit<Q> unit)
<T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>>
ComparableQuantity<E>divide(javax.measure.Quantity<T> that, java.lang.Class<E> asTypeQuantity)
Multiply and cast theComparableQuantity
abstract double
doubleValue(javax.measure.Unit<Q> unit)
boolean
equals(java.lang.Object obj)
Compares this quantity against the specified object for strict equality (same unit and same amount).boolean
equals(AbstractQuantity<Q> that, double epsilon, javax.measure.Unit<Q> epsilonUnit)
Compares this quantity and the specified quantity to the given accuracy.protected float
floatValue(javax.measure.Unit<Q> unit)
javax.measure.Unit<Q>
getUnit()
Returns the measurement unit.abstract java.lang.Number
getValue()
Returns the numeric value of the quantity.protected boolean
hasFraction(double value)
protected boolean
hasFraction(java.math.BigDecimal value)
int
hashCode()
Returns the hash code for this quantity.int
intValue(javax.measure.Unit<Q> unit)
<T extends javax.measure.Quantity<T>>
ComparableQuantity<T>inverse(java.lang.Class<T> quantityClass)
invert and already cast to defined quantityClassabstract boolean
isBig()
boolean
isEquivalentOf(javax.measure.Quantity<Q> that)
Compares two instances of, doing the conversion of unit if necessary.
boolean
isGreaterThan(javax.measure.Quantity<Q> that)
Compares two instances of.
boolean
isGreaterThanOrEqualTo(javax.measure.Quantity<Q> that)
Compares two instances of, doing the conversion of unit if necessary.
boolean
isLessThan(javax.measure.Quantity<Q> that)
Compares two instances of, doing the conversion of unit if necessary.
boolean
isLessThanOrEqualTo(javax.measure.Quantity<Q> that)
Compares two instances of, doing the conversion of unit if necessary.
protected long
longValue(javax.measure.Unit<Q> unit)
<T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>>
ComparableQuantity<E>multiply(javax.measure.Quantity<T> that, java.lang.Class<E> asTypeQuantity)
Divide and cast theComparableQuantity
static javax.measure.Quantity<?>
parse(java.lang.CharSequence csq)
Returns the quantity of unknown type corresponding to the specified representation.ComparableQuantity<Q>
to(javax.measure.Unit<Q> unit)
Returns this quantity after conversion to specified unit.java.lang.String
toString()
Returns theString
representation of this quantity.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
NONE
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> NONE
Holds a dimensionless quantity of none (exact).
-
ONE
public static final javax.measure.Quantity<javax.measure.quantity.Dimensionless> ONE
Holds a dimensionless quantity of one (exact).
-
-
Constructor Detail
-
AbstractQuantity
protected AbstractQuantity(javax.measure.Unit<Q> unit)
constructor.
-
-
Method Detail
-
getValue
public abstract java.lang.Number getValue()
Returns the numeric value of the quantity.
-
getUnit
public javax.measure.Unit<Q> getUnit()
Returns the measurement unit.
-
to
public ComparableQuantity<Q> to(javax.measure.Unit<Q> unit)
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:
to
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Specified by:
to
in interfacejavax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
- Specified by:
to
in 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:
java.lang.ArithmeticException
- if the result is inexact and the quotient has a non-terminating decimal expansion.- See Also:
Quantity.to(Unit)
-
isGreaterThan
public boolean isGreaterThan(javax.measure.Quantity<Q> that)
Description copied from interface:ComparableQuantity
Compares two instances of. Conversion of unit can happen if necessary
- Specified by:
isGreaterThan
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
that
- thequantity<Q>
to be compared with this instance.- Returns:
true
ifthat > this
.
-
isGreaterThanOrEqualTo
public boolean isGreaterThanOrEqualTo(javax.measure.Quantity<Q> that)
Description copied from interface:ComparableQuantity
Compares two instances of, doing the conversion of unit if necessary.
- Specified by:
isGreaterThanOrEqualTo
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
that
- thequantity<Q>
to be compared with this instance.- Returns:
true
ifthat >= this
.
-
isLessThan
public boolean isLessThan(javax.measure.Quantity<Q> that)
Description copied from interface:ComparableQuantity
Compares two instances of, doing the conversion of unit if necessary.
- Specified by:
isLessThan
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
that
- thequantity<Q>
to be compared with this instance.- Returns:
true
ifthat < this
.
-
isLessThanOrEqualTo
public boolean isLessThanOrEqualTo(javax.measure.Quantity<Q> that)
Description copied from interface:ComparableQuantity
Compares two instances of, doing the conversion of unit if necessary.
- Specified by:
isLessThanOrEqualTo
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
that
- thequantity<Q>
to be compared with this instance.- Returns:
true
ifthat < this
.
-
isEquivalentOf
public boolean isEquivalentOf(javax.measure.Quantity<Q> that)
Description copied from interface:ComparableQuantity
Compares two instances of, doing the conversion of unit if necessary.
- Specified by:
isEquivalentOf
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
that
- thequantity<Q>
to be compared with this instance.- Returns:
true
ifthat < this
.
-
compareTo
public int compareTo(javax.measure.Quantity<Q> that)
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
).
-
equals
public boolean equals(java.lang.Object obj)
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
compareTo
orequals(Quantity, epsilon, epsilonUnit)
methods should be used.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the object to compare with.- Returns:
this.getUnit.equals(obj.getUnit()) && this.getValue().equals(obj.getValue())
-
equals
public boolean equals(AbstractQuantity<Q> that, double epsilon, javax.measure.Unit<Q> epsilonUnit)
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.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code value.
-
isBig
public abstract boolean isBig()
-
toString
public java.lang.String toString()
Returns theString
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 theQuantityFormat
implementations and its subclasses.- Overrides:
toString
in classjava.lang.Object
- Returns:
UnitFormat.getInternational().format(this)
-
decimalValue
public abstract java.math.BigDecimal decimalValue(javax.measure.Unit<Q> unit) throws java.lang.ArithmeticException
- Throws:
java.lang.ArithmeticException
-
doubleValue
public abstract double doubleValue(javax.measure.Unit<Q> unit) throws java.lang.ArithmeticException
- Throws:
java.lang.ArithmeticException
-
intValue
public final int intValue(javax.measure.Unit<Q> unit) throws java.lang.ArithmeticException
- Throws:
java.lang.ArithmeticException
-
longValue
protected long longValue(javax.measure.Unit<Q> unit) throws java.lang.ArithmeticException
- Throws:
java.lang.ArithmeticException
-
floatValue
protected final float floatValue(javax.measure.Unit<Q> unit)
-
divide
public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> divide(javax.measure.Quantity<T> that, java.lang.Class<E> asTypeQuantity)
Description copied from interface:ComparableQuantity
Multiply and cast theComparableQuantity
- Specified by:
divide
in 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:
Quantity.divide(Quantity)
,Quantity.asType(Class)
-
multiply
public <T extends javax.measure.Quantity<T>,E extends javax.measure.Quantity<E>> ComparableQuantity<E> multiply(javax.measure.Quantity<T> that, java.lang.Class<E> asTypeQuantity)
Description copied from interface:ComparableQuantity
Divide and cast theComparableQuantity
- Specified by:
multiply
in 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:
QuantityOperations
,QuantityOperations#of(Quantity, Class)
,Quantity.asType(Class)
,Quantity.multiply(Quantity)
-
inverse
public <T extends javax.measure.Quantity<T>> ComparableQuantity<T> inverse(java.lang.Class<T> quantityClass)
Description copied from interface:ComparableQuantity
invert and already cast to defined quantityClass- Specified by:
inverse
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Parameters:
quantityClass
- Quantity to be converted- See Also:
Quantity.inverse()
,Quantity.asType(Class)
-
asType
public final <T extends javax.measure.Quantity<T>> ComparableQuantity<T> asType(java.lang.Class<T> type) throws java.lang.ClassCastException
Casts this quantity to a parameterized quantity of specified nature or throw aClassCastException
if the dimension of the specified quantity and its unit's dimension do not match. For example:
Quantity
length = AbstractQuantity.parse("2 km").asType(Length.class); - Specified by:
asType
in interfaceComparableQuantity<Q extends javax.measure.Quantity<Q>>
- Specified by:
asType
in 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:
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.- See Also:
Unit.asType(Class)
-
parse
public static javax.measure.Quantity<?> parse(java.lang.CharSequence csq)
Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless quantities.
Quatity
proportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class); Note: This method handles only
standard
unit format. Locale-sensitive quantity parsing is currently not supported.- 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
protected boolean hasFraction(java.math.BigDecimal value)
-
-