All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pkl.thirdparty.paguro.collections.UnmodIterator Maven / Gradle / Ivy

Go to download

Shaded fat Jar for pkl-config-java, a Java config library based on the Pkl config language.

There is a newer version: 0.27.1
Show newest version
package org.pkl.thirdparty.paguro.collections;

import org.pkl.thirdparty.jetbrains.annotations.Contract;
import org.pkl.thirdparty.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 A one-time use, mutable, not-thread-safe way to get each value of the underling collection in
 turn. I experimented with various thread-safe alternatives, but the JVM is optimized around
 iterators so this is the lowest common denominator of collection iteration, even though
 iterators are inherently mutable.

 This is called "Unmod" in the sense that it doesn't modify the underlying collection.  Iterators
 are inherently mutable.  The only safe way to handle them is to pass around IteraBLEs so that
 the ultimate client gets its own, unshared iteraTOR.  Order is not guaranteed.
 */
public interface UnmodIterator extends Iterator {
    // ========================================= Instance =========================================
//default void forEachRemaining(Consumer action)
//boolean hasNext()
//E next()

    /** Not allowed - this is supposed to be unmodifiable */
    @Override
    @Deprecated
    default void remove() {
        throw new UnsupportedOperationException("Modification attempted");
    }

    /** Instead of calling this directly, please use {@link #emptyUnmodIterator()} instead */
    enum UnIterator implements UnmodIterator {
        EMPTY {
            @Override
            @Contract(pure = true)
            public boolean hasNext() { return false; }
            @Override public Object next() {
                throw new NoSuchElementException("Can't call next() on an empty iterator");
            }
        }
    }

    /** Returns the empty unmodifiable iterator.*/
    @SuppressWarnings("unchecked")
    static  @NotNull UnmodIterator emptyUnmodIterator() {
        return (UnmodIterator) UnIterator.EMPTY;
    }
}