Class Iterables
java.lang.Object
com.globalmentor.collections.iterables.Iterables
Various utilities to be used with iterables.
- Author:
- Garret Wilson
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T>
Returns anOptional
describing the first element of this iterable, or an emptyOptional
if the iterable is empty.static <T> Optional<T>
Returns anOptional
describing the first and only element of this iterable, or an emptyOptional
if the iterable is empty.static <T,
X extends RuntimeException>
Optional<T>Returns anOptional
describing the first and only element of this iterable, or an emptyOptional
if the iterable is empty.static <E> E
Retrieves the one and only one element expected to be in the iterable.static <E,
X extends RuntimeException>
ERetrieves the one and only one element expected to be in the iterable.
-
Constructor Details
-
Iterables
public Iterables()
-
-
Method Details
-
findFirst
Returns anOptional
describing the first element of this iterable, or an emptyOptional
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 toIterators.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 emptyOptional
if the iterable is empty. - See Also:
-
findOnly
Returns anOptional
describing the first and only element of this iterable, or an emptyOptional
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 toIterators.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 emptyOptional
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 anOptional
describing the first and only element of this iterable, or an emptyOptional
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 toIterators.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 emptyOptional
if the iterable is empty. - Throws:
RuntimeException
- if the given stream has more than one element.- See Also:
-
getOnly
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 elementsIllegalArgumentException
- 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 elementsRuntimeException
- if the given iterable has more than one element.
-