Package org.openhab.core.library.types
Class QuantityType<T extends javax.measure.Quantity<T>>
java.lang.Object
java.lang.Number
org.openhab.core.library.types.QuantityType<T>
- All Implemented Interfaces:
Serializable
,Comparable<QuantityType<T>>
,Command
,PrimitiveType
,State
,Type
@NonNullByDefault({PARAMETER,RETURN_TYPE,FIELD,TYPE_ARGUMENT})
public class QuantityType<T extends javax.measure.Quantity<T>>
extends Number
implements PrimitiveType, State, Command, Comparable<QuantityType<T>>
The measure type extends DecimalType to handle physical unit measurement
- Author:
- Gaël L'hopital - Initial contribution
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final QuantityType<javax.measure.quantity.Dimensionless>
static final QuantityType<javax.measure.quantity.Dimensionless>
-
Constructor Summary
ConstructorDescriptionCreates a dimensionlessQuantityType
with scalar 0 and unitAbstractUnit.ONE
.QuantityType
(Number value, javax.measure.Unit<T> unit) Creates a newQuantityType
with the given value andUnit
.QuantityType
(String value) Creates a newQuantityType
with the given value.QuantityType
(String value, Locale locale) Creates a newQuantityType
with the given value. -
Method Summary
Modifier and TypeMethodDescriptionadd
(QuantityType<T> state) Returns the sum of the givenQuantityType
with this QuantityType.<U extends State>
@Nullable UConvert thisState
's value into another typeint
compareTo
(QuantityType<T> o) QuantityType<?>
divide
(BigDecimal value) Divide this QuantityType by the given value.QuantityType<?>
divide
(QuantityType<?> state) Divide this QuantityType by the givenQuantityType
.double
boolean
float
Formats the value of this type according to a pattern (seeFormatter
).javax.measure.Dimension
javax.measure.Unit<T>
getUnit()
int
hashCode()
int
intValue()
QuantityType<?>
inverse()
Return the reciprocal of this QuantityType.long
QuantityType<?>
multiply
(BigDecimal value) Multiply thisQuantityType
by the given value.QuantityType<?>
multiply
(QuantityType<?> state) Multiply this QuantityType by the givenQuantityType
.negate()
Negates the value of this QuantityType leaving its unit untouched.offset
(QuantityType<T> offset, javax.measure.Unit<T> unit) Apply a given offset to this QuantityTypesubtract
(QuantityType<T> state) Subtract the givenQuantityType
from this QuantityType.Get a string representation that contains the whole internal representation of the type.@Nullable QuantityType<?>
toInvertibleUnit
(String targetUnit) @Nullable QuantityType<?>
toInvertibleUnit
(javax.measure.Unit<?> targetUnit) Convert this QuantityType to a newQuantityType
using the given target unit.toString()
@Nullable QuantityType<T>
@Nullable QuantityType<T>
toUnit
(javax.measure.Unit<?> targetUnit) Convert this QuantityType to a newQuantityType
using the given target unit.@Nullable QuantityType<T>
toUnitRelative
(String targetUnit) @Nullable QuantityType<T>
toUnitRelative
(javax.measure.Unit<T> targetUnit) Convert this QuantityType to a newQuantityType
using the given target unit.static <T extends javax.measure.Quantity<T>>
QuantityType<T>valueOf
(double value, javax.measure.Unit<T> unit) Static access toQuantityType(java.lang.Number,javax.measure.Unit<T>)
.static QuantityType<? extends javax.measure.Quantity<?>>
Static access toQuantityType(String)
.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
-
ONE
-
-
Constructor Details
-
QuantityType
public QuantityType()Creates a dimensionlessQuantityType
with scalar 0 and unitAbstractUnit.ONE
. A default constructor is needed byItemUpdater.receiveUpdate(ItemStateEvent)
) -
QuantityType
Creates a newQuantityType
with the given value. The value may contain a unit. The specificQuantity
is obtained byNumberDelimiterQuantityFormat.parse(CharSequence)
. The English locale is used to determine (decimal/grouping) separator characters.- Parameters:
value
- the non null value representing a quantity with an optional unit.- Throws:
NumberFormatException
- when a quantity without a unit could not be parsedIllegalArgumentException
- when a quantity with a unit could not be parsed
-
QuantityType
Creates a newQuantityType
with the given value. The value may contain a unit. The specificQuantity
is obtained byNumberDelimiterQuantityFormat.parse(CharSequence)
.- Parameters:
value
- the non null value representing a quantity with an optional unit.locale
- the locale used to determine (decimal/grouping) separator characters.- Throws:
NumberFormatException
- when a quantity without a unit could not be parsedIllegalArgumentException
- when a quantity with a unit could not be parsed
-
QuantityType
Creates a newQuantityType
with the given value andUnit
.- Parameters:
value
- the non null measurement value.unit
- the non null measurement unit.
-
-
Method Details
-
valueOf
public static <T extends javax.measure.Quantity<T>> QuantityType<T> valueOf(double value, javax.measure.Unit<T> unit) Static access toQuantityType(java.lang.Number,javax.measure.Unit<T>)
.- Parameters:
value
- the non-null measurement value.unit
- the non-null measurement unit.- Returns:
- a new
QuantityType
-
toString
-
valueOf
Static access toQuantityType(String)
.- Parameters:
value
- the non null value representing a quantity with an optional unit- Returns:
- a new
QuantityType
- Throws:
NumberFormatException
- when a quantity without a unit could not be parsedIllegalArgumentException
- when a quantity with a unit could not be parsed
-
equals
-
compareTo
- Specified by:
compareTo
in interfaceComparable<T extends javax.measure.Quantity<T>>
-
getUnit
-
getDimension
public javax.measure.Dimension getDimension() -
toUnit
Convert this QuantityType to a newQuantityType
using the given target unit.- Parameters:
targetUnit
- the unit to which thisQuantityType
will be converted to.- Returns:
- the new
QuantityType
in the givenUnit
ornull
in case of an error.
-
toUnit
-
toInvertibleUnit
Convert this QuantityType to a newQuantityType
using the given target unit. Implicit conversions using inverse units are allowed (i.e.mired <=> Kelvin
). This may change the dimension.- Parameters:
targetUnit
- the unit to which thisQuantityType
will be converted to.- Returns:
- the new
QuantityType
in the givenUnit
ornull
in case of an error.
-
toInvertibleUnit
-
toUnitRelative
Convert this QuantityType to a newQuantityType
using the given target unit. Similar totoUnit(javax.measure.Unit<?>)
, except that it treats the values as relative instead of absolute. This means that any offsets in the conversion of absolute values are ignored. This is useful when your quantity represents a delta, and not necessarily a measured value itself. For example, 32 °F, when converted with toUnit to Celsius, it will become 0 °C. But when converted with toUnitRelative, it will become 17.8 °C.- Parameters:
targetUnit
- the unit to which thisQuantityType
will be converted to.- Returns:
- the new
QuantityType
in the givenUnit
ornull
in case of an error.
-
toUnitRelative
-
toBigDecimal
-
hashCode
public int hashCode() -
format
Description copied from interface:Type
Formats the value of this type according to a pattern (seeFormatter
). -
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValue
in classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValue
in classNumber
-
toFullString
Description copied from interface:Type
Get a string representation that contains the whole internal representation of the type.The returned string could be consumed by the static 'valueOf(String)' method of the respective type to build a new type that is equal to this type.
- Specified by:
toFullString
in interfaceType
- Returns:
- a full string representation of the type to be consumed by 'valueOf(String)'
-
as
Description copied from interface:State
Convert thisState
's value into another type -
add
Returns the sum of the givenQuantityType
with this QuantityType.- Parameters:
state
- theQuantityType
to add to this QuantityType.- Returns:
- the sum of the given
QuantityType
with this QuantityType.
-
negate
Negates the value of this QuantityType leaving its unit untouched.- Returns:
- the negated value of this QuantityType.
-
subtract
Subtract the givenQuantityType
from this QuantityType.- Parameters:
state
- theQuantityType
to subtract from this QuantityType.- Returns:
- the difference by subtracting the given
QuantityType
from this QuantityType.
-
multiply
Multiply thisQuantityType
by the given value.- Parameters:
value
- the value thisQuantityType
should be multiplied with.- Returns:
- the product of the given value with this
QuantityType
.
-
multiply
Multiply this QuantityType by the givenQuantityType
.- Parameters:
state
- theQuantityType
with which this QuantityType should be multiplied with.- Returns:
- the product of the given
QuantityType
and this QuantityType.
-
divide
Divide this QuantityType by the given value.- Parameters:
value
- the value thisQuantityType
should be divided by.- Returns:
- the quotient from this QuantityType and the given value.
-
divide
Divide this QuantityType by the givenQuantityType
.- Parameters:
state
- theQuantityType
this QuantityType should be divided by.- Returns:
- the quotient from this QuantityType and the given
QuantityType
.
-
offset
Apply a given offset to this QuantityType- Parameters:
offset
- the offset to apply- Returns:
- changed QuantityType by offset
-
inverse
Return the reciprocal of this QuantityType.- Returns:
- a QuantityType with both the value and unit reciprocated
-