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>

public class CachedIteratorCollection<E>extends AbstractCollection<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.

  • 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 _cleared
      True after clear() 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 size
      Size of the collection.
    • first

      First node in the linked list.
    • last

      Last node in the linked list.
  • Constructor Details

    • CachedIteratorCollection

      public CachedIteratorCollection()
      Constructs an empty collection.
  • Method Details

    • add

      public boolean add(E element)
      Adds a data object (element) as a Node and sets previous/next pointers accordingly.
      Specified by:
      add in interface Collection<E>
      Overrides:
      add in class AbstractCollection<E>
    • clear

      public void clear()
      Clears the collection, all pointers reset to 'null'.
      Specified by:
      clear in interface Collection<E>
      Overrides:
      clear in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Remove the first element matching by identity (==). Used by PeerState.acked() to avoid iterating with early-exit break.
      Specified by:
      remove in interface Collection<E>
      Overrides:
      remove in class AbstractCollection<E>
      Returns:
      true if removed
    • unlink

      private void unlink(CachedIteratorCollection.Node<E> x)
      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

      public Iterator<E> 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:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<E>
      Returns:
      a CachedIteratorCollection.CachedIterator instance, reset to the beginning
    • size

      public int size()
      Return size of current collection.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>