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 Details

    • ZERO

      public static final QuantityType<javax.measure.quantity.Dimensionless> ZERO
    • ONE

      public static final QuantityType<javax.measure.quantity.Dimensionless> ONE
  • Constructor Details

    • QuantityType

      public QuantityType()
      Creates a dimensionless QuantityType with scalar 0 and unit AbstractUnit.ONE. A default constructor is needed by ItemUpdater.receiveUpdate(ItemStateEvent))
    • QuantityType

      public QuantityType(String value)
      Creates a new QuantityType with the given value. The value may contain a unit. The specific Quantity is obtained by NumberDelimiterQuantityFormat.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 parsed
      IllegalArgumentException - when a quantity with a unit could not be parsed
    • QuantityType

      public QuantityType(String value, Locale locale)
      Creates a new QuantityType with the given value. The value may contain a unit. The specific Quantity is obtained by NumberDelimiterQuantityFormat.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 parsed
      IllegalArgumentException - when a quantity with a unit could not be parsed
    • QuantityType

      public QuantityType(Number value, javax.measure.Unit<T> unit)
      Creates a new QuantityType with the given value and Unit.
      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)
      Parameters:
      value - the non-null measurement value.
      unit - the non-null measurement unit.
      Returns:
      a new QuantityType
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • valueOf

      public static QuantityType<? extends javax.measure.Quantity<?>> valueOf(String value)
      Static access to QuantityType(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 parsed
      IllegalArgumentException - when a quantity with a unit could not be parsed
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(QuantityType<T> o)
      Specified by:
      compareTo in interface Comparable<T extends javax.measure.Quantity<T>>
    • getUnit

      public javax.measure.Unit<T> getUnit()
    • getDimension

      public javax.measure.Dimension getDimension()
    • toUnit

      public @Nullable QuantityType<T> toUnit(javax.measure.Unit<?> targetUnit)
      Convert this QuantityType to a new QuantityType using the given target unit.
      Parameters:
      targetUnit - the unit to which this QuantityType will be converted to.
      Returns:
      the new QuantityType in the given Unit or null in case of an error.
    • toUnit

      public @Nullable QuantityType<T> toUnit(String targetUnit)
    • toInvertibleUnit

      public @Nullable QuantityType<?> toInvertibleUnit(javax.measure.Unit<?> targetUnit)
      Convert this QuantityType to a new QuantityType 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 this QuantityType will be converted to.
      Returns:
      the new QuantityType in the given Unit or null in case of an error.
    • toInvertibleUnit

      public @Nullable QuantityType<?> toInvertibleUnit(String targetUnit)
    • toUnitRelative

      public @Nullable QuantityType<T> toUnitRelative(javax.measure.Unit<T> targetUnit)
      Convert this QuantityType to a new QuantityType using the given target unit. Similar to toUnit(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 this QuantityType will be converted to.
      Returns:
      the new QuantityType in the given Unit or null in case of an error.
    • toUnitRelative

      public @Nullable QuantityType<T> toUnitRelative(String targetUnit)
    • toBigDecimal

      public BigDecimal toBigDecimal()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • format

      public String format(String pattern)
      Description copied from interface: Type
      Formats the value of this type according to a pattern (see Formatter).
      Specified by:
      format in interface Type
      Parameters:
      pattern - the pattern to use
      Returns:
      the formatted string
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number
    • toFullString

      public String 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 interface Type
      Returns:
      a full string representation of the type to be consumed by 'valueOf(String)'
    • as

      public <U extends State> @Nullable U as(@Nullable Class<U> target)
      Description copied from interface: State
      Convert this State's value into another type
      Specified by:
      as in interface State
      Parameters:
      target - the desired State type
      Returns:
      the State's value in the given type's representation, or null if the conversion was not possible
    • add

      public QuantityType<T> add(QuantityType<T> state)
      Returns the sum of the given QuantityType with this QuantityType.
      Parameters:
      state - the QuantityType to add to this QuantityType.
      Returns:
      the sum of the given QuantityType with this QuantityType.
    • negate

      public QuantityType<T> negate()
      Negates the value of this QuantityType leaving its unit untouched.
      Returns:
      the negated value of this QuantityType.
    • subtract

      public QuantityType<T> subtract(QuantityType<T> state)
      Subtract the given QuantityType from this QuantityType.
      Parameters:
      state - the QuantityType to subtract from this QuantityType.
      Returns:
      the difference by subtracting the given QuantityType from this QuantityType.
    • multiply

      public QuantityType<?> multiply(BigDecimal value)
      Multiply this QuantityType by the given value.
      Parameters:
      value - the value this QuantityType should be multiplied with.
      Returns:
      the product of the given value with this QuantityType.
    • multiply

      public QuantityType<?> multiply(QuantityType<?> state)
      Multiply this QuantityType by the given QuantityType.
      Parameters:
      state - the QuantityType with which this QuantityType should be multiplied with.
      Returns:
      the product of the given QuantityType and this QuantityType.
    • divide

      public QuantityType<?> divide(BigDecimal value)
      Divide this QuantityType by the given value.
      Parameters:
      value - the value this QuantityType should be divided by.
      Returns:
      the quotient from this QuantityType and the given value.
    • divide

      public QuantityType<?> divide(QuantityType<?> state)
      Divide this QuantityType by the given QuantityType.
      Parameters:
      state - the QuantityType this QuantityType should be divided by.
      Returns:
      the quotient from this QuantityType and the given QuantityType.
    • offset

      public QuantityType<T> offset(QuantityType<T> offset, javax.measure.Unit<T> unit)
      Apply a given offset to this QuantityType
      Parameters:
      offset - the offset to apply
      Returns:
      changed QuantityType by offset
    • inverse

      public QuantityType<?> inverse()
      Return the reciprocal of this QuantityType.
      Returns:
      a QuantityType with both the value and unit reciprocated