almost.functional.utils
public final class Iterables extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
any(java.lang.Iterable<T> iterable,
Predicate<? super T> predicate)
Determine if any element of an iterable matches a given predicate.
|
static <T> boolean |
contains(java.lang.Iterable<T> iterable,
T value)
Does an iterable contain a value as determined by Object.isEqual(Object, Object).
|
static <T> java.lang.Iterable<T> |
filter(java.lang.Iterable<T> fromIterable,
Predicate<? super T> predicate)
Create an iterable that filters an existing iterable based on a predicate.
|
static <T> Optional<T> |
find(java.lang.Iterable<? extends T> iterable,
Predicate<? super T> predicate)
Apply a predicate to an iterable, returning an optional of the first element where the predicate is true,
or empty if no true is found.
|
static <T> void |
forEach(java.lang.Iterable<T> iterable,
Consumer<? super T> consumer)
Accept a consumer for each element of an iterable.
|
static <E> Optional<E> |
get(java.lang.Iterable<E> iterable,
int position)
Return an optional of an element from a specified position in an iterable.
|
static <E> java.lang.Iterable<E> |
iterable(java.util.Enumeration<E> enumeration)
Convert an enumeration to an iterator.
|
static <E> Optional<E> |
last(java.lang.Iterable<E> iterable)
Return the last element of an iterable, or empty if the iterable is empty.
|
static <F,T> java.lang.Iterable<T> |
map(java.lang.Iterable<F> fromIterable,
Function<? super F,? extends T> function)
Create an iterable that maps an existing iterable to a new one by applying a function to each of it's elements.
|
static <T,R> R |
reduce(java.lang.Iterable<T> iterable,
R initial,
BiFunction<R,? super T,R> accumulator)
Perform a reduction of an iterable, using an initial value, and a two argument function.
|
public static <T> void forEach(java.lang.Iterable<T> iterable, Consumer<? super T> consumer)
T
- the type of the iterable and consumerconsumer
- the consumer to accept for each elementiterable
- the iterablepublic static <T,R> R reduce(java.lang.Iterable<T> iterable, R initial, BiFunction<R,? super T,R> accumulator)
T
- type of the iterable elements and accumulator's second argumentR
- type returned by reduce, the accumulator and it's first argumentiterable
- the iterable.initial
- the initial value of the first argument.accumulator
- the two argument function.public static <T> Optional<T> find(java.lang.Iterable<? extends T> iterable, Predicate<? super T> predicate)
T
- they type of the iterable and predicateiterable
- the iterable to traversepredicate
- the predicate to testpublic static <T> boolean any(java.lang.Iterable<T> iterable, Predicate<? super T> predicate)
T
- the type of the iterable and predicateiterable
- the iterable to traversepredicate
- the predicate to testpublic static <T> boolean contains(java.lang.Iterable<T> iterable, T value)
T
- the type of the iterable and objectiterable
- the iterablevalue
- the valuepublic static <F,T> java.lang.Iterable<T> map(java.lang.Iterable<F> fromIterable, Function<? super F,? extends T> function)
F
- the type of the original elements, and the argument to the functionT
- the type of the resulting iterable, and the result of the functionfromIterable
- the iterable to be transformedfunction
- the function to apply to the elementspublic static <T> java.lang.Iterable<T> filter(java.lang.Iterable<T> fromIterable, Predicate<? super T> predicate)
T
- the type of the elementsfromIterable
- Iterable being filteredpredicate
- the predicate to base inclusion upon, true cases are included, false excludedpublic static <E> java.lang.Iterable<E> iterable(java.util.Enumeration<E> enumeration)
E
- type of the enumerationenumeration
- the enumeration to make into an Iteratorjava.lang.NullPointerException
- if enumeration is nullpublic static <E> Optional<E> get(java.lang.Iterable<E> iterable, int position)
E
- the type of the elementsiterable
- the iterableposition
- the positionpublic static <E> Optional<E> last(java.lang.Iterable<E> iterable)
E
- type of the iterable elementsiterable
- the iterable