public class Scope extends Object
Both the user as well as the Parser use a Scope to resolve a name into a Variable. In contrast to a
simple Map, this approach provides two advantages: It's usually faster, as the variable only needs to be resolved
once. Modifying it and especially reading it when evaluating an expression is as cheap as a simple field access. The
second advantage is that scopes can be chained. So variables can be either shared by two expression or kept separate,
if required.
| Constructor and Description |
|---|
Scope()
Creates a new empty scope.
|
| Modifier and Type | Method and Description |
|---|---|
Variable |
create(String name)
Searches or creates a variable in this scope.
|
protected void |
createConstants() |
Variable |
find(String name)
Searches for a
Variable with the given name. |
Set<String> |
getLocalNames()
Returns all names of variables known to this scope (ignoring those of the parent scope).
|
Collection<Variable> |
getLocalVariables()
Returns all variables known to this scope (ignoring those of the parent scope).
|
MathContext |
getMathContext() |
Set<String> |
getNames()
Returns all names of variables known to this scope or one of its parent scopes.
|
Variable |
getVariable(String name)
Searches for or creates a variable with the given name.
|
Collection<Variable> |
getVariables()
Returns all variables known to this scope or one of its parent scopes.
|
Variable |
remove(String name)
Removes the variable with the given name from this scope.
|
void |
setMathContext(MathContext mathContext)
Set the math context to use.
|
Scope |
withMathContext(MathContext mathContext)
Set the math context to use.
|
Scope |
withParent(Scope parent)
Specifies the parent scope for this scope.
|
Scope |
withStrictLookup(boolean strictLookup)
Determines if strict lookup should be used or not.
|
public Scope()
The scope will not be completely empty, as pi and euler are always defined as constants.
If an not yet known variable is accessed, it will be created and initialized with 0.
public Scope withStrictLookup(boolean strictLookup)
A scope with strict lookup will not create unknown variables upon their first access but rather throw an error.
By default, scopes are not strict and will automatically create variables when first requested.
strictLookup - true if the scope should be switched to strict lookup, false otherwisepublic Scope withParent(Scope parent)
If a scope cannot resolve a variable, it tries to resolve it using its parent scope. This permits to share a certain set of variables.
parent - the parent scope to use.public Variable find(String name)
Variable with the given name.
If the variable does not exist null will be returned
name - the name of the variable to searchpublic Variable getVariable(String name)
If no variable with the given name is found, a new variable is created in this scope
name - the variable to look forIllegalArgumentException - if autocreateVariables is false and the given variable was not
creted yet.public Variable create(String name)
Tries to find a variable with the given name in this scope. If no variable with the given name is found, the parent scope is not checked, but a new variable is created.
name - the variable to search or createpublic Variable remove(String name)
If will not remove the variable from a parent scope.
name - the name of the variable to removepublic Set<String> getLocalNames()
public Set<String> getNames()
public Collection<Variable> getLocalVariables()
public Collection<Variable> getVariables()
public MathContext getMathContext()
public void setMathContext(MathContext mathContext)
mathContext - the math contextpublic Scope withMathContext(MathContext mathContext)
mathContext - the math contextprotected void createConstants()
Copyright © 2019. All rights reserved.