public class Variable
extends java.lang.Object
programmatically
.
A variable may have an optional initializer associated with it. This can
be a simple instance that is assigned at construction and when reset. Or
can be an expression which is evaluated to generate the initial value.
Any instance or expression will be assigned each time the reset()
method is called. The result of executing the initializer expression will
be stored as the new value of the variable. The variable is reset
automatically at construction. If no initializer expression is provided,
resetting the variable will set its value to null
. The
initializer expression for a variable may be arbitrarily complex, but
it may not refer to its containing variable, and it may only refer to
libraries and variables which previously have been registered with the
symbol resolver.
Variables are stored within variable pools inside a symbol resolver
. A variable is added to a variable pool through the
SymbolResolver.registerVariable(com.goldencode.expr.Scope, java.lang.String, java.lang.Class<?>, com.goldencode.expr.Initializer, boolean)
method. It is that method which
instantiates new Variable
instances. Although a data type
is optional during registration if an initializer expression is provided,
it is recommended to provide a data type if it is possible that a more
specific data type is needed than may be returned by the initializer.
Otherwise, methods available only in the more specific type will not be
accessible to expressions which reference this variable.
Modifier and Type | Field and Description |
---|---|
private Initializer |
initializer
Used to initialize and reset current value.
|
private java.lang.String |
name
Symbol by which this variable will be referenced in user expressions.
|
private boolean |
readOnly
Whether the variable is read-only from within expressions.
|
private java.lang.Class |
type
Data type.
|
private java.lang.Object |
value
Current value.
|
Constructor and Description |
---|
Variable(java.lang.String name,
java.lang.Class type,
Initializer initializer,
boolean readOnly)
Construct a user variable with the given name and type, and initialize
its value using the supplied expression.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getName()
Get the name of this variable.
|
java.lang.Class |
getType()
Get the data type of this variable.
|
java.lang.Object |
getValue()
Get the current value of this variable.
|
boolean |
isReadOnly()
Indicate whether this variable is read-only from within expressions.
|
void |
reset()
Reset the current value of this variable to its default, using the
initializer expression provided at construction.
|
void |
setValue(java.lang.Object value)
Set the current value of this variable.
|
java.lang.String |
toString()
Get a string representation of the value contained within this
variable.
|
private java.lang.String name
private java.lang.Class type
private java.lang.Object value
private boolean readOnly
private Initializer initializer
Variable(java.lang.String name, java.lang.Class type, Initializer initializer, boolean readOnly)
assignment compatible
with the declared data type. If the data type is omitted, the return
type of the initializer will be used as the variable's data type.name
- Case-sensitive name by which this variable will be referenced
within user expressions.type
- Data type of this variable. Required if
initializer
is null
.initializer
- The expression or value used to initialize and reset the value
of this variable. May be null
, in which case the
starting value of the variable is null
, and the
value to which the variable will be reset
is also
null
. Required if type
is
null
.java.lang.IllegalArgumentException
- if type
represents a primitive class; if
type
was null
and could not be
determined from initializer
's return type; if
type
was not null
and was
incompatible with initializer
's return type.UnresolvedSymbolException
- if initializer expression cannot resolve a mandatory symbol.java.lang.ClassCastException
- if initializer expression produces a non-null
result which is not appropriate to the data type of this
variable.public java.lang.String getName()
public java.lang.Class getType()
public java.lang.Object getValue()
public boolean isReadOnly()
true
if this variable is read-only, else
false
.public void setValue(java.lang.Object value)
value
- New value. Must be assignment-compatible with this variable's
data type.java.lang.ClassCastException
- if type of value
is not compatible with this
variable's data type.public void reset()
null
.UnresolvedSymbolException
- if initializer expression cannot resolve a mandatory symbol.java.lang.ClassCastException
- if initializer expression produces a non-null
result which is not appropriate to the data type of this
variable.public java.lang.String toString()
toString
implementation of the value object. A null
value is
safely handled.toString
in class java.lang.Object