E
- Element type.public class ScopedList<E>
extends java.lang.Object
implements java.lang.Iterable<E>
Elements can be added to any scope, using the addAt(int, Object)
method. Convenience
methods exist to easily add an element to the global scope or to the current scope, and to
add multiple elements at once. Elements can be removed from any scope, using the removeFrom(int, Object)
method. Convenience methods exist to remove multiple elements at
once.
When a scope is popped, all elements associated with the current scope are removed.
A read-only sublist containing all the nodes in a particular scope is available via the sublistAtScope(int)
method. The number of scopes can be queried with scopes()
.
This class is meant to provide a high-performance implementation of a scoped list which is iterated often, in full, from head to tail.
Modifier and Type | Class and Description |
---|---|
private static class |
ScopedList.Node<E>
A node in the list.
|
private class |
ScopedList.NodeList
A linked list of nodes representing a single scope in the containing, scoped list.
|
Modifier and Type | Field and Description |
---|---|
private ScopedList.Node<E> |
head
Head of the list
|
private java.util.List<ScopedList.NodeList> |
scopes
List of scopes, each of which contain zero or more nodes
|
private int |
size
Number of nodes in the list
|
private ScopedList.Node<E> |
tail
Tail of the list
|
Constructor and Description |
---|
ScopedList()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(boolean global,
E element)
Add the given element to the tail of the current scope (also the tail of the list), or to
the tail of the global (first) scope.
|
void |
addAll(boolean global,
java.util.List<E> elements)
Add all of the given elements, in their current order, to the tail of the current scope
(also the tail of the list), or to the tail of the global (first) scope.
|
void |
addAt(int depth,
E element)
Add the given element to the tail of the scope at the given scope depth, where zero depth
represents the current scope, and a positive depth indicate older scopes.
|
private ScopedList.NodeList |
getScopeAtDepth(int depth)
Get the node list representing the scope at the given depth, where zero depth represents
the current scope, and a positive depth indicate older scopes.
|
java.util.Iterator<E> |
iterator()
Obtain a read-only iterator on the full list of elements, which iterates from head to tail.
|
void |
popScope()
Pop a scope, discarding all the nodes it contains.
|
void |
pushScope()
Push a new, empty scope.
|
int |
removeAllFrom(boolean global,
java.util.List<E> elements)
Remove all of the given elements from the current scope, or from the global (first) scope.
|
boolean |
removeFrom(int depth,
E element)
Remove the given element from the scope at the given depth, where zero depth represents
the current scope, and a positive depth indicate older scopes.
|
int |
scopes()
Return the number of scopes currently open.
|
java.util.List<E> |
sublistAtScope(int depth)
Get an unmodifiable list view of the scope at the given depth, where zero depth represents
the current scope, and a positive depth indicate older scopes.
|
java.lang.String |
toString()
Get a string representation of the contents of the list, primarily for debugging purposes.
|
private final java.util.List<ScopedList.NodeList> scopes
private ScopedList.Node<E> head
private ScopedList.Node<E> tail
private int size
public int scopes()
public void pushScope()
public void popScope()
public void add(boolean global, E element)
global
- true
to add the element to the tail of the global scope;
false
to add the element to the tail of the full list.element
- Element to be added.java.util.NoSuchElementException
- if there is no scope in the list.public void addAt(int depth, E element)
scopes() - 1
indicates the global scope.depth
- Depth of the scope to which to add the element, as described above.element
- Element to be added.java.util.NoSuchElementException
- if there is no scope in the list.java.lang.IndexOutOfBoundsException
- if depth < 0 || depth >= scopes()
.public void addAll(boolean global, java.util.List<E> elements)
global
- true
to add the elements to the tail of the global scope;
false
to add the elements to the tail of the full list.elements
- Elements to be added.java.util.NoSuchElementException
- if there is no scope in the list.public boolean removeFrom(int depth, E element)
scopes()
- 1
indicates the global scope.depth
- Depth of the scope from which to remove the element, as described above.element
- Element to be removed.true
if the element was removed;
false
if the element was not found.public int removeAllFrom(boolean global, java.util.List<E> elements)
global
- true
to remove the elements from the global scope;
false
to remove the elements from the full list.elements
- Elements to be removed.public java.util.List<E> sublistAtScope(int depth)
scopes()
- 1
indicates the global scope.depth
- Scope depth as described above.UnsupportedOperationException
.java.util.NoSuchElementException
- if there is no scope in the list.java.lang.IndexOutOfBoundsException
- if depth < 0 || depth >= scopes()
.public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
public java.lang.String toString()
toString
in class java.lang.Object
private ScopedList.NodeList getScopeAtDepth(int depth)
scopes()
- 1
indicates the global scope.depth
- Scope depth as described above.java.util.NoSuchElementException
- if there is no scope in the list.java.lang.IndexOutOfBoundsException
- if depth < 0 || depth >= scopes()
.