package org.xbib.datastructures.immutable;

import java.lang.reflect.Array;
import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import static org.xbib.datastructures.immutable.ImmutableList.castOrCopyToCollection;

/**
 * A {@link Collection} whose contents will never change, and which offers a few additional
 * guarantees detailed below.
 *
 * <p><b>Warning:</b> avoid <i>direct</i> usage of {@link ImmutableCollection} as a type (just as
 * with {@link Collection} itself). Prefer subtypes such as {@link ImmutableSet} or {@link
 * ImmutableList}, which have well-defined {@link #equals} semantics, thus avoiding a common source
 * of bugs and confusion.
 *
 * <h3>About <i>all</i> {@code Immutable-} collections</h3>
 *
 * <p>The remainder of this documentation applies to every public {@code Immutable-} type in this
 * package, whether it is a subtype of {@code ImmutableCollection} or not.
 *
 * <h4>Guarantees</h4>
 *
 * <p>Each makes the following guarantees:
 *
 * <ul>
 *   <li><b>Shallow immutability.</b> Elements can never be added, removed or replaced in this
 *       collection. This is a stronger guarantee than that of {@link
 *       Collections#unmodifiableCollection}, whose contents change whenever the wrapped collection
 *       is modified.
 *   <li><b>Null-hostility.</b> This collection will never contain a null element.
 *   <li><b>Deterministic iteration.</b> The iteration order is always well-defined, depending on
 *       how the collection was created. Typically this is insertion order unless an explicit
 *       ordering is otherwise specified (e.g. {@link ImmutableSortedSet#naturalOrder}). See the
 *       appropriate factory method for details. View collections iterate in the same order as
 *       the parent, except as noted.
 *   <li><b>Thread safety.</b> It is safe to access this collection concurrently from multiple
 *       threads.
 *   <li><b>Integrity.</b> This type cannot be subclassed outside this package (which would allow
 *       these guarantees to be violated).
 * </ul>
 *
 * <h4>"Interfaces", not implementations</h4>
 *
 * <p>These are classes instead of interfaces to prevent external subtyping, but should be thought
 * of as interfaces in every important sense. Each public class such as {@link ImmutableSet} is a
 * <i>type</i> offering meaningful behavioral guarantees. This is substantially different from the
 * case of (say) {@link HashSet}, which is an <i>implementation</i>, with semantics that were
 * largely defined by its supertype.
 *
 * <p>For field types and method return types, you should generally use the immutable type (such as
 * {@link ImmutableList}) instead of the general collection interface type (such as {@link List}).
 * This communicates to your callers all of the semantic guarantees listed above, which is almost
 * always very useful information.
 *
 * <p>On the other hand, a <i>parameter</i> type of {@link ImmutableList} is generally a nuisance to
 * callers. Instead, accept {@link Iterable} and have your method or constructor body pass it to the
 * appropriate {@code copyOf} method itself.
 *
 * <p>Expressing the immutability guarantee directly in the type that user code references is a
 * powerful advantage. Although Java offers certain immutable collection factory methods, such as
 * {@link Collections#singleton(Object)} and <a
 * href="https://docs.oracle.com/javase/9/docs/api/java/util/Set.html#immutable">{@code Set.of}</a>,
 * we recommend using <i>these</i> classes instead for this reason (as well as for consistency).
 *
 * <h4>Creation</h4>
 *
 * <p>Except for logically "abstract" types like {@code ImmutableCollection} itself, each {@code
 * Immutable} type provides the static operations you need to obtain instances of that type. These
 * usually include:
 *
 * <ul>
 *   <li>Static methods named {@code of}, accepting an explicit list of elements or entries.
 *   <li>Static methods named {@code copyOf} (or {@code copyOfSorted}), accepting an existing
 *       collection whose contents should be copied.
 *   <li>A static nested {@code Builder} class which can be used to populate a new immutable
 *       instance.
 * </ul>
 *
 * <h4>Warnings</h4>
 *
 * <ul>
 *   <li><b>Warning:</b> as with any collection, it is almost always a bad idea to modify an element
 *       (in a way that affects its {@link Object#equals} behavior) while it is contained in a
 *       collection. Undefined behavior and bugs will result. It's generally best to avoid using
 *       mutable objects as elements at all, as many users may expect your "immutable" object to be
 *       <i>deeply</i> immutable.
 * </ul>
 *
 * <h4>Performance notes</h4>
 *
 * <ul>
 *   <li>Implementations can be generally assumed to prioritize memory efficiency, then speed of
 *       access, and lastly speed of creation.
 *   <li>The {@code copyOf} methods will sometimes recognize that the actual copy operation is
 *       unnecessary; for example, {@code copyOf(copyOf(anArrayList))} should copy the data only
 *       once. This reduces the expense of habitually making defensive copies at API boundaries.
 *       However, the precise conditions for skipping the copy operation are undefined.
 *   <li><b>Warning:</b> a view collection such as {@link ImmutableMap#keySet()} or {@link
 *       ImmutableList#subList} may retain a reference to the entire data set, preventing it from
 *       being garbage collected. If some of the data is no longer reachable through other means,
 *       this constitutes a memory leak. Pass the view collection to the appropriate {@code copyOf}
 *       method to obtain a correctly-sized copy.
 *   <li>The performance of using the associated {@code Builder} class can be assumed to be no
 *       worse, and possibly better, than creating a mutable collection and copying it.
 *   <li>Implementations generally do not cache hash codes. If your element or key type has a slow
 *       {@code hashCode} implementation, it should cache it itself.
 * </ul>
 *
 * <h4>Example usage</h4>
 *
 * <pre>{@code
 * class Foo {
 *   private static final ImmutableSet<String> RESERVED_CODES =
 *       ImmutableSet.of("AZ", "CQ", "ZX");
 *
 *   private final ImmutableSet<String> codes;
 *
 *   public Foo(Iterable<String> codes) {
 *     this.codes = ImmutableSet.copyOf(codes);
 *     checkArgument(Collections.disjoint(this.codes, RESERVED_CODES));
 *   }
 * }
 * }</pre>
 */
public abstract class ImmutableCollection<E> extends AbstractCollection<E> {
    /*
     * We expect SIZED (and SUBSIZED, if applicable) to be added by the spliterator factory methods.
     * These are properties of the collection as a whole; SIZED and SUBSIZED are more properties of
     * the spliterator implementation.
     */
    static final int SPLITERATOR_CHARACTERISTICS =
            Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED;

    ImmutableCollection() {
    }

    /**
     * Returns an unmodifiable iterator across the elements in this collection.
     */
    @Override
    public abstract UnmodifiableIterator<E> iterator();

    @Override
    public Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, SPLITERATOR_CHARACTERISTICS);
    }

    private static final Object[] EMPTY_ARRAY = {};

    @Override
    public final Object[] toArray() {
        return toArray(EMPTY_ARRAY);
    }

    @Override
    /*
     * This suppression is here for two reasons:
     *
     * 1. b/192354773 in our checker affects toArray declarations.
     *
     * 2. `other[size] = null` is unsound. We could "fix" this by requiring callers to pass in an
     * array with a nullable element type. But probably they usually want an array with a non-nullable
     * type. That said, we could *accept* a `T[]` (which, given that we treat arrays as
     * covariant, would still permit a plain `T[]`) and return a plain `T[]`. But of course that would
     * require its own suppression, since it is also unsound. toArray(T[]) is just a mess from a
     * nullness perspective. The signature below at least has the virtue of being relatively simple.
     */
    @SuppressWarnings("nullness")
    public final <T extends Object> T[] toArray(T[] other) {
        Objects.requireNonNull(other);
        int size = size();

        if (other.length < size) {
            Object[] internal = internalArray();
            if (internal != null) {
                return copy(internal, internalArrayStart(), internalArrayEnd(), other);
            }
            other = newArray(other, size);
        } else if (other.length > size) {
            other[size] = null;
        }
        copyIntoArray(other, 0);
        return other;
    }

    /**
     * If this collection is backed by an array of its elements in insertion order, returns it.
     */
    Object[] internalArray() {
        return null;
    }

    /**
     * If this collection is backed by an array of its elements in insertion order, returns the offset
     * where this collection's elements start.
     */
    int internalArrayStart() {
        throw new UnsupportedOperationException();
    }

    /**
     * If this collection is backed by an array of its elements in insertion order, returns the offset
     * where this collection's elements end.
     */
    int internalArrayEnd() {
        throw new UnsupportedOperationException();
    }

    @Override
    public abstract boolean contains(Object object);

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean add(E e) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean remove(Object object) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean addAll(Collection<? extends E> newElements) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean removeAll(Collection<?> oldElements) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean removeIf(Predicate<? super E> filter) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     */
    @Override
    public final boolean retainAll(Collection<?> elementsToKeep) {
        throw new UnsupportedOperationException();
    }

    /**
     * Guaranteed to throw an exception and leave the collection unmodified.
     *
     * @throws UnsupportedOperationException always
     * @deprecated Unsupported operation.
     */
    @Deprecated
    @Override
    public final void clear() {
        throw new UnsupportedOperationException();
    }

    /**
     * Returns an {@code ImmutableList} containing the same elements, in the same order, as this
     * collection.
     *
     * <p><b>Performance note:</b> in most cases this method can return quickly without actually
     * copying anything. The exact circumstances under which the copy is performed are undefined and
     * subject to change.
     */
    public ImmutableList<E> asList() {
        switch (size()) {
            case 0:
                return ImmutableList.of();
            case 1:
                return ImmutableList.of(iterator().next());
            default:
                return new RegularImmutableAsList<E>(this, toArray());
        }
    }

    /**
     * Returns {@code true} if this immutable collection's implementation contains references to
     * user-created objects that aren't accessible via this collection's methods. This is generally
     * used to determine whether {@code copyOf} implementations should make an explicit copy to avoid
     * memory leaks.
     */
    abstract boolean isPartialView();

    /**
     * Copies the contents of this immutable collection into the specified array at the specified
     * offset. Returns {@code offset + size()}.
     */
    int copyIntoArray(Object[] dst, int offset) {
        for (E e : this) {
            dst[offset++] = e;
        }
        return offset;
    }

    /**
     * Abstract base class for builders of {@link ImmutableCollection} types.
     */
    public abstract static class Builder<E> {
        static final int DEFAULT_INITIAL_CAPACITY = 4;

        static int expandedCapacity(int oldCapacity, int minCapacity) {
            if (minCapacity < 0) {
                throw new AssertionError("cannot store more than MAX_VALUE elements");
            }
            // careful of overflow!
            int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
            if (newCapacity < minCapacity) {
                newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
            }
            if (newCapacity < 0) {
                newCapacity = Integer.MAX_VALUE;
                // guaranteed to be >= newCapacity
            }
            return newCapacity;
        }

        Builder() {
        }

        /**
         * Adds {@code element} to the {@code ImmutableCollection} being built.
         *
         * <p>Note that each builder class covariantly returns its own type from this method.
         *
         * @param element the element to add
         * @return this {@code Builder} instance
         * @throws NullPointerException if {@code element} is null
         */
        public abstract Builder<E> add(E element);

        /**
         * Adds each element of {@code elements} to the {@code ImmutableCollection} being built.
         *
         * <p>Note that each builder class overrides this method in order to covariantly return its own
         * type.
         *
         * @param elements the elements to add
         * @return this {@code Builder} instance
         * @throws NullPointerException if {@code elements} is null or contains a null element
         */
        public Builder<E> add(E... elements) {
            for (E element : elements) {
                add(element);
            }
            return this;
        }

        /**
         * Adds each element of {@code elements} to the {@code ImmutableCollection} being built.
         *
         * <p>Note that each builder class overrides this method in order to covariantly return its own
         * type.
         *
         * @param elements the elements to add
         * @return this {@code Builder} instance
         * @throws NullPointerException if {@code elements} is null or contains a null element
         */
        public Builder<E> addAll(Iterable<? extends E> elements) {
            for (E element : elements) {
                add(element);
            }
            return this;
        }

        /**
         * Adds each element of {@code elements} to the {@code ImmutableCollection} being built.
         *
         * <p>Note that each builder class overrides this method in order to covariantly return its own
         * type.
         *
         * @param elements the elements to add
         * @return this {@code Builder} instance
         * @throws NullPointerException if {@code elements} is null or contains a null element
         */
        public Builder<E> addAll(Iterator<? extends E> elements) {
            while (elements.hasNext()) {
                add(elements.next());
            }
            return this;
        }

        /**
         * Returns a newly-created {@code ImmutableCollection} of the appropriate type, containing the
         * elements provided to this builder.
         *
         * <p>Note that each builder class covariantly returns the appropriate type of {@code
         * ImmutableCollection} from this method.
         */
        public abstract ImmutableCollection<E> build();
    }

    /**
     * Equivalent to Arrays.copyOfRange(source, from, to, arrayOfType.getClass()).
     */
    /*
     * Arrays are a mess from a nullness perspective, and Class instances for object-array types are
     * even worse. For now, we just suppress and move on with our lives.
     *
     * - https://github.com/jspecify/jspecify/issues/65
     *
     * - https://github.com/jspecify/jdk/commit/71d826792b8c7ef95d492c50a274deab938f2552
     */
    @SuppressWarnings("nullness")
    private static <T extends Object> T[] copy(Object[] source, int from, int to, T[] arrayOfType) {
        return Arrays.copyOfRange(source, from, to, (Class<? extends T[]>) arrayOfType.getClass());
    }

    public static <T> T[] newArray(Class<T> type, int length) {
        return (T[]) Array.newInstance(type, length);
    }

    /**
     * Returns a new array of the given length with the same type as a reference array.
     *
     * @param reference any array of the desired type
     * @param length    the length of the new array
     */
    public static <T extends Object> T[] newArray(T[] reference, int length) {
        Class<?> type = reference.getClass().getComponentType();
        // the cast is safe because
        // result.getClass() == reference.getClass().getComponentType()
        @SuppressWarnings("unchecked")
        T[] result = (T[]) Array.newInstance(type, length);
        return result;
    }

    protected static <T extends Object> Spliterator<T> indexed(int size,
                                                               int extraCharacteristics,
                                                               IntFunction<T> function) {
        return indexed(size, extraCharacteristics, function, null);
    }

    protected static <T extends Object> Spliterator<T> indexed(int size,
                                                               int extraCharacteristics,
                                                               IntFunction<T> function, Comparator<? super T> comparator) {
        if (comparator != null) {
            if ((extraCharacteristics & Spliterator.SORTED) != 0) {
                throw new IllegalArgumentException();
            }
        }
        class WithCharacteristics implements Spliterator<T> {
            private final Spliterator.OfInt delegate;

            WithCharacteristics(Spliterator.OfInt delegate) {
                this.delegate = delegate;
            }

            @Override
            public boolean tryAdvance(Consumer<? super T> action) {
                return delegate.tryAdvance((IntConsumer) i -> action.accept(function.apply(i)));
            }

            @Override
            public void forEachRemaining(Consumer<? super T> action) {
                delegate.forEachRemaining((IntConsumer) i -> action.accept(function.apply(i)));
            }

            @Override
            public Spliterator<T> trySplit() {
                Spliterator.OfInt split = delegate.trySplit();
                return (split == null) ? null : new WithCharacteristics(split);
            }

            @Override
            public long estimateSize() {
                return delegate.estimateSize();
            }

            @Override
            public int characteristics() {
                return Spliterator.ORDERED
                        | Spliterator.SIZED
                        | Spliterator.SUBSIZED
                        | extraCharacteristics;
            }

            @Override
            public Comparator<? super T> getComparator() {
                if (hasCharacteristics(Spliterator.SORTED)) {
                    return comparator;
                } else {
                    throw new IllegalStateException();
                }
            }
        }
        return new WithCharacteristics(IntStream.range(0, size).spliterator());
    }

    /**
     * Returns an iterator containing the elements of {@code array} in order. The returned iterator is
     * a view of the array; subsequent changes to the array will be reflected in the iterator.
     *
     * <p><b>Note:</b> It is often preferable to represent your data using a collection type, for
     * example using {@link Arrays#asList(Object[])}, making this method unnecessary.
     *
     * <p>The {@code Iterable} equivalent of this method is either {@link Arrays#asList(Object[])},
     * {@link ImmutableList#copyOf(Object[])}}, or {@link ImmutableList#of}.
     */
    @SafeVarargs
    protected static <T extends Object> UnmodifiableIterator<T> forArray(T... array) {
        return forArray(array, 0, array.length, 0);
    }

    /**
     * Returns a list iterator containing the elements in the specified range of {@code array} in
     * order, starting at the specified index.
     *
     * <p>The {@code Iterable} equivalent of this method is {@code
     * Arrays.asList(array).subList(offset, offset + length).listIterator(index)}.
     */
    protected static <T extends Object> UnmodifiableListIterator<T> forArray(T[] array,
                                                                             int offset, int length, int index) {
        if (length < 0) {
            throw new IllegalArgumentException();
        }
        int end = offset + length;

        // Technically we should give a slightly more descriptive error on overflow
        checkPositionIndexes(offset, end, array.length);
        checkPositionIndex(index, length, "index");
        if (length == 0) {
            return emptyListIterator();
        }
        return new ArrayItr<>(array, offset, length, index);
    }

    @SuppressWarnings("unchecked")
    private static <T extends Object> UnmodifiableListIterator<T> emptyListIterator() {
        return (UnmodifiableListIterator<T>) ArrayItr.EMPTY;
    }

    protected static int checkPositionIndex(int index, int size, String desc) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
        }
        return index;
    }

    protected static void checkPositionIndexes(int start, int end, int size) {
        if (start < 0 || end < start || end > size) {
            throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size));
        }
    }

    private static String badPositionIndexes(int start, int end, int size) {
        if (start < 0 || start > size) {
            return badPositionIndex(start, size, "start index");
        }
        if (end < 0 || end > size) {
            return badPositionIndex(end, size, "end index");
        }
        // end < start
        return String.format("end index (%s) must not be less than start index (%s)", end, start);
    }

    private static String badPositionIndex(int index, int size, String desc) {
        if (index < 0) {
            return String.format("%s (%s) must not be negative", desc, index);
        } else if (size < 0) {
            throw new IllegalArgumentException("negative size: " + size);
        } else { // index > size
            return String.format("%s (%s) must not be greater than size (%s)", desc, index, size);
        }
    }

    protected static final class ArrayItr<T extends Object>
            extends AbstractIndexedListIterator<T> {
        static final UnmodifiableListIterator<Object> EMPTY = new ArrayItr<>(new Object[0], 0, 0, 0);

        private final T[] array;
        private final int offset;

        ArrayItr(T[] array, int offset, int length, int index) {
            super(length, index);
            this.array = array;
            this.offset = offset;
        }

        @Override
        protected T get(int index) {
            return array[offset + index];
        }
    }

    public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
        return toArray(iterable, newArray(type, 0));
    }

    public static <T extends Object> T[] toArray(Iterable<? extends T> iterable, T[] array) {
        Collection<? extends T> collection = castOrCopyToCollection(iterable);
        return collection.toArray(array);
    }
}
