package de.geolykt.starloader.util; import java.util.Iterator; import java.util.NoSuchElementException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; interface CollectionNode { static class NodeIterator implements Iterator { @NotNull private final CollectionNode root; @Nullable private CollectionNode head; @Nullable private CollectionNode previous; @Nullable private CollectionNode preprevious; public NodeIterator(@NotNull CollectionNode root) { this.head = this.root = root; } public NodeIterator(@NotNull CollectionNode root, @NotNull CollectionNode head) { this.root = root; this.head = head; } @Override public boolean hasNext() { return this.head != null; } @Override public T next() { CollectionNode head = this.head; if (head == null) { throw new NoSuchElementException(); } this.head = head.next(); this.preprevious = this.previous; this.previous = head; return head.get(); } @Override public void remove() { if (this.previous == null) { throw new NoSuchElementException("#next not called"); } synchronized (this.root) { CollectionNode preprevious = this.preprevious; if (preprevious == null) { this.root.setNext(this.head); } else { preprevious.setNext(this.head); } } } } @Nullable public CollectionNode next(); @NotNull public E get(); public void setNext(@Nullable CollectionNode next); }