java.lang.Object
java.util.AbstractCollection<E>
net.i2p.router.util.CachedIteratorCollection<E>
- Type Parameters:
E- type of elements in this collection
- All Implemented Interfaces:
Iterable<E>, Collection<E>
Synchronized collection with per-thread cached iterators for efficient element iteration.
Extends AbstractCollection to provide a collection that can be iterated without creating new iterator objects per call. Uses a linked list structure with per-thread cached iterator instances to minimize object churn during frequent iteration operations.
All mutation and iteration methods are synchronized for thread safety. Nested iteration from the same thread allocates a new iterator.
Nested Class Summary
Modifier and TypeClassDescriptionstatic classStatic inner CachedIterator class - implements hasNext(),CachedIteratorCollection.CachedIterator.next()& remove()private static classNode object that contains: (1) Data object (2) Link to previous Node object (3) Link to next Node object.Field Summary
FieldsModifier and TypeFieldDescription(package private) booleanTrue afterclear()to signal iterator instances the collection is gone.The per-thread cached iterators, weakly referenced so stale thread local entries do not retain the collection and its owning peer state after a clear.(package private) CachedIteratorCollection.Node<E> First node in the linked list.(package private) CachedIteratorCollection.Node<E> Last node in the linked list.(package private) intSize of the collection.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionbooleanAdds a data object (element) as a Node and sets previous/next pointers accordingly.voidclear()Clears the collection, all pointers reset to 'null'.iterator()Returns a cached iterator over the elements in this collection.voidReset the current thread's cached iterator so it is no longer considered "in use".booleanRemove the first element matching by identity (==).intsize()Return size of current collection.private voidUnlink a node from the doubly-linked list.Methods inherited from class AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
Field Details
_iterator
The per-thread cached iterators, weakly referenced so stale thread local entries do not retain the collection and its owning peer state after a clear._cleared
volatile boolean _clearedTrue afterclear()to signal iterator instances the collection is gone. The thread local map entries of other threads are cleaned lazily through the weak reference key.size
int sizeSize of the collection.first
CachedIteratorCollection.Node<E> firstFirst node in the linked list.last
Last node in the linked list.
Constructor Details
CachedIteratorCollection
public CachedIteratorCollection()Constructs an empty collection.
Method Details
add
Adds a data object (element) as a Node and sets previous/next pointers accordingly.- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollection<E>
clear
public void clear()Clears the collection, all pointers reset to 'null'.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
remove
Remove the first element matching by identity (==). Used byPeerState.acked()to avoid iterating with early-exit break.- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- Returns:
- true if removed
unlink
Unlink a node from the doubly-linked list.- Parameters:
x- the node to unlink
releaseCurrentThreadIterator
public void releaseCurrentThreadIterator()Reset the current thread's cached iterator so it is no longer considered "in use". Call this before any early return from an iteration loop that may not exhaust the iterator.iterator
Returns a cached iterator over the elements in this collection.Each thread gets its own iterator instance that is reused and reset on each call. If the current thread's cached iterator is still in use (hasNext() not yet exhausted), a new iterator is allocated to allow safe nested iteration.
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- a
CachedIteratorCollection.CachedIteratorinstance, reset to the beginning
size
public int size()Return size of current collection.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>