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.
|
BigDecimal |
getValue()
Returns the value previously set.
|
boolean |
isConstant()
Determines if this variable is constant.
|
void |
makeConstant(BigDecimal value)
Sets the given value and marks it as constant.
|
void |
makeConstant(Supplier<BigDecimal> supplier)
Makes a constant who's value is supplied when this constant is first referenced.
|
void |
setValue(BigDecimal value)
Sets the value if the variable.
|
void |
setValue(double value)
Sets the value if the variable.
|
void |
setValue(long value)
Sets the value if the variable.
|
String |
toString() |
Variable |
withValue(BigDecimal value)
Sets the value and returns this.
|
Variable |
withValue(double value)
Sets the value and returns this.
|
Variable |
withValue(long value)
Sets the value and returns this.
|
public void setValue(BigDecimal value)
value - the new value of the variableIllegalStateException - if the variable is constantpublic void setValue(double value)
value - the new value of the variableIllegalStateException - if the variable is constantpublic void setValue(long value)
value - the new value of the variableIllegalStateException - if the variable is constantpublic void makeConstant(BigDecimal value)
value - the new (and final) value of this variablepublic void makeConstant(Supplier<BigDecimal> supplier)
supplier - supplier of the constant valuepublic BigDecimal getValue()
public String getName()
public boolean isConstant()
public Variable withValue(BigDecimal value)
value - the new value of this variablepublic Variable withValue(double value)
value - the new value of this variablepublic Variable withValue(long value)
value - the new value of this variableCopyright © 2019. All rights reserved.