Class TypeUtils

java.lang.Object
io.openems.edge.common.type.TypeUtils

public class TypeUtils extends Object
Handles implicit conversions between OpenemsTypes.
  • Constructor Details

    • TypeUtils

      public TypeUtils()
  • Method Details

    • getAsType

      public static <T> T getAsType(OpenemsType type, Object value) throws 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:
      IllegalArgumentException - on error
    • getAsJson

      public static com.google.gson.JsonElement getAsJson(OpenemsType type, Object originalValue)
      Gets the value of the given type as JsonElement.
      Parameters:
      type - the type as OpenemsType
      originalValue - the value
      Returns:
      the converted value
    • sumInt

      public static Integer sumInt(List<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
    • sumLong

      public static Long sumLong(List<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
    • sum

      public static Integer sum(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 Long sum(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
    • sum

      public static Double sum(Double... values)
      Safely add Doubles. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.
      Parameters:
      values - the Double values
      Returns:
      the sum, possibly null
    • subtract

      public static Integer subtract(Integer minuend, 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 Long subtract(Long minuend, 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
    • subtract

      public static Double subtract(Double minuend, Double subtrahend)
      Safely subtract Doubles.
      • 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 Integer multiply(Integer firstFactor, 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 Float multiply(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 Double multiply(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 Integer divide(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 Long divide(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 Integer max(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 Float max(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 Integer min(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 Double min(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 Float average(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 Integer averageInt(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
    • averageInt

      public static Integer averageInt(List<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 Integer averageRounded(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(String description, Object... objects) throws IllegalArgumentException
      Throws an descriptive exception if the object is null.
      Parameters:
      description - text that is added to the exception
      objects - the objects
      Throws:
      IllegalArgumentException - if any object is null
    • toDouble

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

      public static Double toDouble(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 int lower boundary
      highLimit - the int upper boundary
      value - the int actual value
      Returns:
      the adjusted int value
    • fitWithin

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

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

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