Class Value<T>
- java.lang.Object
-
- io.openems.edge.common.channel.value.Value<T>
-
- Type Parameters:
T
- the type of the value
public class Value<T> extends java.lang.Object
It is important to understand, that a Channel Value could be UNDEFINED (i.e. 'null') at any point in time - e.g. due to lost communication connection to a Device or Service, or because the system is just starting up and does not yet have any data. This class wraps a 'value' information for a Channel and provides convenience methods for retrieving it.To get the actual value of a Channel using this object, typically one of the following methods will fit:
get()
: gets the value or null. Be aware ofNullPointerException
s!getOrError()
: gets the value or throws anInvalidValueException
if the value is nullorElse(Object)
: gets the value; or fallback alternative if the value is null
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
UNDEFINED_VALUE_STRING
The String for UNDEFINED, i.e.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <O extends OptionsEnum>
OasEnum()
Gets the value as its Enum option.com.google.gson.JsonElement
asJson()
Gets the value in GSON JSON format.java.util.Optional<T>
asOptional()
Gets the value as an Optional.java.lang.String
asOptionString()
Gets the value as its String option.java.lang.String
asString()
Gets the value as a formatted String with its unit.java.lang.String
asStringWithoutUnit()
Gets the value as a formatted String without its unit.T
get()
Gets the value or null.T
getOrError()
Gets the value or throws an Exception on null.java.time.LocalDateTime
getTimestamp()
Gets the timestamp when the value was created.boolean
isDefined()
Is the value defined?.T
orElse(T alternativeValue)
Gets the value or the given alternativeValue.java.lang.String
toString()
-
-
-
Field Detail
-
UNDEFINED_VALUE_STRING
public static final java.lang.String UNDEFINED_VALUE_STRING
The String for UNDEFINED, i.e. 'null' value channels.- See Also:
- Constant Field Values
-
-
Method Detail
-
asString
public java.lang.String asString()
Gets the value as a formatted String with its unit. (Same as toString())- Returns:
- the value as formatted String
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
asStringWithoutUnit
public java.lang.String asStringWithoutUnit()
Gets the value as a formatted String without its unit.- Returns:
- formatted string without unit
-
get
public T get()
Gets the value or null.- Returns:
- the value
-
getOrError
public T getOrError() throws InvalidValueException
Gets the value or throws an Exception on null.- Returns:
- the value; never null
- Throws:
InvalidValueException
- if value is null
-
asOptional
public java.util.Optional<T> asOptional()
Gets the value as an Optional.- Returns:
- the value as
Optional
-
isDefined
public boolean isDefined()
Is the value defined?. This is an abbreviation for Value.asOptional().isPresent().- Returns:
- true if the value is defined; false if it is UNDEFINED
-
orElse
public T orElse(T alternativeValue)
Gets the value or the given alternativeValue. This is short for '.asOptional().or()'.- Parameters:
alternativeValue
- the alternative value- Returns:
- the value; or an alternative value if the value is null
-
asOptionString
public java.lang.String asOptionString()
Gets the value as its String option. Enum options are converted to Strings.- Returns:
- the String option of Enum values
- Throws:
java.lang.IllegalArgumentException
- no matching option existing
-
asEnum
public <O extends OptionsEnum> O asEnum()
Gets the value as its Enum option.- Type Parameters:
O
- theOptionsEnum
type- Returns:
- the Enum value
- Throws:
java.lang.IllegalArgumentException
- no matching Enum option existing
-
asJson
public com.google.gson.JsonElement asJson()
Gets the value in GSON JSON format.- Returns:
- the value as
JsonElement
-
getTimestamp
public java.time.LocalDateTime getTimestamp()
Gets the timestamp when the value was created.- Returns:
- the timestamp
-
-