public class Scope extends Object implements Serializable
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.
| Modifier and Type | Method and Description |
|---|---|
static Scope |
create()
Creates a new empty scope.
|
Variable |
create(String name)
Searches or creates a variable in this scope.
|
static Scope |
createWithParent(Scope parent)
Creates a new scope with the given parent.
|
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.
|
public static Scope create()
The scope will not be completely empty, as Math.PI (pi) and Math.E (E) are always
defined as constants.
public static Scope createWithParent(Scope parent)
If a variable is resolved, first the own scope is scanned. If no variable for the given name is found, the parent scope is scanned. If this yields a variable, it will be returned. Otherwise it will be defined in the local scope (not in the parent).
parent - the scope to use as lookup source for further variablespublic 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 forpublic 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 Set<String> getLocalNames()
public Set<String> getNames()
public Collection<Variable> getLocalVariables()
public Collection<Variable> getVariables()
Copyright © 2015. All rights reserved.