Class Value<T>
java.lang.Object
io.openems.edge.common.channel.value.Value<T>
- Type Parameters:
T
- the type of the value
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<O extends OptionsEnum>
OasEnum()
Gets the value as its Enum option.com.google.gson.JsonElement
asJson()
Gets the value in GSON JSON format.Gets the value as an Optional.Gets the value as its String option.asString()
Gets the value as a formatted String with its unit.Gets the value as a formatted String without its unit.get()
Gets the value or null.Gets the value or throws an Exception on null.Gets the timestamp when the value was created.void
If a value is present, performs the given action with the value, otherwise does nothing.boolean
Is the value defined?.Gets the value or the given alternativeValue.toString()
-
Field Details
-
UNDEFINED_VALUE_STRING
The String for UNDEFINED, i.e. 'null' value channels.- See Also:
-
-
Constructor Details
-
Value
-
-
Method Details
-
asString
Gets the value as a formatted String with its unit. (Same as toString())- Returns:
- the value as formatted String
-
toString
-
asStringWithoutUnit
Gets the value as a formatted String without its unit.- Returns:
- formatted string without unit
-
get
Gets the value or null.- Returns:
- the value
-
getOrError
Gets the value or throws an Exception on null.- Returns:
- the value; never null
- Throws:
InvalidValueException
- if value is null
-
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
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action
- the action to be performed, if a value is present- Throws:
NullPointerException
- if value is present and the given action isnull
-
orElse
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
Gets the value as its String option. Enum options are converted to Strings.- Returns:
- the String option of Enum values
- Throws:
IllegalArgumentException
- no matching option existing
-
asEnum
Gets the value as its Enum option.- Type Parameters:
O
- theOptionsEnum
type- Returns:
- the Enum value
- Throws:
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
Gets the timestamp when the value was created.- Returns:
- the timestamp
-