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.
|
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).
|
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.
|
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 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 reuqested.
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. If null, the common root scope is used which defines a bunch of
constants (e and pi).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()
Copyright © 2020. All rights reserved.