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

VCollections.src.org.violetlib.collections.ICollection Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023 Alan Snyder.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the license agreement. For details see
 * accompanying license terms.
 */

package org.violetlib.collections;

import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.CheckReturnValue;

import org.violetlib.collections.impl.EmptyIIterator;
import org.violetlib.collections.impl.Impl;

import org.jetbrains.annotations.*;

import static java.util.Spliterator.*;

/**
  Common methods for immutable collections.

  @param  The type of the elements.
*/

public @CheckReturnValue interface ICollection
    extends IIterable
{
    /**
      Cast a collection to a specific type. This method is no more reliable than an explicit type cast, but it prevents
      the warning.
    */

    static  @NotNull ICollection cast(@NotNull ICollection o)
    {
        @SuppressWarnings("unchecked")
        ICollection result = (ICollection) o;
        return result;
    }

    static  @NotNull ICollection concatenate(@NotNull IList> sources)
    {
        return Impl.concatenate(sources);
    }

    static  @NotNull IIterator concatenateIterators(@NotNull IList> sources)
    {
        return Impl.concatenateIterators(sources);
    }

    @SafeVarargs
    static  @NotNull ICollection concatenate(ICollection... sources)
    {
        return Impl.concatenate(IList.cast(IList.create(sources)));
    }

    static  @NotNull IIterator emptyIterator()
    {
        return EmptyIIterator.get();
    }

    /**
      Return true if and only if there are no elements in the collection.
    */

    default boolean isEmpty()
    {
        Iterator it = iterator();
        return !it.hasNext();
    }

    /**
      Return the number of elements in the collection.
    */

    default int size()
    {
        int count = 0;
        for (V v : this) {
            ++count;
        }
        return count;
    }

    /**
      Return true if and only if the specified element is an element of the collection.

      @param target The element to find in the list.
      @return true if and only if {@code target} is an element of the collection.
    */

    default boolean contains(@NotNull Object target)
    {
        return find(e -> e.equals(target) ? true : null, false);
    }

    /**
      Visit each element of the collection. The elements are visited in the order defined by the collection (if there is
      one); otherwise, the visitation order is unspecified.

      @param visitor The visitor to call on the elements.
    */

    default void visit(@NotNull Visitor visitor)
    {
        for (V v : this) {
            visitor.visit(v);
        }
    }

    /**
      Visit the elements of the collection until the visitor returns a non-null result. The elements are visited in the
      order defined by the collection (if there is one); otherwise, the visitation order is unspecified.

      @param visitor The visitor to call on the elements.
      @return the first non-null result returned by the visitor, or null if none.
    */

    default  @Nullable R find(@NotNull FindVisitor visitor)
    {
        for (V v : this) {
            R result = visitor.visit(v);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    /**
      Visit each element of the collection until the visitor returns a non-null result. The elements are visited in the
      order defined by the collection (if there is one); otherwise, the visitation order is unspecified.

      @param visitor The visitor to call on the elements.
      @param defaultValue The value to return if no visitor call returns a non-null result.
      @return the first non-null result returned by the visitor, or {@code defaultValue} if none.
    */

    default  @NotNull R find(@NotNull FindVisitor visitor, @NotNull R defaultValue)
    {
        R result = find(visitor);
        return result != null ? result : defaultValue;
    }

    /**
      Return a list containing the members of this collection, sorted in the natural sort order.
      @return the list.
    */

    default @NotNull IList sort()
    {
        return IList.create(this).sort();
    }

    /**
      Return a list containing the same elements as this collection, but sorted using the specified comparator.
      @param c The comparator used to determine the order of the elements in the returned list.
      @return the sorted list.
    */

    default @NotNull IList sort(@NotNull Comparator c)
    {
        return IList.create(this).sort(c);
    }

    /**
      Return a Spliterator that yields the elements of the collection.
    */

    @Override
    default Spliterator spliterator() {
        return Spliterators.spliterator(iterator(), size(), IMMUTABLE | NONNULL | SIZED);
    }

    /**
      Return a stream of the elements of the collection.
    */

    default Stream stream()
    {
        return StreamSupport.stream(spliterator(), false);
    }

    /**
      Return a new Java list containing the elements of this collection, in the same order (if the collection defines an
      order).
    */

    default @NotNull List toJavaList()
    {
        List result = new ArrayList<>();
        for (V v: this) {
            result.add(v);
        }
        return result;
    }

    /**
      Return a new unmodifiable Java list containing the elements of this collection, in the same order (if the
      collection defines an order).
    */

    default @NotNull List toJavaUnmodifiableList()
    {
        return Collections.unmodifiableList(toJavaList());
    }

    /**
      Return a new Java set containing the elements of this collection.
    */

    default @NotNull Set toJavaSet()
    {
        Set result = new HashSet<>();
        for (V v: this) {
            result.add(v);
        }
        return result;
    }

    /**
      Return a new Java set containing the elements of this collection.
    */

    default @NotNull Set toJavaUnmodifiableSet()
    {
        return Collections.unmodifiableSet(toJavaSet());
    }

    /**
      Return a new Java array containing the elements of this collection.
    */

    default  @NotNull E[] toJavaArray(@NotNull E[] template)
    {
        return toJavaList().toArray(template);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy