Class UnitUtils


  • @NonNullByDefault
    public class UnitUtils
    extends Object
    A utility for parsing dimensions to interface classes of Quantity and parsing units from format strings.
    Author:
    Henning Treu - Initial contribution
    • Constructor Detail

      • UnitUtils

        public UnitUtils()
    • Method Detail

      • parseDimension

        public static @Nullable Class<? extends javax.measure.Quantity<?>> parseDimension​(String dimension)
        Parses a String denoting a dimension (e.g. Length, Temperature, Mass,..) into a Class instance of an interface from Quantity. The interfaces reside in javax.measure.quantity and framework specific interfaces in org.openhab.core.library.dimension.
        Parameters:
        dimension - the simple name of an interface from the package javax.measure.quantity or org.openhab.core.library.dimension.
        Returns:
        the Class instance of the interface or null if the given dimension is blank.
        Throws:
        IllegalArgumentException - in case no class instance could be parsed from the given dimension.
      • getDimensionName

        public static @Nullable String getDimensionName​(javax.measure.Unit<?> unit)
        The name of the dimension as stated in the ChannelType configuration. e.g.

        Unit: 'm' -> "Length"

        Unit: 'kWh' -> "Energy"

        If the Unit can not be found in any of the available Measurement systems, it returns null

        Parameters:
        unit - The Unit to get the Dimension's name from.
        Returns:
        The Dimension string or null if the unit can not be found in any of the SystemOfUnits.
      • parseUnit

        public static @Nullable javax.measure.Unit<?> parseUnit​(@Nullable String pattern)
        A utility method to parse a unit symbol either directly or from a given pattern (like stateDescription or widget label). In the latter case, the unit is expected to be the last part of the pattern separated by " " (e.g. "%.2f °C" for °C).
        Parameters:
        stringWithUnit - the string to extract the unit symbol from
        Returns:
        the unit symbol extracted from the string or null if no unit could be parsed
      • isDifferentMeasurementSystem

        public static boolean isDifferentMeasurementSystem​(javax.measure.Unit<? extends javax.measure.Quantity<?>> thisUnit,
                                                           javax.measure.Unit<?> thatUnit)