public class Variable extends Object
A variable is resolved or created using a Scope
. This ensures that the same name always resolves to the
same variable. In contrast to using a Map, reading and writing a variable can be much faster, as it only needs
to be resolved once. Reading and writing it, is basically as cheap as a field access.
A variable can be made constant, which will fail all further attempts to change it.
Modifier | Constructor and Description |
---|---|
protected |
Variable(String name)
Creates a new variable.
|
Modifier and Type | Method and Description |
---|---|
String |
getName()
Returns the name of the variable.
|
double |
getValue()
Returns the value previously set.
|
boolean |
isConstant()
Determines if this variable is constant.
|
void |
makeConstant(double value)
Sets the given value and marks it as constant.
|
void |
setValue(double value)
Sets the value if the variable.
|
String |
toString() |
Variable |
withValue(double value)
Sets the value and returns this.
|
public void setValue(double value)
value
- the new value of the variableIllegalStateException
- if the variable is constantpublic void makeConstant(double value)
value
- the new (and final) value of this variablepublic double getValue()
public String getName()
public boolean isConstant()
public Variable withValue(double value)
value
- the new value of this variableCopyright © 2020. All rights reserved.