Interface Quantity<Q extends Quantity<Q>>
- Type Parameters:
Q- The type of the quantity.
- All Known Subinterfaces:
Acceleration,AmountOfSubstance,Angle,Area,CatalyticActivity,Dimensionless,ElectricCapacitance,ElectricCharge,ElectricConductance,ElectricCurrent,ElectricInductance,ElectricPotential,ElectricResistance,Energy,Force,Frequency,Illuminance,Length,LuminousFlux,LuminousIntensity,MagneticFlux,MagneticFluxDensity,Mass,Power,Pressure,RadiationDoseAbsorbed,RadiationDoseEffective,Radioactivity,SolidAngle,Speed,Temperature,Time,Volume
Mass,
time, distance, heat, and angular separation are among the familiar examples
of quantitative properties.
Unit<Mass> pound = ... Quantity<Length> size = ... Sensor<Temperature>
thermometer = ... Vector3D<Speed> aircraftSpeed = ...
- Arithmetic operations
Quantity
instances. All implementations shall produce equivalent results for
the same operation applied on equivalent quantities. Two quantities are
equivalent if, after conversion to the same unit of measurement, they have
the same numerical value (ignoring rounding errors). For example 2000 metres
is equivalent to 2 km, but 2°C is not equivalent to 2 K; it is equivalent to
275.15 K instead. Above requirement applied to addition means that 2°C + 2 K
shall be equivalent to 275.15 K + 2 K.
All operations shall preserve the basic laws of algebra, in particular commutativity of addition and multiplication (A + B = B + A) and associativity of addition and multiplication (A + B) + C = A + (B + C). In order to preserve those algebra laws, this specification requires all arithmetic operations to execute as is all operands were converted to system unit before the operation is carried out, and the result converted back to any compatible unit at implementation choice. For example 4 cm + 1 inch shall produce any result equivalent to 0.04 m + 0.0254 m.
Implementations are allowed to avoid conversion to system unit if the result is guaranteed to be equivalent. This is often the case when the conversion between quantity unit and system unit is only a scale factor. However this is not the case for conversions applying an offset or more complex formula. For example 2°C + 1°C = 274.15°C, not 3°C. This counter-intuitive result is essential for preserving algebra laws like associativity, and is also the expected result from a thermodynamic point of view.
- API Note:
- This interface places no restrictions on the mutability of
implementations, however immutability is strongly recommended. All
implementations must be
Comparable.
- Since:
- 1.0
- Version:
- 2.5, May 20, 2023
- Author:
- Jean-Marie Dautelle, Martin Desruisseaux, Werner Keil, Otavio Santana
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enum -
Method Summary
Modifier and TypeMethodDescriptionReturns the sum of thisQuantitywith the one specified.Casts this quantity to a parameterized unit of specified nature or throw aClassCastExceptionif the dimension of the specified quantity and this measure unit's dimension do not match.Returns the quotient of thisQuantitydivided by theNumberspecified.Quantity<?>Returns the quotient of thisQuantitydivided by theQuantityspecified.getScale()Returns theScaleof thisQuantity, if it's absolute or relative.getUnit()Returns the unit of thisQuantity.getValue()Returns the value of thisQuantity.Quantity<?>inverse()Returns aQuantitythat is the multiplicative inverse of thisQuantity, having reciprocal value and reciprocal unit as given bythis.getUnit().inverse().booleanisEquivalentTo(Quantity<Q> that) Compares two instances ofQuantity <Q>, performing the conversion of units if necessary.Returns the product of thisQuantitywith theNumbervalue specified.Quantity<?>Returns the product of thisQuantitywith the one specified.negate()Returns aQuantitywhose value is(-this.getValue()).Returns the difference between thisQuantityand the one specified.Returns thisQuantityconverted into another (compatible)Unit.Convenient method equivalent toto(getUnit().toSystemUnit()).
-
Method Details
-
add
Returns the sum of thisQuantitywith the one specified. The result shall be as if this quantity and the given addend were converted to system unit before to be added, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
addend- theQuantityto be added.- Returns:
this + addend.
-
subtract
Returns the difference between thisQuantityand the one specified. The result shall be as if this quantity and the given subtrahend were converted to system unit before to be subtracted, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
subtrahend- theQuantityto be subtracted.- Returns:
this - subtrahend.
-
divide
Returns the quotient of thisQuantitydivided by theQuantityspecified. The result shall be as if this quantity and the given divisor were converted to system unit before to be divided, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
divisor- theQuantitydivisor.- Returns:
this / divisor.- Throws:
ClassCastException- if the type of an element in the specified operation is incompatible with this quantity
-
divide
Returns the quotient of thisQuantitydivided by theNumberspecified. The result shall be as if this quantity was converted to system unit before to be divided, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
divisor- theNumberdivisor.- Returns:
this / divisor.
-
multiply
Returns the product of thisQuantitywith the one specified. The result shall be as if this quantity and the given multiplicand were converted to system unit before to be multiplied, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
multiplicand- theQuantitymultiplicand.- Returns:
this * multiplicand.- Throws:
ClassCastException- if the type of an element in the specified operation is incompatible with this quantity
-
multiply
Returns the product of thisQuantitywith theNumbervalue specified. The result shall be as if this quantity was converted to system unit before to be multiplied, and the result converted back to the unit of this quantity or any other compatible unit at implementation choice.- Parameters:
multiplicand- theNumbermultiplicand.- Returns:
this * multiplicand.
-
to
Returns thisQuantityconverted into another (compatible)Unit.- Parameters:
unit- theUnit unitin 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.
-
inverse
Returns aQuantitythat is the multiplicative inverse of thisQuantity, having reciprocal value and reciprocal unit as given bythis.getUnit().inverse().- Returns:
- reciprocal
Quantity - See Also:
-
negate
Returns aQuantitywhose value is(-this.getValue()).- Returns:
-this.
-
asType
Casts this quantity to a parameterized unit of specified nature or throw aClassCastExceptionif the dimension of the specified quantity and this measure unit's dimension do not match. For example:Quantity<Length> length = Quantities.getQuantity("2 km").asType(Length.class);orQuantity<Speed> C = length.multiply(299792458).divide(second).asType(Speed.class);- Type Parameters:
T- The type of the quantity.- 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 SI unit for the quantity.- See Also:
-
getValue
Returns the value of thisQuantity.- Returns:
- a value.
-
getUnit
Returns the unit of thisQuantity.- Returns:
- the unit (shall not be
null).
-
toSystemUnit
Convenient method equivalent toto(getUnit().toSystemUnit()).- Returns:
- this quantity or a new quantity equivalent to this quantity stated in SI units.
- Throws:
ArithmeticException- if the result is inexact and the quotient has a non-terminating decimal expansion.
-
getScale
Returns theScaleof thisQuantity, if it's absolute or relative.- Returns:
- the scale, if it's an absolute or relative quantity.
- Since:
- 2.0
- See Also:
-
isEquivalentTo
Compares two instances ofQuantity <Q>, performing the conversion of units if necessary.- Parameters:
that- thequantity<Q>to be compared with this instance.- Returns:
trueifthat ≡ this.- Throws:
NullPointerException- if the quantity is null- Since:
- 2.1
- See Also:
-