Interface ImmutableCollection<E>

Type Parameters:
E - The type of elements in this collection.
All Superinterfaces:
Iterable<E>
All Known Subinterfaces:
ImmutableList<E>, ImmutableSet<E>
All Known Implementing Classes:
ImmutableArrayList, ImmutableHashSet

public interface ImmutableCollection<E> extends Iterable<E>
Immutable collections with a functional API.
Author:
ActiveViam
  • Method Summary

    Modifier and Type
    Method
    Description
    add(E e)
    Create a new collection having the items of this collection plus one new item.
    add(E first, E second, E... rest)
    Create a new collection having the items of this collection plus new items.
    Create a new collection having the items of this collection plus items from another collection.
    addAll(E[] items)
    Create a new collection having the items of this collection plus items from an array.
    addAll(Collection<? extends E> items)
    Create a new collection having the items of this collection plus items from another collection.
    boolean
    contains(E item)
    Indicate if a given item is contained in this collection.
    default boolean
    Indicate if a given item is not contained in this collection.
    default boolean
    every(Predicate<E> predicate)
    Test if all the items of this collection match a given predicate.
    default Optional<E>
    find(Predicate<E> predicate)
    Find the first item that matches a given predicate.
    default int
    findIndex(Predicate<E> predicate)
    Find the number of iterations that were needed to find an item matching the given predicate.
    default E
    Give the first item of this collection.
    flatMap(Function<E,Iterable<R>> mapper)
    Flat map this collection.
    default void
    Apply a function for each item of this collection until this function return false.
    default void
    Apply a function for each item of this collection until this function return false.
    default <R> Optional<R>
    getFirst(Function<E,Optional<R>> optionalResult)
    When all the items of this collection can produce an optional result via a method, returns the first non empty result.
    default boolean
    Indicate if this collection is empty.
    default boolean
    Indicate if this collection contains at least one item.
    default <R> R
    reduce(BiFunction<R,E,R> reducer, R initialValue)
    Apply a reducer to this collection.
    int
    Give the size of this collection.
    default boolean
    some(Predicate<E> predicate)
    Test if one of the items of this collection matches a given predicate.
    E[]
    toArray(E[] a)
    Copy the content of this collection in an array.
    default E[]
    Transforms this collection into an array.
    Create a read-only java util collection from this collection.

    Methods inherited from interface java.lang.Iterable

    forEach, iterator, spliterator
  • Method Details

    • size

      int size()
      Give the size of this collection.
      Returns:
      The size of this collection.
    • add

      Create a new collection having the items of this collection plus one new item.
      Parameters:
      e - The items to add.
      Returns:
      The new collection.
    • add

      ImmutableCollection<E> add(E first, E second, E... rest)
      Create a new collection having the items of this collection plus new items.
      Parameters:
      first - The first item to add.
      second - The second item to add.
      rest - The other items to add.
      Returns:
      The new collection.
    • contains

      boolean contains(E item)
      Indicate if a given item is contained in this collection.
      Parameters:
      item - The item to search for.
      Returns:
      If this item was found in this collection.
      See Also:
    • toCollection

      Collection<E> toCollection()
      Create a read-only java util collection from this collection.
      Returns:
      The collection.
    • flatMap

      <R> ImmutableCollection<R> flatMap(Function<E,Iterable<R>> mapper)
      Flat map this collection.
      Type Parameters:
      R - The type returned by the mapper.
      Parameters:
      mapper - The mapping function.
      Returns:
      The iterable representing the mapped elements.
      See Also:
    • first

      default E first() throws NoSuchElementException
      Give the first item of this collection.
      Returns:
      The first item of this collection.
      Throws:
      NoSuchElementException - If this collection is empty.
    • isEmpty

      default boolean isEmpty()
      Indicate if this collection is empty.
      Returns:
      If this collection is empty.
    • isNotEmpty

      default boolean isNotEmpty()
      Indicate if this collection contains at least one item.
      Returns:
      If this collection contains any item.
    • addAll

      ImmutableCollection<E> addAll(Collection<? extends E> items)
      Create a new collection having the items of this collection plus items from another collection.
      Parameters:
      items - The items to add.
      Returns:
      The new collection.
    • addAll

      default ImmutableCollection<E> addAll(ImmutableCollection<E> items)
      Create a new collection having the items of this collection plus items from another collection.
      Parameters:
      items - The items to add.
      Returns:
      The new collection.
    • addAll

      default ImmutableCollection<E> addAll(E[] items)
      Create a new collection having the items of this collection plus items from an array.
      Parameters:
      items - The items to add.
      Returns:
      The new collection.
    • forEachUntil

      default void forEachUntil(Predicate<E> loop)
      Apply a function for each item of this collection until this function return false.
      Parameters:
      loop - The function to call with each item of this collection.
    • forEach

      default void forEach(BiFunction<E,Integer,Boolean> loop)
      Apply a function for each item of this collection until this function return false.
      Parameters:
      loop - The function to call with each item of this collection and its index.
    • reduce

      default <R> R reduce(BiFunction<R,E,R> reducer, R initialValue)
      Apply a reducer to this collection.
      Type Parameters:
      R - The output type of the reducer.
      Parameters:
      reducer - The reducer.
      initialValue - The initial value.
      Returns:
      The reduced value.
      See Also:
    • some

      default boolean some(Predicate<E> predicate)
      Test if one of the items of this collection matches a given predicate.
      Parameters:
      predicate - The predicate to use.
      Returns:
      If one of the items of this collection matches the predicate.
    • every

      default boolean every(Predicate<E> predicate)
      Test if all the items of this collection match a given predicate.
      Parameters:
      predicate - The predicate to use.
      Returns:
      If all the items of this collection match the predicate.
    • find

      default Optional<E> find(Predicate<E> predicate)
      Find the first item that matches a given predicate.
      Parameters:
      predicate - The predicate with which to test this collection's items.
      Returns:
      The item in an optional. This optional will be empty if no item matched.
    • findIndex

      default int findIndex(Predicate<E> predicate)
      Find the number of iterations that were needed to find an item matching the given predicate.
      Parameters:
      predicate - The predicate with which to test this collection's items.
      Returns:
      The number of iterations, or -1 if no item matched the predicate.
    • doesNotContain

      default boolean doesNotContain(E item)
      Indicate if a given item is not contained in this collection.
      Parameters:
      item - The item to search for.
      Returns:
      If this item was not found in this collection.
      See Also:
    • toArray

      E[] toArray(E[] a)
      Copy the content of this collection in an array.
      Parameters:
      a - The array to attempt copy into.
      Returns:
      An array containing the items of this collection. Can be different than a if a was not large enough.
      See Also:
    • toArray

      default E[] toArray(IntFunction<E[]> a)
      Transforms this collection into an array.
      Parameters:
      a - The function that creates an array of the correct type given its size as input.
      Returns:
      The array containing the items in this collection.
    • getFirst

      default <R> Optional<R> getFirst(Function<E,Optional<R>> optionalResult)
      When all the items of this collection can produce an optional result via a method, returns the first non empty result.
      Type Parameters:
      R - The type of the value contained in the created Optional.
      Parameters:
      optionalResult - The function creating the optional result from an item in the collection. Will only be called while the current result is an empty optional.
      Returns:
      The optional with the first non empty result, or an empty optional if all results were empty.