Top Description Fields Constructors Methods
tech.units.indriya

public abstract Class AbstractQuantity<Q extends Quantity<Q>>

extends Object
implements ComparableQuantity<Q extends Quantity<Q>>, UnitSupplier<Q extends Quantity<Q>>, ValueSupplier<Number>
Class Inheritance
All Implemented Interfaces
tech.uom.lib.common.function.ValueSupplier, tech.uom.lib.common.function.UnitSupplier, tech.units.indriya.ComparableQuantity, java.io.Serializable, tech.uom.lib.common.function.QuantityConverter, java.lang.Comparable, javax.measure.Quantity
Known Direct Subclasses
tech.units.indriya.quantity.NumberQuantity, tech.units.indriya.quantity.time.TemporalQuantity, tech.units.indriya.quantity.time.TimeUnitQuantity
Annotations
@API
status:STABLE
@SuppressWarnings:unchecked
Imports
java.math.BigDecimal, java.util.Objects, javax.measure.Quantity, .Unit, javax.measure.format.QuantityFormat, javax.measure.quantity.Dimensionless, org.apiguardian.api.API, tech.units.indriya.format.SimpleQuantityFormat, .SimpleUnitFormat, tech.units.indriya.function.Calculus, tech.units.indriya.internal.function.ScaleHelper, tech.units.indriya.quantity.Quantities, tech.units.indriya.spi.NumberSystem, tech.uom.lib.common.function.UnitSupplier, .ValueSupplier

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.

Authors
Werner Keil, Andi Huber
Since
1.0
Version
2.6 Oct 6, 2024

Field Summary

Modifier and TypeField and Description
public static final Quantity<Dimensionless>
NONE

Holds a dimensionless quantity of none (exact).

public static final Quantity<Dimensionless>
ONE

Holds a dimensionless quantity of one (exact).

private final Quantity.Scale
scale

Holds the measurement scale

private static final long
private final Unit<Q>
unit

Holds the measurement unit

Constructor Summary

AccessConstructor and Description
protected
AbstractQuantity(Unit<Q>
a unit
unit
,
Quantity.Scale
the scale, absolute or relative
scale
)

Constructor.

protected
AbstractQuantity(Unit<Q>
a unit
unit
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public final <T extends Quantity<T>> ComparableQuantity<T>

Returns:

this quantity parameterized with the specified type.
asType
(Class<T>
the quantity class identifying the nature of the quantity.
type
)

Implements tech.units.indriya.ComparableQuantity.asType.

Casts this quantity to a parameterized quantity of specified nature or throw a ClassCastException if the dimension of the specified quantity and its unit's dimension do not match.

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.
compareTo
(Quantity<Q>
Quantity to which this AbstractQuantity is to be compared.
that
)

Implements java.lang.Comparable.compareTo.

Compares this quantity to the specified quantity.

public <T extends Quantity<T>, E extends Quantity<E>> ComparableQuantity<E>
divide(Quantity<T>
quantity to be multiplied
that
,
Class<E>
quantity to be converted
asTypeQuantity
)

Implements tech.units.indriya.ComparableQuantity.divide.

Multiply and cast the ComparableQuantity

public boolean

Returns:

this.getUnit.equals(obj.getUnit()) && this.getScale().equals(obj.getScale() && this.getValue().equals(obj.getValue())
equals
(Object
the object to compare with.
obj
)

Overrides java.lang.Object.equals.

Compares this quantity against the specified object for strict equality (same unit and same amount).

public Quantity.Scale

Returns:

the scale.
getScale
()

Implements javax.measure.Quantity.getScale.

Returns the absolute or relative scale.

public Unit<Q>

Returns:

the measurement unit.
getUnit
()

Implements javax.measure.Quantity.getUnit, tech.uom.lib.common.function.UnitSupplier.getUnit.

Returns the measurement unit.

public abstract Number

Returns:

the quantity value.
getValue
()

Redeclares javax.measure.Quantity.getValue, tech.uom.lib.common.function.ValueSupplier.getValue.

Returns the numeric value of the quantity.

protected boolean
hasFraction(double value)
Deprecated seems unused

protected boolean
hasFraction(BigDecimal value)
Deprecated seems unused

public int

Returns:

the hash code value.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code for this quantity.

public <T extends Quantity<T>> ComparableQuantity<T>
inverse(Class<T>
Quantity to be converted
quantityClass
)

Implements tech.units.indriya.ComparableQuantity.inverse.

invert and already cast to defined quantityClass

public boolean
isEquivalentTo(Quantity<Q>
the quantity<Q> to be compared with this instance.
that
)

Implements javax.measure.Quantity.isEquivalentTo.

Compares two instances of Quantity <Q>, performing the conversion of units if necessary.

public boolean
isGreaterThan(Quantity<Q>
the quantity<Q> to be compared with this instance.
that
)

Implements tech.units.indriya.ComparableQuantity.isGreaterThan.

Compares two instances of Quantity.

public boolean
isGreaterThanOrEqualTo(Quantity<Q>
the quantity<Q> to be compared with this instance.
that
)

Implements tech.units.indriya.ComparableQuantity.isGreaterThanOrEqualTo.

Compares two instances of Quantity, converting the unit if necessary.

public boolean
isLessThan(Quantity<Q>
the quantity<Q> to be compared with this instance.
that
)

Implements tech.units.indriya.ComparableQuantity.isLessThan.

Compares two instances of Quantity, converting the unit if necessary.

public boolean
isLessThanOrEqualTo(Quantity<Q>
the quantity<Q> to be compared with this instance.
that
)

Implements tech.units.indriya.ComparableQuantity.isLessThanOrEqualTo.

Compares two instances of Quantity, converting the unit if necessary.

public <T extends Quantity<T>, E extends Quantity<E>> ComparableQuantity<E>
multiply(Quantity<T>
quantity to be divided
that
,
Class<E>
quantity to be converted
asTypeQuantity
)

Implements tech.units.indriya.ComparableQuantity.multiply.

Divide and cast the ComparableQuantity

private NumberSystem
public static Quantity<?>

Returns:

SimpleQuantityFormat.getInstance().parse(csq)
parse
(CharSequence
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<Q>

Returns:

this quantity or a new quantity equivalent to this quantity stated in the specified unit.
to
(Unit<Q>
the unit in which the returned quantity is stated.
anotherUnit
)

Implements tech.units.indriya.ComparableQuantity.to.

Returns this quantity after conversion to specified unit.

public String

Returns:

SimpleQuantityFormat.getInstance().format(this)
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of this quantity.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

NONEback to summary
public static final Quantity<Dimensionless> NONE

Holds a dimensionless quantity of none (exact).

ONEback to summary
public static final Quantity<Dimensionless> ONE

Holds a dimensionless quantity of one (exact).

scaleback to summary
private final Quantity.Scale scale

Holds the measurement scale

serialVersionUIDback to summary
private static final long serialVersionUID
unitback to summary
private final Unit<Q> unit

Holds the measurement unit

Constructor Detail

AbstractQuantityback to summary
protected AbstractQuantity(Unit<Q> unit, Quantity.Scale scale)

Constructor.

Parameters
unit:Unit<Q>

a unit

scale:Quantity.Scale

the scale, absolute or relative

AbstractQuantityback to summary
protected AbstractQuantity(Unit<Q> unit)

Constructor. Applies ABSOLUTE Scale if none was given.

Parameters
unit:Unit<Q>

a unit

Method Detail

asTypeback to summary
public final <T extends Quantity<T>> ComparableQuantity<T> asType(Class<T> type) throws ClassCastException

Implements tech.units.indriya.ComparableQuantity.asType.

Casts this quantity to a parameterized quantity of specified nature or throw a ClassCastException 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);

Parameters
<T>

Doc from javax.measure.Quantity.asType. The type of the quantity.

type:Class<T>

the quantity class identifying the nature of the quantity.

Returns:ComparableQuantity<T>

this quantity parameterized with the specified type.

Exceptions
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)
compareToback to summary
public int compareTo(Quantity<Q> that)

Implements java.lang.Comparable.compareTo.

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 unit). If units are not the same, the unit of the specified quantity is converted.

Implementation Note

Note: this class uses implicit unit conversion that is inconsistent with equals.

Parameters
that:Quantity<Q>

Quantity to which this AbstractQuantity is to be compared.

Returns:int

a negative integer, zero, or a positive integer as this quantity is less than, equal/equivalent to, or greater than the specified Measurement quantity.

Annotations
@Override
See Also
tech.units.indriya.spi.NumberSystem#compare
divideback to summary
public <T extends Quantity<T>, E extends Quantity<E>> ComparableQuantity<E> divide(Quantity<T> that, Class<E> asTypeQuantity)

Implements tech.units.indriya.ComparableQuantity.divide.

Doc from tech.units.indriya.ComparableQuantity.divide.

Multiply and cast the ComparableQuantity

Parameters
that:Quantity<T>

quantity to be multiplied

asTypeQuantity:Class<E>

quantity to be converted

Returns:ComparableQuantity<E>

the QuantityOperations multiplied and converted

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Compares this quantity against the specified object for strict equality (same unit and same amount).

Similarly to the BigDecimal#equals method which consider 2.0 and 2.00 as different objects because of different internal scales, quantities such as Quantities.getQuantity(3.0, KILOGRAM) Quantities.getQuantity(3, KILOGRAM) and Quantities.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 or isEquivalentTo methods should be used.

Parameters
obj:Object

the object to compare with.

Returns:boolean

this.getUnit.equals(obj.getUnit()) && this.getScale().equals(obj.getScale() && this.getValue().equals(obj.getValue())

Annotations
@Override
getScaleback to summary
public Quantity.Scale getScale()

Implements javax.measure.Quantity.getScale.

Returns the absolute or relative scale.

Returns:Quantity.Scale

the scale.

Annotations
@Override
getUnitback to summary
public Unit<Q> getUnit()

Implements javax.measure.Quantity.getUnit, tech.uom.lib.common.function.UnitSupplier.getUnit.

Returns the measurement unit.

Returns:Unit<Q>

the measurement unit.

Annotations
@Override
getValueback to summary
public abstract Number getValue()

Redeclares javax.measure.Quantity.getValue, tech.uom.lib.common.function.ValueSupplier.getValue.

Returns the numeric value of the quantity.

Returns:Number

the quantity value.

Annotations
@Override
hasFractionback to summary
protected boolean hasFraction(double value)

Deprecated

seems unused

hasFractionback to summary
protected boolean hasFraction(BigDecimal value)

Deprecated

seems unused

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code for this quantity.

Returns:int

the hash code value.

Annotations
@Override
inverseback to summary
public <T extends Quantity<T>> ComparableQuantity<T> inverse(Class<T> quantityClass)

Implements tech.units.indriya.ComparableQuantity.inverse.

Doc from tech.units.indriya.ComparableQuantity.inverse.

invert and already cast to defined quantityClass

Parameters
quantityClass:Class<T>

Quantity to be converted

Annotations
@Override
isEquivalentToback to summary
public boolean isEquivalentTo(Quantity<Q> that)

Implements javax.measure.Quantity.isEquivalentTo.

Doc from javax.measure.Quantity.isEquivalentTo.

Compares two instances of Quantity <Q>, performing the conversion of units if necessary.

Parameters
that:Quantity<Q>

the quantity<Q> to be compared with this instance.

Returns:boolean

true if that ≡ this.

Annotations
@Override
isGreaterThanback to summary
public boolean isGreaterThan(Quantity<Q> that)

Implements tech.units.indriya.ComparableQuantity.isGreaterThan.

Doc from tech.units.indriya.ComparableQuantity.isGreaterThan.

Compares two instances of Quantity. Conversion of unit can happen if necessary

Parameters
that:Quantity<Q>

the quantity<Q> to be compared with this instance.

Returns:boolean

true if this > that.

Annotations
@Override
isGreaterThanOrEqualToback to summary
public boolean isGreaterThanOrEqualTo(Quantity<Q> that)

Implements tech.units.indriya.ComparableQuantity.isGreaterThanOrEqualTo.

Doc from tech.units.indriya.ComparableQuantity.isGreaterThanOrEqualTo.

Compares two instances of Quantity, converting the unit if necessary.

Parameters
that:Quantity<Q>

the quantity<Q> to be compared with this instance.

Returns:boolean

true if this >= that.

Annotations
@Override
isLessThanback to summary
public boolean isLessThan(Quantity<Q> that)

Implements tech.units.indriya.ComparableQuantity.isLessThan.

Doc from tech.units.indriya.ComparableQuantity.isLessThan.

Compares two instances of Quantity, converting the unit if necessary.

Parameters
that:Quantity<Q>

the quantity<Q> to be compared with this instance.

Returns:boolean

true if this < that.

Annotations
@Override
isLessThanOrEqualToback to summary
public boolean isLessThanOrEqualTo(Quantity<Q> that)

Implements tech.units.indriya.ComparableQuantity.isLessThanOrEqualTo.

Doc from tech.units.indriya.ComparableQuantity.isLessThanOrEqualTo.

Compares two instances of Quantity, converting the unit if necessary.

Parameters
that:Quantity<Q>

the quantity<Q> to be compared with this instance.

Returns:boolean

true if this <= that.

Annotations
@Override
multiplyback to summary
public <T extends Quantity<T>, E extends Quantity<E>> ComparableQuantity<E> multiply(Quantity<T> that, Class<E> asTypeQuantity)

Implements tech.units.indriya.ComparableQuantity.multiply.

Doc from tech.units.indriya.ComparableQuantity.multiply.

Divide and cast the ComparableQuantity

Parameters
that:Quantity<T>

quantity to be divided

asTypeQuantity:Class<E>

quantity to be converted

Returns:ComparableQuantity<E>

the QuantityOperations multiplied and converted

Annotations
@Override
numberSystemback to summary
private NumberSystem numberSystem()
parseback to summary
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.
Quantity proportion = AbstractQuantity.parse("0.234").asType(Dimensionless.class);

Note

This method handles only standard unit format. Locale-sensitive or mixed quantity parsing is currently not supported by this class. If you need flexible parsing of both single and mixed quantities, please use getQuantity() method of the Quantities facade instead.

Parameters
csq:CharSequence

the decimal value and its unit (if any) separated by space(s).

Returns:Quantity<?>

SimpleQuantityFormat.getInstance().parse(csq)

toback to summary
public ComparableQuantity<Q> to(Unit<Q> anotherUnit)

Implements tech.units.indriya.ComparableQuantity.to.

Returns this quantity after conversion to specified unit. The default implementation returns NumberQuantity.of(doubleValue(unit), unit) . If this quantity is already stated in the specified unit, then this quantity is returned and no conversion is performed.

Parameters
anotherUnit:Unit<Q>

the unit in which the returned quantity is stated.

Returns:ComparableQuantity<Q>

this quantity or a new quantity equivalent to this quantity stated in the specified unit.

Annotations
@Override
Exceptions
ArithmeticException:
if the result is inexact and the quotient has a non-terminating decimal expansion.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String 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 the QuantityFormat implementations and its subclasses.

Returns:String

SimpleQuantityFormat.getInstance().format(this)

Annotations
@Override