public class Namespace<E extends Named>
extends java.lang.Object
implements java.io.Serializable
Name
s and the objects
carrying these names. These objects have to implement the interface
Named
. It is possible to have nested namespaces in order to
represent different visibility scopes.Modifier and Type | Field and Description |
---|---|
private Namespace<E> |
parent
The fall-back namespace for symbols not present in this Namespace.
|
private boolean |
sealed
A namespace can be made immutable, this is called "sealing".
|
private static long |
serialVersionUID |
private java.util.Map<Name,E> |
symbols
The map that maps a name to a symbols of that name if it is defined in
this Namespace.
|
Constructor and Description |
---|
Namespace()
Construct an empty Namespace without a parent namespace.
|
Namespace(Namespace<E> parent)
Construct a Namespace that uses
parent as a fallback
for finding symbols not defined in this one. |
Modifier and Type | Method and Description |
---|---|
void |
add(E sym)
Adds the object
sym to this Namespace. |
void |
add(java.lang.Iterable<? extends E> list) |
void |
add(Namespace<E> source) |
void |
addSafely(E sym)
Adds the object
sym to this namespace. |
void |
addSafely(java.lang.Iterable<? extends E> names) |
java.util.Collection<E> |
allElements() |
Namespace<E> |
compress() |
boolean |
contains(E var) |
Namespace<E> |
copy() |
java.util.Collection<E> |
elements()
returns list of the elements (not the keys) in this
namespace (not about the one of the parent)
|
Namespace<E> |
extended(E sym)
creates a new Namespace that has this as parent, and contains
an entry for
sym . |
Namespace<E> |
extended(java.lang.Iterable<? extends E> ext) |
void |
flushToParent() |
boolean |
isEmpty() |
boolean |
isSealed() |
E |
lookup(Name name)
looks if a registered object is declared in this namespace, if
negative it asks its parent
|
E |
lookup(java.lang.String name)
Convenience method to look up.
|
protected E |
lookupLocally(Name name) |
Namespace<E> |
parent()
returns the fall-back Namespace of this Namespace, i.e. the one
where symbols are looked up that are not found in this one.
|
void |
remove(Name name)
Remove a name from the namespace.
|
private void |
reset() |
void |
seal() |
<T extends E> |
set(ImmutableSet<T> names) |
Namespace<E> |
simplify() |
java.lang.String |
toString() |
private static final long serialVersionUID
private Namespace<E extends Named> parent
private java.util.Map<Name,E extends Named> symbols
private boolean sealed
public Namespace()
public void add(E sym)
sym
to this Namespace.
If an object with the same name is already there, it is quietly
replaced by sym
. Use addSafely() instead if possible.
TODO:The problem of saving to localSym, symbols, and symbolRefs is not solved yet.
(This is no longer self-explanatory. mu 2016)
If the local table is empty, then the new symbol is added as
"singleton map". This has been adapted from an earlier
implementation, done for memory efficiency reasons: Many namespaces
only contain a single element; no need to allocate a hash map.
The hash map is only created when the 2nd element is added.
This is not threadsafe.public void add(java.lang.Iterable<? extends E> list)
public void addSafely(E sym)
sym
to this namespace.
Throws a runtime exception if an object with the same name is
already there.public void addSafely(java.lang.Iterable<? extends E> names)
public void remove(Name name)
name
- non-null name whose symbol is to be removed.public Namespace<E> extended(E sym)
sym
.public E lookup(Name name)
name
- a Name representing the name of the symbol to look forpublic E lookup(java.lang.String name)
public java.util.Collection<E> elements()
public java.util.Collection<E> allElements()
public Namespace<E> parent()
public java.lang.String toString()
toString
in class java.lang.Object
private void reset()
public <T extends E> void set(ImmutableSet<T> names)
public void seal()
public boolean isEmpty()
public boolean isSealed()
public boolean contains(E var)
public void flushToParent()