Class TypeUtils


  • public class TypeUtils
    extends java.lang.Object
    Handles implicit conversions between OpenemsTypes
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Integer abs​(java.lang.Integer value)
      Safely returns the absolute value of an Integer value.
      static void assertNull​(java.lang.String description, java.lang.Object... objects)
      Throws an descriptive exception if the object is null.
      static double average​(double... values)
      Safely finds the average value of all values.
      static java.lang.Float average​(java.lang.Integer... values)
      Safely finds the average value of all values.
      static java.lang.Integer averageRounded​(java.lang.Integer... values)
      Safely finds the average value of all values and rounds the result to an Integer using Math.round(float).
      static java.lang.Integer divide​(java.lang.Integer dividend, int divisor)
      Safely divides Integers.
      static java.lang.Long divide​(java.lang.Long dividend, long divisor)
      Safely divides Longs.
      static int fitWithin​(int lowLimit, int highLimit, int value)
      Fits a value within a lower and upper boundary.
      static com.google.gson.JsonElement getAsJson​(OpenemsType type, java.lang.Object originalValue)  
      static <T> T getAsType​(OpenemsType type, java.lang.Object value)  
      static java.lang.Integer max​(java.lang.Integer... values)
      Safely finds the max value of all values.
      static java.lang.Double min​(java.lang.Double... values)
      Safely finds the min value of all values.
      static java.lang.Integer min​(java.lang.Integer... values)
      Safely finds the min value of all values.
      static java.lang.Double multiply​(java.lang.Double... factors)
      Safely multiply Doubles.
      static java.lang.Integer multiply​(java.lang.Integer... factors)
      Safely multiply Integers.
      static <T> T orElse​(T nullableValue, T alternativeValue)
      Returns the 'alternativeValue' if the 'nullableValue' is null.
      static java.lang.Integer subtract​(java.lang.Integer minuend, java.lang.Integer subtrahend)
      Safely subtract Integers.
      static java.lang.Long subtract​(java.lang.Long minuend, java.lang.Long subtrahend)
      Safely subtract Longs.
      static java.lang.Integer sum​(java.lang.Integer... values)
      Safely add Integers.
      static java.lang.Long sum​(java.lang.Long... values)
      Safely add Longs.
      static java.lang.Double toDouble​(java.lang.Float value)
      Safely convert from Float to Double
      static java.lang.Double toDouble​(java.lang.Integer value)
      Safely convert from Integer to Double
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TypeUtils

        public TypeUtils()
    • Method Detail

      • getAsType

        public static <T> T getAsType​(OpenemsType type,
                                      java.lang.Object value)
                               throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • getAsJson

        public static com.google.gson.JsonElement getAsJson​(OpenemsType type,
                                                            java.lang.Object originalValue)
      • sum

        public static java.lang.Integer sum​(java.lang.Integer... values)
        Safely add Integers. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.
        Parameters:
        values -
        Returns:
      • sum

        public static java.lang.Long sum​(java.lang.Long... values)
        Safely add Longs. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.
        Parameters:
        values -
        Returns:
      • subtract

        public static java.lang.Integer subtract​(java.lang.Integer minuend,
                                                 java.lang.Integer subtrahend)
        Safely subtract Integers.
        • if minuend is null -> result is null
        • if subtrahend is null -> result is minuend
        • if both are null -> result is null
        Parameters:
        minuend - the minuend of the subtraction
        subtrahend - the subtrahend of the subtraction
        Returns:
        the result, possibly null
      • subtract

        public static java.lang.Long subtract​(java.lang.Long minuend,
                                              java.lang.Long subtrahend)
        Safely subtract Longs.
        • if minuend is null -> result is null
        • if subtrahend is null -> result is minuend
        • if both are null -> result is null
        Parameters:
        minuend - the minuend of the subtraction
        subtrahend - the subtrahend of the subtraction
        Returns:
        the result, possibly null
      • multiply

        public static java.lang.Integer multiply​(java.lang.Integer... factors)
        Safely multiply Integers.
        Parameters:
        factors - the factors of the multiplication
        Returns:
        the result, possibly null if all factors are null
      • multiply

        public static java.lang.Double multiply​(java.lang.Double... factors)
        Safely multiply Doubles.
        Parameters:
        factors - the factors of the multiplication
        Returns:
        the result, possibly null if all factors are null
      • divide

        public static java.lang.Integer divide​(java.lang.Integer dividend,
                                               int divisor)
        Safely divides Integers.
        • if dividend is null -> result is null
        Parameters:
        dividend - the dividend of the division
        divisor - the divisor of the division
        Returns:
        the result, possibly null
      • divide

        public static java.lang.Long divide​(java.lang.Long dividend,
                                            long divisor)
        Safely divides Longs.
        • if dividend is null -> result is null
        Parameters:
        dividend - the dividend of the division
        divisor - the divisor of the division
        Returns:
        the result, possibly null
      • max

        public static java.lang.Integer max​(java.lang.Integer... values)
        Safely finds the max value of all values.
        Returns:
        the max value; or null if all values are null
      • min

        public static java.lang.Double min​(java.lang.Double... values)
        Safely finds the min value of all values.
        Returns:
        the min value; or null if all values are null
      • average

        public static java.lang.Float average​(java.lang.Integer... values)
        Safely finds the average value of all values.
        Returns:
        the average value; or null if all values are null
      • average

        public static double average​(double... values)
        Safely finds the average value of all values.
        Returns:
        the average value; or Double.NaN if all values are invalid.
      • averageRounded

        public static java.lang.Integer averageRounded​(java.lang.Integer... values)
        Safely finds the average value of all values and rounds the result to an Integer using Math.round(float).
        Returns:
        the rounded average value; or null if all values are null
      • min

        public static java.lang.Integer min​(java.lang.Integer... values)
        Safely finds the min value of all values.
        Returns:
        the min value; or null if all values are null
      • assertNull

        public static void assertNull​(java.lang.String description,
                                      java.lang.Object... objects)
                               throws java.lang.IllegalArgumentException
        Throws an descriptive exception if the object is null.
        Parameters:
        description - text that is added to the exception
        objects - the objects
        Throws:
        java.lang.IllegalArgumentException - if any object is null
      • toDouble

        public static java.lang.Double toDouble​(java.lang.Integer value)
        Safely convert from Integer to Double
        Parameters:
        value - the Integer value, possibly null
        Returns:
        the Double value, possibly null
      • toDouble

        public static java.lang.Double toDouble​(java.lang.Float value)
        Safely convert from Float to Double
        Parameters:
        value - the Float value, possibly null
        Returns:
        the Double value, possibly null
      • orElse

        public static <T> T orElse​(T nullableValue,
                                   T alternativeValue)
        Returns the 'alternativeValue' if the 'nullableValue' is null.
        Parameters:
        nullableValue - the value, can be null
        alternativeValue - the alternative value
        Returns:
        either the value (not null), alternatively the 'orElse' value
      • fitWithin

        public static int fitWithin​(int lowLimit,
                                    int highLimit,
                                    int value)
        Fits a value within a lower and upper boundary.
        Parameters:
        lowLimit - the lower boundary
        highLimit - the upper boundary
        value - the actual value
        Returns:
        the adjusted value
      • abs

        public static java.lang.Integer abs​(java.lang.Integer value)
        Safely returns the absolute value of an Integer value.
        Parameters:
        value - the Integer value, possibly null
        Returns:
        the absolute value, possibly null