Quantity<T extends Unit<T> > class
abstract
An abstract representation of a physical quantity, encapsulating a numerical value and a specific unit of measurement.
This class serves as the foundation for all specific quantity types (e.g., Length
,
Pressure
, Temperature
). It enforces a common interface for unit conversions,
comparisons, and string formatting.
Immutability
Quantity
objects are immutable. Operations that might seem to modify a quantity,
such as unit conversion (convertTo
) or arithmetic, always return a new Quantity
instance with the updated value or unit, leaving the original instance unchanged.
This promotes safer and more predictable code.
Type Parameter T
The type parameter T
represents the specific Unit
enum associated with this
quantity type. For example, a Pressure
quantity would use PressureUnit
for T
,
so it would be declared as class Pressure extends Quantity<PressureUnit>
.
Comparison
Quantity
implements Comparable<Quantity<T>>
, allowing quantities of the
same type (e.g., two Length
objects) to be compared based on their physical
magnitude, even if their internal units differ. The compareTo
method handles
necessary conversions for accurate comparison.
- Implemented types
-
- Comparable<
Quantity< T> >
- Comparable<
- Implementers
- Annotations
-
- @immutable
Constructors
- Quantity.new(double _value, T _unit)
-
Creates a new
Quantity
with a given numerical value and its corresponding unit.const
Properties
- hashCode → int
-
Returns a hash code for this
Quantity
instance.no setteroverride - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- unit → T
-
Returns the unit of measurement associated with this quantity's original value.
no setter
- value → double
-
Returns the numerical value of this quantity in its original unit.
no setter
Methods
-
compareTo(
Quantity< T> other) → int -
Compares this quantity to another Quantity of the same type (
T
).override -
convertTo(
T targetUnit) → Quantity< T> -
Creates a new
Quantity
instance of the same type, with its value converted to the specifiedtargetUnit
. -
getValue(
T targetUnit) → double -
Converts this quantity's value to the specified
targetUnit
and returns the numerical result of this conversion. -
isEquivalentTo(
Quantity< T> other) → bool - Checks if this quantity has the same physical magnitude as another.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
{T? targetUnit, int? fractionDigits, bool showUnitSymbol = true, String unitSymbolSeparator = '\u00A0', String? locale, NumberFormat? numberFormat}) → String -
Returns a string representation of this quantity, with options for
formatting and unit conversion.
override
Operators
-
operator <(
Quantity< T> other) → bool - Checks if this quantity's magnitude is less than another's.
-
operator <=(
Quantity< T> other) → bool - Checks if this quantity's magnitude is less than or equal to another's.
-
operator ==(
Object other) → bool -
Determines whether this Quantity is equal to another Object.
override
-
operator >(
Quantity< T> other) → bool - Checks if this quantity's magnitude is greater than another's.
-
operator >=(
Quantity< T> other) → bool - Checks if this quantity's magnitude is greater than or equal to another's.