forEachEntry method Null safety

void forEachEntry(
  1. void action(
    1. DoubleLinkedQueueEntry<E> element
    )
)

Calls action for each entry object of this double-linked queue.

Each element of the queue has an associated DoubleLinkedQueueEntry. This method iterates the entry objects from first to last and calls action with each object in turn.

The entry objects can also be accessed using firstEntry and lastEntry, and iterated using DoubleLinkedQueueEntry.nextEntry() and DoubleLinkedQueueEntry.previousEntry().

The action function can use methods on DoubleLinkedQueueEntry to remove the entry or it can insert elements before or after the entry. If the current entry is removed, iteration continues with the entry that was following the current entry when action was called. Any elements inserted after the current element before it is removed will not be visited by the iteration.

Implementation

void forEachEntry(void action(DoubleLinkedQueueEntry<E> element)) {
  var cursor = _sentinel._nextLink!;
  while (true) {
    var element = cursor._asNonSentinelEntry();
    if (element == null) break;
    if (!identical(element._queue, this)) {
      throw ConcurrentModificationError(this);
    }
    cursor = cursor._nextLink!;
    // Remember both element and element._nextLink (as cursor).
    // If someone calls `element.remove()` we continue from `next`.
    // Otherwise we use the value of element._nextLink which may have been
    // updated.
    action(element);
    if (identical(this, element._queue)) {
      cursor = element._nextLink!;
    }
  }
}