Package io.openems.edge.common.type
Class TypeUtils
java.lang.Object
io.openems.edge.common.type.TypeUtils
Handles implicit conversions between
OpenemsType
s.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Integer
Safely returns the absolute value of an Integer value.static void
assertNull
(String description, 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 Float
Safely finds the average value of all values.static Integer
averageInt
(Integer... values) Safely finds the average value of all values.static Integer
averageInt
(List<Integer> values) Safely finds the average value of all values.static Integer
averageRounded
(Integer... values) Safely finds the average value of all values and rounds the result to an Integer usingMath.round(float)
.static Integer
Safely divides Integers.static Long
Safely divides Longs.static double
fitWithin
(double lowLimit, double highLimit, double value) Fits a value within a lower and upper boundary.static float
fitWithin
(float lowLimit, float highLimit, float value) Fits a value within a lower and upper boundary.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, Object originalValue) Gets the value of the given type asJsonElement
.static <T> T
getAsType
(OpenemsType type, Object value) Converts and casts a Object to a given type.static Float
Safely finds the max value of all values.static Integer
Safely finds the max value of all values.static Double
Safely finds the min value of all values.static Integer
Safely finds the min value of all values.static Double
Safely multiply Doubles.static Float
Safely multiply Floats.static Integer
Safely multiply Integers.static <T> T
orElse
(T nullableValue, T alternativeValue) Returns the 'alternativeValue' if the 'nullableValue' is null.static Double
Safely subtract Doubles.static Integer
Safely subtract Integers.static Long
Safely subtract Longs.static Double
Safely add Doubles.static Integer
Safely add Integers.static Long
Safely add Longs.static Integer
Safely add Integers.static Long
Safely add Longs.static Double
static Double
-
Constructor Details
-
TypeUtils
public TypeUtils()
-
-
Method Details
-
getAsType
Converts and casts a Object to a given type.- Type Parameters:
T
- the Type for implicit casting of the result- Parameters:
type
- the type asOpenemsType
value
- the value asObject
- Returns:
- the converted and casted value
- Throws:
IllegalArgumentException
- on error
-
getAsJson
Gets the value of the given type asJsonElement
.- Parameters:
type
- the type asOpenemsType
originalValue
- the value- Returns:
- the converted value
-
sumInt
Safely add Integers. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.- Parameters:
values
- theInteger
values- Returns:
- the sum
-
sumLong
Safely add Longs. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.- Parameters:
values
- theLong
values- Returns:
- the sum
-
sum
Safely add Integers. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.- Parameters:
values
- theInteger
values- Returns:
- the sum
-
sum
Safely add Longs. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.- Parameters:
values
- theLong
values- Returns:
- the sum
-
sum
Safely add Doubles. If one of them is null it is considered '0'. If all of them are null, 'null' is returned.- Parameters:
values
- theDouble
values- Returns:
- the sum, possibly null
-
subtract
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 subtractionsubtrahend
- the subtrahend of the subtraction- Returns:
- the result, possibly null
-
subtract
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 subtractionsubtrahend
- the subtrahend of the subtraction- Returns:
- the result, possibly null
-
subtract
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 subtractionsubtrahend
- the subtrahend of the subtraction- Returns:
- the result, possibly null
-
multiply
Safely multiply Integers.- Parameters:
firstFactor
- first factor of the multiplicationfurtherFactors
- further factors of the multiplication- Returns:
- the result, possibly null if the first factor is null
-
multiply
Safely multiply Floats.- Parameters:
factors
- the factors of the multiplication- Returns:
- the result, possibly null if all factors are null
-
multiply
Safely multiply Doubles.- Parameters:
factors
- the factors of the multiplication- Returns:
- the result, possibly null if all factors are null
-
divide
Safely divides Integers.- if dividend is null -> result is null
- Parameters:
dividend
- the dividend of the divisiondivisor
- the divisor of the division- Returns:
- the result, possibly null
-
divide
Safely divides Longs.- if dividend is null -> result is null
- Parameters:
dividend
- the dividend of the divisiondivisor
- the divisor of the division- Returns:
- the result, possibly null
-
max
Safely finds the max value of all values.- Parameters:
values
- theInteger
values- Returns:
- the max value; or null if all values are null
-
max
Safely finds the max value of all values.- Parameters:
values
- theFloat
values- Returns:
- the max value; or null if all values are null
-
min
Safely finds the min value of all values.- Parameters:
values
- theInteger
values- Returns:
- the min value; or null if all values are null
-
min
Safely finds the min value of all values.- Parameters:
values
- theDouble
values- Returns:
- the min value; or null if all values are null
-
average
Safely finds the average value of all values.- Parameters:
values
- theInteger
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
Safely finds the average value of all values.- Parameters:
values
- theInteger
values- Returns:
- the average value; or null if all values are null
-
averageInt
Safely finds the average value of all values.- Parameters:
values
- theInteger
values- Returns:
- the average value; or null if all values are null
-
averageRounded
Safely finds the average value of all values and rounds the result to an Integer usingMath.round(float)
.- Parameters:
values
- theInteger
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 exceptionobjects
- the objects- Throws:
IllegalArgumentException
- if any object is null
-
toDouble
- Parameters:
value
- the Integer value, possibly null- Returns:
- the Double value, possibly null
-
toDouble
- 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 nullalternativeValue
- 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 boundaryhighLimit
- the int upper boundaryvalue
- 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 boundaryhighLimit
- the double upper boundaryvalue
- 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 boundaryhighLimit
- the float upper boundaryvalue
- the float actual value- Returns:
- the adjusted float value
-
abs
Safely returns the absolute value of an Integer value.- Parameters:
value
- the Integer value, possibly null- Returns:
- the absolute value, possibly null
-