Class Iterables

java.lang.Object
com.globalmentor.collections.iterables.Iterables

public class Iterables extends Object
Various utilities to be used with iterables.
Author:
Garret Wilson
See Also:
  • Constructor Details

    • Iterables

      public Iterables()
  • Method Details

    • findFirst

      public static <T> Optional<T> findFirst(@Nonnull Iterable<T> iterable)
      Returns an Optional describing the first element of this iterable, or an empty Optional if the iterable is empty.
      Implementation Specification:
      This implementation efficiently short-circuits and avoids creating an iterator if the iterable is an instance of a Collection, which has a known size. Otherwise this implementation delegates to Iterators.findNext(Iterator).
      Type Parameters:
      T - the type of elements returned by the iterator.
      Parameters:
      iterable - The iterable from which the first object should be retrieved.
      Returns:
      An Optional describing the first element of this iterable, or an empty Optional if the iterable is empty.
      See Also:
    • findOnly

      public static <T> Optional<T> findOnly(@Nonnull Iterable<T> iterable)
      Returns an Optional describing the first and only element of this iterable, or an empty Optional if the iterable is empty.
      Implementation Specification:
      This implementation efficiently short-circuits and avoids creating an iterator if the iterable is an instance of a Collection, which has a known size. Otherwise this implementation delegates to Iterators.findOnly(Iterator).
      Type Parameters:
      T - The type of elements returned by the iterable.
      Parameters:
      iterable - The iterable from which the only object should be retrieved.
      Returns:
      An Optional describing the only element of this iterable, or an empty Optional if the iterable is empty.
      Throws:
      IllegalArgumentException - if the given stream has more than one element.
      See Also:
    • findOnly

      public static <T, X extends RuntimeException> Optional<T> findOnly(@Nonnull Iterable<T> iterable, @Nonnull Supplier<X> manyElementsExceptionSupplier)
      Returns an Optional describing the first and only element of this iterable, or an empty Optional if the iterable is empty.
      Implementation Specification:
      This implementation efficiently short-circuits and avoids creating an iterator if the iterable is an instance of a Collection, which has a known size. Otherwise this implementation delegates to Iterators.findOnly(Iterator, Supplier).
      Type Parameters:
      T - The type of elements returned by the iterable.
      X - The type of exception to be thrown if there are many elements.
      Parameters:
      iterable - The iterable from which the only object should be retrieved.
      manyElementsExceptionSupplier - The strategy for creating an exception to throw if more than one element is present.
      Returns:
      An Optional describing the only element of this iterable, or an empty Optional if the iterable is empty.
      Throws:
      RuntimeException - if the given stream has more than one element.
      See Also:
    • getOnly

      public static <E> E getOnly(@Nonnull Iterable<E> iterable)
      Retrieves the one and only one element expected to be in the iterable.
      Implementation Specification:
      This implementation delegates to Iterators.getOnly(Iterator).
      Type Parameters:
      E - The type of element in the iterable.
      Parameters:
      iterable - The iterable from which the element will be retrieved.
      Returns:
      The one and only one element in the iterable.
      Throws:
      NoSuchElementException - if the iterable has no more elements
      IllegalArgumentException - if the given iterable has more than one element.
    • getOnly

      public static <E, X extends RuntimeException> E getOnly(@Nonnull Iterable<E> iterable, @Nonnull Supplier<X> manyElementsExceptionSupplier)
      Retrieves the one and only one element expected to be in the iterable.
      Implementation Specification:
      This implementation delegates to Iterators.getOnly(Iterator, Supplier).
      Type Parameters:
      E - The type of element in the iterable.
      X - The type of exception to be thrown if there are many elements.
      Parameters:
      iterable - The iterable from which the element will be retrieved.
      manyElementsExceptionSupplier - The strategy for creating an exception to throw if more than one element is present.
      Returns:
      The one and only one element in the iterable.
      Throws:
      NoSuchElementException - if the iterable has no more elements
      RuntimeException - if the given iterable has more than one element.