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 averageInt​(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)
      Gets the value of the given type as JsonElement.
      static <T> T getAsType​(OpenemsType type, java.lang.Object value)
      Converts and casts a Object to a given type.
      static java.lang.Float max​(java.lang.Float... values)
      Safely finds the max value of all values.
      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.Float multiply​(java.lang.Float... factors)
      Safely multiply Floats.
      static java.lang.Integer multiply​(java.lang.Integer firstFactor, java.lang.Integer... furtherFactors)
      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
        Converts and casts a Object to a given type.
        Type Parameters:
        T - the Type for implicit casting of the result
        Parameters:
        type - the type as OpenemsType
        value - the value as Object
        Returns:
        the converted and casted value
        Throws:
        java.lang.IllegalArgumentException - on error
      • getAsJson

        public static com.google.gson.JsonElement getAsJson​(OpenemsType type,
                                                            java.lang.Object originalValue)
        Gets the value of the given type as JsonElement.
        Parameters:
        type - the type as OpenemsType
        originalValue - the value
        Returns:
        the converted value
      • 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 - the Integer values
        Returns:
        the sum
      • 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 - the Long values
        Returns:
        the sum
      • 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 firstFactor,
                                                 java.lang.Integer... furtherFactors)
        Safely multiply Integers.
        Parameters:
        firstFactor - first factor of the multiplication
        furtherFactors - further factors of the multiplication
        Returns:
        the result, possibly null if the first factor is null
      • multiply

        public static java.lang.Float multiply​(java.lang.Float... factors)
        Safely multiply Floats.
        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.
        Parameters:
        values - the Integer values
        Returns:
        the max value; or null if all values are null
      • max

        public static java.lang.Float max​(java.lang.Float... values)
        Safely finds the max value of all values.
        Parameters:
        values - the Float values
        Returns:
        the max 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.
        Parameters:
        values - the Integer values
        Returns:
        the min 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.
        Parameters:
        values - the Double 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.
        Parameters:
        values - the Integer 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.
        Parameters:
        values - the double values
        Returns:
        the average value; or Double.NaN if all values are invalid.
      • averageInt

        public static java.lang.Integer averageInt​(java.lang.Integer... values)
        Safely finds the average value of all values.
        Parameters:
        values - the Integer values
        Returns:
        the average value; or null if all values are null
      • 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).
        Parameters:
        values - the Integer values
        Returns:
        the rounded average 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.
        Type Parameters:
        T - the Type for implicit casting
        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