Class AbstractQuantity<Q extends Quantity<Q>>
- java.lang.Object
-
- tech.units.indriya.AbstractQuantity<Q>
-
- All Implemented Interfaces:
Serializable,Comparable<Quantity<Q>>,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:
NumberQuantity,TemporalQuantity,TimeUnitQuantity
@API(status=STABLE) public abstract class AbstractQuantity<Q extends Quantity<Q>> extends Object implements ComparableQuantity<Q>, tech.uom.lib.common.function.UnitSupplier<Q>, tech.uom.lib.common.function.ValueSupplier<Number>
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
doubleconstants 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/hApplications may sub-class
AbstractQuantityfor 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.
- Since:
- 1.0
- Version:
- 2.6 Oct 6, 2024
- Author:
- Werner Keil, Andi Huber
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface javax.measure.Quantity
Quantity.Scale
-
-
Field Summary
Fields Modifier and Type Field Description static Quantity<Dimensionless>NONEHolds a dimensionless quantity of none (exact).static Quantity<Dimensionless>ONEHolds a dimensionless quantity of one (exact).
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractQuantity(Unit<Q> unit)Constructor.protectedAbstractQuantity(Unit<Q> unit, Quantity.Scale scale)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T extends Quantity<T>>
ComparableQuantity<T>asType(Class<T> type)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.intcompareTo(Quantity<Q> that)Compares this quantity to the specified quantity.<T extends Quantity<T>,E extends Quantity<E>>
ComparableQuantity<E>divide(Quantity<T> that, Class<E> asTypeQuantity)Multiply and cast theComparableQuantitybooleanequals(Object obj)Compares this quantity against the specified object for strict equality (same unit and same amount).Quantity.ScalegetScale()Returns the absolute or relative scale.Unit<Q>getUnit()Returns the measurement unit.abstract NumbergetValue()Returns the numeric value of the quantity.protected booleanhasFraction(double value)Deprecated.seems unusedprotected booleanhasFraction(BigDecimal value)Deprecated.seems unusedinthashCode()Returns the hash code for this quantity.<T extends Quantity<T>>
ComparableQuantity<T>inverse(Class<T> quantityClass)invert and already cast to defined quantityClassbooleanisEquivalentTo(Quantity<Q> that)booleanisGreaterThan(Quantity<Q> that)Compares two instances ofQuantity.booleanisGreaterThanOrEqualTo(Quantity<Q> that)Compares two instances ofQuantity, converting the unit if necessary.booleanisLessThan(Quantity<Q> that)Compares two instances ofQuantity, converting the unit if necessary.booleanisLessThanOrEqualTo(Quantity<Q> that)Compares two instances ofQuantity, converting the unit if necessary.<T extends Quantity<T>,E extends Quantity<E>>
ComparableQuantity<E>multiply(Quantity<T> that, Class<E> asTypeQuantity)Divide and cast theComparableQuantitystatic Quantity<?>parse(CharSequence csq)Returns the quantity of unknown type corresponding to the specified representation.ComparableQuantity<Q>to(Unit<Q> anotherUnit)Returns this quantity after conversion to specified unit.StringtoString()Returns theStringrepresentation of this quantity.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface tech.units.indriya.ComparableQuantity
add, divide, divide, inverse, multiply, multiply, subtract
-
Methods inherited from interface javax.measure.Quantity
negate, toSystemUnit
-
-
-
-
Field Detail
-
NONE
public static final Quantity<Dimensionless> NONE
Holds a dimensionless quantity of none (exact).
-
ONE
public static final Quantity<Dimensionless> ONE
Holds a dimensionless quantity of one (exact).
-
-
Constructor Detail
-
AbstractQuantity
protected AbstractQuantity(Unit<Q> unit, Quantity.Scale scale)
Constructor.- Parameters:
unit- a unitscale- the scale, absolute or relative
-
AbstractQuantity
protected AbstractQuantity(Unit<Q> unit)
Constructor. AppliesABSOLUTEScaleif none was given.- Parameters:
unit- a unit
-
-
Method Detail
-
getScale
public Quantity.Scale getScale()
Returns the absolute or relative scale.
-
to
public ComparableQuantity<Q> to(Unit<Q> anotherUnit)
Returns this quantity after conversion to specified unit. The default implementation returnsNumberQuantity.of(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 Quantity<Q>>- Specified by:
toin interfaceQuantity<Q extends Quantity<Q>>- Specified by:
toin interfacetech.uom.lib.common.function.QuantityConverter<Q extends Quantity<Q>>- Parameters:
anotherUnit- the unit in which the returned quantity 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:
Quantity.to(Unit)
-
isGreaterThan
public boolean isGreaterThan(Quantity<Q> that)
Description copied from interface:ComparableQuantityCompares two instances ofQuantity. Conversion of unit can happen if necessary- Specified by:
isGreaterThanin interfaceComparableQuantity<Q extends Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthis > that.
-
isGreaterThanOrEqualTo
public boolean isGreaterThanOrEqualTo(Quantity<Q> that)
Description copied from interface:ComparableQuantityCompares two instances ofQuantity, converting the unit if necessary.- Specified by:
isGreaterThanOrEqualToin interfaceComparableQuantity<Q extends Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthis >= that.
-
isLessThan
public boolean isLessThan(Quantity<Q> that)
Description copied from interface:ComparableQuantityCompares two instances ofQuantity, converting the unit if necessary.- Specified by:
isLessThanin interfaceComparableQuantity<Q extends Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthis < that.
-
isLessThanOrEqualTo
public boolean isLessThanOrEqualTo(Quantity<Q> that)
Description copied from interface:ComparableQuantityCompares two instances ofQuantity, converting the unit if necessary.- Specified by:
isLessThanOrEqualToin interfaceComparableQuantity<Q extends Quantity<Q>>- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthis <= that.
-
isEquivalentTo
public boolean isEquivalentTo(Quantity<Q> that)
- Specified by:
isEquivalentToin interfaceQuantity<Q extends Quantity<Q>>
-
compareTo
public int compareTo(Quantity<Q> that)
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'sunit). If units are not the same, the unit of the specified quantity is converted.- Specified by:
compareToin interfaceComparable<Q extends Quantity<Q>>- Parameters:
that-Quantityto which thisAbstractQuantityis to be compared.- 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.
-
equals
public boolean equals(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
compareToorisEquivalentTo(javax.measure.Quantity<Q>)methods should be used.
-
hashCode
public int hashCode()
Returns the hash code for this quantity.
-
toString
public String 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.
-
divide
public <T extends Quantity<T>,E extends Quantity<E>> ComparableQuantity<E> divide(Quantity<T> that, Class<E> asTypeQuantity)
Description copied from interface:ComparableQuantityMultiply and cast theComparableQuantity- Specified by:
dividein interfaceComparableQuantity<Q extends 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 Quantity<T>,E extends Quantity<E>> ComparableQuantity<E> multiply(Quantity<T> that, Class<E> asTypeQuantity)
Description copied from interface:ComparableQuantityDivide and cast theComparableQuantity- Specified by:
multiplyin interfaceComparableQuantity<Q extends 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 Quantity<T>> ComparableQuantity<T> inverse(Class<T> quantityClass)
Description copied from interface:ComparableQuantityinvert and already cast to defined quantityClass- Specified by:
inversein interfaceComparableQuantity<Q extends Quantity<Q>>- Parameters:
quantityClass- Quantity to be converted- See Also:
Quantity.inverse(),Quantity.asType(Class)
-
asType
public final <T extends 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 Quantity<Q>>- Specified by:
asTypein interfaceQuantity<Q extends 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:
Unit.asType(Class)
-
parse
public static Quantity<?> parse(CharSequence csq)
Returns the quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless quantities.
Quantityproportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class); Note: This method handles only
standardunit format. Locale-sensitive ormixedquantity parsing is currently not supported by this class. If you need flexible parsing of both single and mixed quantities, please usegetQuantity()method of theQuantitiesfacade instead.- Parameters:
csq- the decimal value and its unit (if any) separated by space(s).- Returns:
SimpleQuantityFormat.getInstance().parse(csq)
-
hasFraction
protected boolean hasFraction(double value)
Deprecated.seems unused
-
hasFraction
protected boolean hasFraction(BigDecimal value)
Deprecated.seems unused
-
-