Package org.djunits.quantity.def
Class Quantity<Q extends Quantity<Q>>
java.lang.Object
java.lang.Number
org.djunits.quantity.def.Quantity<Q>
- Type Parameters:
Q- the quantity type
- All Implemented Interfaces:
Serializable,Comparable<Q>,Additive<Q>,Scalable<Q>,Value<Q,Q>
- Direct Known Subclasses:
AbsorbedDose,Acceleration,AmountOfSubstance,Angle,AngularAcceleration,AngularVelocity,Area,ArealObjectDensity,CatalyticActivity,Density,Dimensionless,Duration,ElectricalCapacitance,ElectricalConductance,ElectricalInductance,ElectricalResistance,ElectricCharge,ElectricCurrent,ElectricPotential,Energy,EquivalentDose,FlowMass,FlowVolume,Force,Frequency,Illuminance,Length,LinearDensity,LinearObjectDensity,LuminousFlux,LuminousIntensity,MagneticFlux,MagneticFluxDensity,Mass,Momentum,Power,Pressure,RadioActivity,SIQuantity,SolidAngle,Speed,TemperatureDifference,Torque,Volume,VolumetricObjectDensity
public abstract class Quantity<Q extends Quantity<Q>>
extends Number
implements Value<Q,Q>, Comparable<Q>, Additive<Q>, Scalable<Q>
Quantity is an abstract class that stores the basic information about a quantity. A physical quantity can be expressed as a
value, which is the combination of a numerical value and a unit of measurement. The type of physical quantity is encoded in
the class (Length, Speed, Area, etc.) with its associated (base) unit of measurement, whereas the numerical value is stored
in the si field. Additionally, each quantity has a displayUnit that gives the preference for the (scaled) display of the
quantity, e.g., in a toString() method.
Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.
- Author:
- Alexander Verbraeck
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabs()Make the SI-values of this element absolute.Add an element to this element, based on the SI-values.<TQ extends Quantity<TQ>>
TQReturn the quantity 'as' a known quantity, using a unit to express the result in.final intDivide this quantity by another quantity, and return a SIQuantity as the result.doublebooleanTest if this Quantity is equal to another Quantity.booleaneq0()Test if this Quantity is equal to 0.0.booleanfloatformat()Concise description of this quantity.format(QuantityFormat format) String representation of this quantity after applying the format.String representation of this quantity, expressed in the specified unit.booleanTest if this Quantity is greater than or equal to another Quantity.booleange0()Test if this Quantity is greater than or equal to 0.0.Retrieve the unit of this Value.final doubleRetrieve the value in the current display unit.final doubleRetrieve the value converted into some specified unit.getName()Return the "pretty" name of the quantity.booleanTest if this Quantity is greater than another Quantity.booleangt0()Test if this Quantity is greater than 0.0.inthashCode()instantiate(double valueInUnit, Unit<?, Q> unit) Instantiate a quantity with a value and a unit.abstract QinstantiateSi(double siValue) Instantiate a quantity with an SI or base value.static <Q extends Quantity<Q>>
Qinterpolate(Q zero, Q one, double ratio) Interpolate between two quantities.intintValue()booleanIndicate whether this is a Relative Value.booleanTest if this Quantity is less than or equal to another Quantity.booleanle0()Test if this Quantity is less than or equal to 0.0.longbooleanTest if this Quantity is less than another Quantity.booleanlt0()Test if this Quantity is less than 0.0.static <Q extends Quantity<Q>>
Qmax(Q quantity1, Q... quantities) Return the maximum value of one or more quantities.static <Q extends Quantity<Q>>
Qmean(Q quantity1, Q... quantities) Return the mean of one or more quantities.static <Q extends Quantity<Q>>
Qmin(Q quantity1, Q... quantities) Return the minimum value of one or more quantities.Multiply this quantity with another quantity, and return a SIQuantity as the result.booleanTest if this Quantity is not equal to another Quantity.booleanne0()Test if this Quantity is not equal to 0.0.negate()Negate this element, based on the SI-values.static <Q extends Quantity<Q>>
QReturns a quantity based on a value and the textual representation of the unit, which can be localized.static SIQuantityReturn the product of one or more quantities.Quantity<?>Return the reciprocal of this quantity (1/q).scaleBy(double factor) Scale the element by a dimensionless factor, based on the SI-values.setDisplayUnit(Unit<?, Q> newUnit) Set a new display unit for the value.doublesi()Return the SI value of the quantity.siUnit()Return the SI unit of this quantity.Subtract an element from this element, based on the SI-values.static <Q extends Quantity<Q>>
Qsum(Q quantity1, Q... quantities) Return the sum of one or more quantities.toString()Description of this quantity with default formatting.static <Q extends Quantity<Q>>
QReturns a quantity for the textual representation of a value with a unit.Methods inherited from class java.lang.Number
byteValue, shortValueMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djunits.value.Value
isAbsolute, setDisplayUnit
-
Field Details
-
si
public final double siThe si value.
-
-
Constructor Details
-
Quantity
Instantiate a quantity with a value and a display unit.- Parameters:
valueInUnit- the value expressed in the display unitunit- the display unit to use
-
-
Method Details
-
getDisplayUnit
Description copied from interface:ValueRetrieve the unit of this Value. -
setDisplayUnit
Description copied from interface:ValueSet a new display unit for the value. Internally, the value will not changed since it is stored in a base unit. -
getInUnit
public final double getInUnit()Retrieve the value in the current display unit.- Returns:
- the value in the current display unit
-
getInUnit
Retrieve the value converted into some specified unit.- Parameters:
targetUnit- the unit to convert the value into- Returns:
- the double value of this quantity expressed in the target unit
-
getName
Return the "pretty" name of the quantity.- Returns:
- the "pretty" name of the quantity
-
siUnit
Return the SI unit of this quantity.- Returns:
- the SI unit of this quantity
-
si
public double si()Return the SI value of the quantity.- Returns:
- the SI value of the quantity
-
instantiateSi
Instantiate a quantity with an SI or base value.- Parameters:
siValue- the value expressed in the base (SI) unit- Returns:
- a quantity with the given SI-value and base (SI) unit
-
instantiate
Instantiate a quantity with a value and a unit.- Parameters:
valueInUnit- the double value, expressed in the unitunit- the unit- Returns:
- a quantity with the given value and display unit
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classNumber
-
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValuein classNumber
-
lt
Test if this Quantity is less than another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is less than o; false otherwise
-
le
Test if this Quantity is less than or equal to another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is less than or equal to o; false otherwise
-
gt
Test if this Quantity is greater than another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is greater than o; false otherwise
-
ge
Test if this Quantity is greater than or equal to another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is greater than or equal to o; false otherwise
-
eq
Test if this Quantity is equal to another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is equal to o; false otherwise
-
ne
Test if this Quantity is not equal to another Quantity.- Parameters:
other- the right hand side operand of the comparison- Returns:
- true if this is not equal to o; false otherwise
-
lt0
public boolean lt0()Test if this Quantity is less than 0.0.- Returns:
- true if this is less than 0.0; false if this is not less than 0.0
-
le0
public boolean le0()Test if this Quantity is less than or equal to 0.0.- Returns:
- true if this is less than or equal to 0.0; false if this is not less than or equal to 0.0
-
gt0
public boolean gt0()Test if this Quantity is greater than 0.0.- Returns:
- true if this is greater than 0.0; false if this is not greater than 0.0
-
ge0
public boolean ge0()Test if this Quantity is greater than or equal to 0.0.- Returns:
- true if this is greater than or equal to 0.0; false if this is not greater than or equal to 0.0
-
eq0
public boolean eq0()Test if this Quantity is equal to 0.0.- Returns:
- true if this is equal to 0.0; false if this is not equal to 0.0
-
ne0
public boolean ne0()Test if this Quantity is not equal to 0.0.- Returns:
- true if this is not equal to 0.0; false if this is equal to 0.0
-
compareTo
- Specified by:
compareToin interfaceComparable<Q extends Quantity<Q>>
-
hashCode
public int hashCode() -
equals
-
valueOf
Returns a quantity for the textual representation of a value with a unit. The String representation that can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed, but not required, between the value and the unit.- Type Parameters:
Q- the quantity type- Parameters:
text- the textual representation to parse into the quantityexample- an example instance to deliver- Returns:
- the quantity representation of the value with its unit
- Throws:
IllegalArgumentException- when the text cannot be parsedNullPointerException- when the text argument is null
-
of
Returns a quantity based on a value and the textual representation of the unit, which can be localized.- Type Parameters:
Q- the quantity type- Parameters:
valueInUnit- the value, expressed in the unit as given by unitStringunitString- the textual representation of the unitexample- an example instance to deliver- Returns:
- the quantity representation of the value in its unit
- Throws:
IllegalArgumentException- when the unit cannot be parsed or is incorrectNullPointerException- when the unitString argument is null
-
toString
Description of this quantity with default formatting. -
format
Concise description of this quantity. -
format
String representation of this quantity after applying the format.- Parameters:
format- the format to apply for the quantity- Returns:
- a String representation of this quantity, formatted according to the given format
-
format
String representation of this quantity, expressed in the specified unit. -
interpolate
Interpolate between two quantities. Note that the first quantities does not have to be smaller than the second.- Type Parameters:
Q- the quantity type- Parameters:
zero- the quantity at a ratio of zeroone- the quantity at a ratio of oneratio- the ratio between 0 and 1, inclusive- Returns:
- a Quantity at the given ratio between 0 and 1
-
max
Return the maximum value of one or more quantities.- Type Parameters:
Q- the quantity type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the maximum value of the quantities
-
min
Return the minimum value of one or more quantities.- Type Parameters:
Q- the quantity type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the minimum value of more than two quantities
-
sum
Return the sum of one or more quantities.- Type Parameters:
Q- the quantity type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the sum of the quantities
-
product
Return the product of one or more quantities.- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the product of the quantities
-
mean
Return the mean of one or more quantities.- Type Parameters:
Q- the quantity type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the mean of the quantities
-
add
Description copied from interface:AdditiveAdd an element to this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales: 10 °C + 5 °C = 288.15 °C and not 15 °C. -
subtract
Description copied from interface:AdditiveSubtract an element from this element, based on the SI-values. -
abs
Description copied from interface:AdditiveMake the SI-values of this element absolute. A new element wil be returned. Note that this can lead to unwanted effects for elements with Offset scales. A temperature of -10 degrees Celsius will have its Kelvin temperatures negated. -
negate
Description copied from interface:AdditiveNegate this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. A temperature in Celsius that is negated, will have its Kelvin temperatures negated. -
scaleBy
Description copied from interface:ScalableScale the element by a dimensionless factor, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. 2 * 10 °C = 293.15 °C and not 20 °C. -
multiply
Multiply this quantity with another quantity, and return a SIQuantity as the result.- Parameters:
quantity- the quantity to multiply with- Returns:
- the multiplication of this quantity and the given quantity
-
divide
Divide this quantity by another quantity, and return a SIQuantity as the result.- Parameters:
quantity- the quantity to divide by- Returns:
- the division of this quantity and the given quantity
-
reciprocal
Return the reciprocal of this quantity (1/q).- Returns:
- the reciprocal of this quantity, with the correct SI units
-
as
Return the quantity 'as' a known quantity, using a unit to express the result in. Throw a Runtime exception when the SI units of this quantity and the target quantity do not match.- Type Parameters:
TQ- target quantity type- Parameters:
targetUnit- the unit to convert the quantity to- Returns:
- a quantity typed in the target quantity class
- Throws:
IllegalArgumentException- when the units do not match
-
isRelative
public boolean isRelative()Description copied from interface:ValueIndicate whether this is a Relative Value.
-