Package org.djunits.quantity.def
Class Quantity<Q extends Quantity<Q,U>,U extends UnitInterface<U,Q>>
java.lang.Object
java.lang.Number
org.djunits.quantity.def.Quantity<Q,U>
- Type Parameters:
Q- the quantity typeU- the unit type
- All Implemented Interfaces:
Serializable,Comparable<Q>,Additive<Q>,Scalable<Q>,Value<U,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,U>,U extends UnitInterface<U,Q>>
extends Number
implements Value<U,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:
-
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,TU>, TU extends UnitInterface<TU, TQ>>
TQas(TU targetUnit) Return 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.booleanfloatstatic Stringformat(double d) Format a string according to the current locale and the standard (minimized) format, such as "3.14" or "300.0".static StringFormat a string according to the current locale and the provided format string.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()abstract Qinstantiate(double siValue) Instantiate a quantity with an SI or base value.instantiate(double value, U unit) Instantiate a quantity with a value and a unit.static <Q extends Quantity<Q,U>, U extends UnitInterface<U, 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,U>, U extends UnitInterface<U, Q>>
Qmax(Q quantity1, Q... quantities) Return the maximum value of one or more quantities.static <Q extends Quantity<Q,U>, U extends UnitInterface<U, Q>>
Qmean(Q quantity1, Q... quantities) Return the mean of one or more quantities.static <Q extends Quantity<Q,U>, U extends UnitInterface<U, 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,U>, U extends UnitInterface<U, 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(U 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,U>, U extends UnitInterface<U, Q>>
Qsum(Q quantity1, Q... quantities) Return the sum of one or more quantities.Concise display description of this value, without the engineering formatting, so without trailing zeroes.toDisplayString(U displayUnit) Concise display description of this value, without the engineering formatting, so without trailing zeroes.toString()Concise description of this value.toString(boolean verbose, boolean withUnit) Somewhat verbose description of this value with optional type and unit information.Somewhat verbose description of this value with the values expressed in the specified unit.Somewhat verbose description of this value with the values expressed in the specified unit.Format this DoubleScalar in SI unit using prefixes when possible.toStringSIPrefixed(int smallestPower, int biggestPower) Format this DoubleScalar in SI unit using prefixes when possible and within the specified size range.Concise textual representation of this value, without the engineering formatting, so without trailing zeroes.toTextualString(U displayUnit) Concise textual representation of this value, without the engineering formatting, so without trailing zeroes.static <Q extends Quantity<Q,U>, U extends UnitInterface<U, 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
-
Constructor Details
-
Quantity
Instantiate a quantity with a value and a display unit.- Parameters:
value- the value expressed in the display unitdisplayUnit- the display unit to use
-
-
Method Details
-
getDisplayUnit
Description copied from interface:ValueRetrieve the unit of this Value.- Specified by:
getDisplayUnitin interfaceValue<Q extends Quantity<Q,U>, U extends UnitInterface<U, Q>> - Returns:
- 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.- Specified by:
setDisplayUnitin interfaceValue<Q extends Quantity<Q,U>, U extends UnitInterface<U, Q>> - Parameters:
newUnit- the new display unit of this value- Returns:
- 'this' for fluent design
-
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
-
instantiate
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:
value- the double value, experessed 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
-
hashCode
public int hashCode() -
equals
-
valueOf
public static <Q extends Quantity<Q,U>, Q valueOfU extends UnitInterface<U, Q>> (String text, Q example) 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 typeU- the unit 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
public static <Q extends Quantity<Q,U>, Q ofU extends UnitInterface<U, Q>> (double value, String unitString, Q example) Returns a quantity based on a value and the textual representation of the unit, which can be localized.- Type Parameters:
Q- the quantity typeU- the unit type- Parameters:
value- the value to useunitString- 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
-
format
Format a string according to the current locale and the standard (minimized) format, such as "3.14" or "300.0".- Parameters:
d- the number to format- Returns:
- the formatted number using the current Locale
-
format
Format a string according to the current locale and the provided format string.- Parameters:
d- the number to formatformat- the formatting string to use for the number- Returns:
- the formatted number using the current Locale and the format string
-
toString
Concise description of this value. -
toString
Somewhat verbose description of this value with the values expressed in the specified unit. -
toString
Somewhat verbose description of this value with optional type and unit information.- Parameters:
verbose- if true; include type info; if false; exclude type infowithUnit- if true; include the unit; of false; exclude the unit- Returns:
- printable string with the value contents
-
toString
Somewhat verbose description of this value with the values expressed in the specified unit.- Parameters:
displayUnit- the unit into which the values are converted for displayverbose- if true; include type info; if false; exclude type infowithUnit- if true; include the unit; of false; exclude the unit- Returns:
- printable string with the value contents
-
toStringSIPrefixed
Format this DoubleScalar in SI unit using prefixes when possible. If the value is too small or too large, e-notation and the plain SI unit are used.- Returns:
- formatted value of this DoubleScalar
-
toStringSIPrefixed
Format this DoubleScalar in SI unit using prefixes when possible and within the specified size range. If the value is too small or too large, e-notation and the plain SI unit are used.- Parameters:
smallestPower- the smallest exponent value that will be written using an SI prefixbiggestPower- the largest exponent value that will be written using an SI prefix- Returns:
- formatted value of this DoubleScalar
-
toTextualString
Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is added between the number and the unit.- Returns:
- a String with the value with the default textual representation of the unit attached.
-
toTextualString
Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is added between the number and the unit.- Parameters:
displayUnit- the display unit for the value- Returns:
- a String with the value with the default textual representation of the provided unit attached.
-
toDisplayString
Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is added between the number and the unit.- Returns:
- a String with the value with the default display representation of the unit attached.
-
toDisplayString
Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is added between the number and the unit.- Parameters:
displayUnit- the display unit for the value- Returns:
- a String with the value with the default display representation of the provided unit attached.
-
interpolate
public static <Q extends Quantity<Q,U>, Q interpolateU extends UnitInterface<U, Q>> (Q zero, Q one, double ratio) Interpolate between two quantities. Note that the first quantities does not have to be smaller than the second.- Type Parameters:
Q- the quantity typeU- the unit 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
@SafeVarargs public static <Q extends Quantity<Q,U>, Q maxU extends UnitInterface<U, Q>> (Q quantity1, Q... quantities) Return the maximum value of one or more quantities.- Type Parameters:
Q- the quantity typeU- the unit type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the maximum value of the quantities
-
min
@SafeVarargs public static <Q extends Quantity<Q,U>, Q minU extends UnitInterface<U, Q>> (Q quantity1, Q... quantities) Return the minimum value of one or more quantities.- Type Parameters:
Q- the quantity typeU- the unit type- Parameters:
quantity1- the first quantityquantities- the other quantities- Returns:
- the minimum value of more than two quantities
-
sum
@SafeVarargs public static <Q extends Quantity<Q,U>, Q sumU extends UnitInterface<U, Q>> (Q quantity1, Q... quantities) Return the sum of one or more quantities.- Type Parameters:
Q- the quantity typeU- the unit 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
@SafeVarargs public static <Q extends Quantity<Q,U>, Q meanU extends UnitInterface<U, Q>> (Q quantity1, Q... quantities) Return the mean of one or more quantities.- Type Parameters:
Q- the quantity typeU- the unit 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
public <TQ extends Quantity<TQ,TU>, TQ asTU extends UnitInterface<TU, TQ>> (TU targetUnit) throws IllegalArgumentException 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 typeTU- target unit 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.- Specified by:
isRelativein interfaceValue<Q extends Quantity<Q,U>, U extends UnitInterface<U, Q>> - Returns:
- whether this is a Relative Value
-