public class Variable extends Object implements NamedValueWithSource, Identifiable
Represents a single variable. A variable consists of a name, a value, and a source. The supported value types depend on the implementation of the variable persistence mechanism.
Constructor and Description |
---|
Variable(String name,
Object value)
Creates a new variable with a mandatory name and optional value.
|
Variable(String name,
Object value,
String source)
Creates a new variable with a mandatory name, optional value, and source.
|
Variable(String id,
String name,
Object value,
String source)
Creates a new variable with an optional id, name, optional value, and optional source.
|
Variable(ValueAccessor<T> name,
T value)
Creates a new variable with a mandatory name and optional value.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o) |
static Variable |
findByName(String name,
Collection<Variable> variables)
Finds the variable with the given name among the given variables.
|
String |
getId()
Returns the id of the variable.
|
String |
getName()
Returns the name of the variable.
|
String |
getSource()
Returns the source of the variable.
|
Object |
getValue()
Returns the value of the variable.
|
<S> S |
getValue(Class<S> expectedType)
Convenience method for returning the value of the variable cast to the expected type.
|
<S> S |
getValue(com.google.common.reflect.TypeToken<S> expectedType)
Convenience method for returning the value of the variable cast to the expected type.
|
int |
hashCode() |
String |
toString() |
Variable |
withId(String id)
Returns a new variable with the given id and the current name, value, and source.
|
Variable |
withSource(String source)
Returns a new variable with the given source and the current id, name, and value.
|
public Variable(ValueAccessor<T> name, T value)
Creates a new variable with a mandatory name and optional value.
name
- the name of the variablevalue
- the value of the variablepublic Variable(String name, Object value)
Creates a new variable with a mandatory name and optional value.
name
- the name of the variablevalue
- the value of the variablepublic Variable(String name, Object value, String source)
Creates a new variable with a mandatory name, optional value, and source.
name
- the name of the variablevalue
- the value of the variablesource
- the source of the variablepublic Variable(String id, String name, Object value, String source)
Creates a new variable with an optional id, name, optional value, and optional source.
id
- the id of the variablename
- the name of the variablevalue
- the value of the variablesource
- the source of the variablepublic String getId()
Returns the id of the variable.
getId
in interface Identifiable
public String getName()
Returns the name of the variable.
getName
in interface NamedValue
public Object getValue()
Returns the value of the variable.
getValue
in interface NamedValue
public <S> S getValue(Class<S> expectedType)
Convenience method for returning the value of the variable cast to the expected type. If the variable value is not of the expected type and not null, an IllegalArgumentException
is thrown.
getValue
in interface NamedValue
S
- the type of the valueexpectedType
- the expected type of the variable valueIllegalArgumentException
- thrown if the cast is not possiblepublic <S> S getValue(com.google.common.reflect.TypeToken<S> expectedType)
Convenience method for returning the value of the variable cast to the expected type. If the variable value is not of the expected type and not null, an IllegalArgumentException
is thrown.
getValue
in interface NamedValue
S
- the type of the valueexpectedType
- the expected type of the variable valueIllegalArgumentException
- thrown if the cast is not possiblepublic String getSource()
Returns the source of the variable.
getSource
in interface NamedValueWithSource
public Variable withId(String id)
Returns a new variable with the given id and the current name, value, and source.
id
- the new id of the variablepublic Variable withSource(String source)
Returns a new variable with the given source and the current id, name, and value.
source
- the source of the variablepublic static Variable findByName(String name, Collection<Variable> variables)
Finds the variable with the given name among the given variables.
name
- the name of the variable to findvariables
- the variables to searchNoSuchElementException
- thrown if there is no variable with the given nameCopyright © 2010 - 2019 edorasware ag. All Rights Reserved.