|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.staccatocommons.collections.iterable.Iterables
public class Iterables
Class methods that complement the Collections
functionality, providing common algorithms for collections and iterables.
With no exception, all these methods are eager, that is, processing is
completely performed on method evaluation.
Otherwise stated, null collections, functors and iterables are prohibited as
parameter, but empty collections and iterables are allowed.
Iterables class contains only side-effect-free methods that do not
modify any of its arguments, and thus can be used with immutable collections.
For algorithms that modify the state of the input collections, see
ModifiableIterables. However, Stacatto-commons-collection API
recommends to avoid those methods when possible, as will fail with
unmodifiable collections.
| Constructor Summary | |
|---|---|
Iterables()
|
|
| Method Summary | ||
|---|---|---|
static
|
all(Iterable<A> iterable,
Evaluable<? super A> predicate)
Tests if all the elements of the given Iterable satisfy the given
predicate |
|
static
|
allEqual(Iterable<A> iterable)
Answers if all elements in the collection are equal. |
|
static
|
allEquivBy(Iterable<A> iterable,
Evaluable2<? super A,? super A> equivTest)
Answers if all elements in the collection are equivalent using the given equivTest. |
|
static
|
allSame(Iterable<A> iterable)
Answers if all elements in the collection are the same object. |
|
static
|
any(Iterable<A> iterable)
Returns any element from this iterable, or throws a NoSuchElementException it iterable is empty. |
|
static
|
any(Iterable<A> iterable,
Evaluable<? super A> predicate)
Tests if any of the elements of the given Iterable satisfy the
given predicate |
|
static
|
anyOrNone(Iterable<A> iterable)
Gets any element of the given collection. |
|
static
|
containsBefore(Iterable<A> iterable,
A element,
A next)
Answers if element is contained by the given iterable, and its first
occurrence is before the first occurrence of the second one, if any. |
|
static
|
containsBeforeIndex(Iterable<A> iterable,
A element,
int index)
Answers if element is contained by the given iterable, and its first
occurrence is before the given index. |
|
static
|
countOf(Iterable<A> iterable,
Evaluable<? super A> predicate)
Answers the number of element that satisfy the given predicate |
|
static
|
cross(Collection<A> collection1,
Collection<B> collection2)
Answers the Cartesian product of the two given Collections. |
|
static
|
cross(Iterable<A> iterable1,
Iterable<B> iterable2)
Answers the Cartesian product of the two given Iterables. |
|
static
|
equiv(Iterable<? extends A> iterable1,
Iterable<? extends A> iterable2)
Test that the elements of both iterables are equal, and in the same order. |
|
static
|
equivBy(Iterable<? extends A> iterable1,
Iterable<? extends A> iterable2,
Evaluable2<? super A,? super A> equivTest)
Test that the elements of of both iterables are equal, and in the same order, using the given equalityTest for determining equality
of elements. |
|
static
|
filter(Iterable<A> iterable,
Evaluable<? super A> predicate)
Selects all elements that evaluate to true. |
|
static
|
find(Iterable<A> iterable,
Evaluable<? super A> predicate)
Alternative version of findOrNone(Iterable, Evaluable), where the
element is returned if found, or a NoSuchElementException is thrown
otherwise |
|
static
|
findOrNone(Iterable<A> iterable,
Evaluable<? super A> predicate)
Looks for a object that matches the given predicate. |
|
static
|
flatMap(Iterable<A> iterable,
Applicable<? super A,? extends Iterable<B>> function)
Maps the given iterable into a list of iterables using the
given function, and flattens the result, concatenating all the
resulting iterables into a List |
|
static
|
fold(Iterable<A> iterable,
B initial,
Applicable2<? super B,? super A,? extends B> function)
Answers the result of aggregating the given initial value and
the Iterable elements using the given function
As a particular case, if iterable is empty, this method
returns the initial value. |
|
static
|
get(Iterable<A> iterable,
int n)
Gets the zero-based, n-th element of the given
Iterable, according to its iteration order. |
|
static
|
indexOf(Iterable<A> iterable,
A element)
The zero-based index of a given element in the iterable when iterating over it |
|
static
|
isBefore(Iterable<A> iterable,
A previous,
A next)
Whether an element is before another when iterating through the given iterable. |
|
static
|
isEmpty(Iterable<A> iterable)
Answers if the given Iterable is empty or not, that is, if its
iterator returns at least one element. |
|
static
|
isNullOrEmpty(Collection<A> collection)
Answers if the given Collection is empty or null |
|
static
|
isNullOrEmpty(Iterable<A> iterable)
Answers if the given Iterable is empty or null |
|
static
|
map(Collection<A> collection,
Applicable<? super A,? extends B> function)
Maps the given Collection into a new List, using the given
function
The resulting list contains contains the result of applying the given
function to each element retrieved from the original
iterable |
|
static
|
map(Iterable<A> iterable,
Applicable<? super A,? extends B> function)
Maps the given Iterable into a new list, using the given
function. |
|
static
|
partition(Iterable<A> iterable,
Evaluable<? super A> predicate)
Splits the given iterable by returning two Lists, the first one
containing those elements that satisfy the given predicate, and the second
one containing those elements that do not satisfy it. |
|
static
|
product(Iterable<A> iterable,
NumberType<A> type)
Answers the product of the numeric elements of the given Iterable,
using the given NumberType to implement the multiplication. |
|
static
|
reduce(Iterable<A> iterable,
Applicable2<? super A,? super A,? extends A> function)
Answers the result of aggregating the given iterable elements
using the given function. |
|
static
|
reduce(Iterable<A> iterable,
Reduction<? super A,B> reduction)
Answers the result of aggregating the given iterable using the
given reduction |
|
static
|
single(Collection<A> collection)
Returns the single element of the given collection. |
|
static int |
size(Iterable<?> iterable)
Answers the size of the given iterable, that is, the number of
elements it retrieves |
|
static
|
sum(Iterable<A> iterable,
NumberType<A> type)
Answers the sum of the numeric elements of the given Iterable,
using the given NumberType to implement the addition. |
|
static
|
take(Iterable<A> iterable,
int amountOfElements)
Selects at most the fist N elements from the iterable, according to its iteration order. |
|
static
|
toList(Collection<A> collection)
Converts the given collection into a List. |
|
static
|
toList(Iterable<A> iterable)
Converts the given iterable into a List. |
|
static
|
toSet(A... elements)
Converts the given elements into a Set. |
|
static
|
toSet(Collection<A> collection)
Converts the given collection into a Set. |
|
static
|
toSet(Iterable<A> iterable)
Converts the given Iterable into a Set. |
|
static
|
toSortedList(Iterable<A> iterable,
Comparator<? super A> comparator)
Sorts a the given iterable into a new list, using the given
comparator. |
|
static
|
toSortedSet(Iterable<A> iterable)
Sorts a given iterable using its natural ordering |
|
static
|
toSortedSet(Iterable<A> iterable,
Comparator<? super A> comparator)
Sorts a given iterable using the given Comparator into
a new SortedSet |
|
static
|
zip(Iterable<A> iterable1,
Iterable<B> iterable2)
Returns a List formed by Tuple2 of elements from the two iterables. |
|
static
|
zip(Iterable<A> iterable1,
Iterable<B> iterable2,
Applicable2<A,B,C> function)
Returns a List formed by the result of applying the given
function to each Tuple2 of elements from the two iterables
given. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Iterables()
| Method Detail |
|---|
@NonNull
public static <A> List<A> filter(@NonNull
Iterable<A> iterable,
@NonNull
Evaluable<? super A> predicate)
A - iterable - predicate -
@NonNull
public static <A> List<A> take(@NonNull
Iterable<A> iterable,
@NotNegative
int amountOfElements)
A - iterable - amountOfElements -
@NonNull
public static <A> A reduce(@NonNull
Iterable<A> iterable,
@NonNull
Applicable2<? super A,? super A,? extends A> function)
iterable elements
using the given function.
A - the Iterable's elements typeiterable - function - the aggregation Applicable
iterable's element
NoSuchElementException - if the given Iterable is empty.
@NonNull
public static <A,B> B fold(@NonNull
Iterable<A> iterable,
B initial,
@NonNull
Applicable2<? super B,? super A,? extends B> function)
initial value and
the Iterable elements using the given function
As a particular case, if iterable is empty, this method
returns the initial value.
A - the Iterable's elements typeB - the aggregated value typeiterable - initial - function - the aggregation Applicable
iterable's elements.
@NonNull
public static <A,B> B reduce(@NonNull
Iterable<A> iterable,
@NonNull
Reduction<? super A,B> reduction)
iterable using the
given reduction
A - the Iterable's elements typeB - the aggregated value typeiterable - the iterable to aggregatefunction - the Reduction to apply to this iterable
iterable's elements.
public static <A> A find(@NonNull
Iterable<A> iterable,
@NonNull
Evaluable<? super A> predicate)
findOrNone(Iterable, Evaluable), where the
element is returned if found, or a NoSuchElementException is thrown
otherwise
A - iterable - predicate -
NoSuchElementException - if no element matches the predicate
@NonNull
public static <A> Option<A> findOrNone(@NonNull
Iterable<A> iterable,
@NonNull
Evaluable<? super A> predicate)
Option.none(). Otherwise
returns Option.some(Object), for the first object found
A - iterable - non nullpredicate - non null
public static <A> A single(@Size(value=1)
Collection<A> collection)
A - the collection typecollection - a single element (size==1) collection
public static <A> A any(@NonNull
Iterable<A> iterable)
NoSuchElementException it iterable is empty. Notice that
any does not mean random, it may return always the same
element - for example the first for a list -, but the exact element
returned from the iterable unspecified.
A - iterable -
NoSuchElementException - if the iterable is empty
@NonNull
public static <A> Option<A> anyOrNone(@NonNull
Iterable<A> iterable)
A - iterable -
public static <A> boolean all(@NonNull
Iterable<A> iterable,
@NonNull
Evaluable<? super A> predicate)
Iterable satisfy the given
predicate
A - iterable - predicate - the predicate that will be used to evaluate each element
true if all the elements satisfy the given predicate,
false otherwise. As a particular case of this rule,
this method will return true for empty iterables,
regardless of the predicate.
public static <A> boolean allEquivBy(@NonNull
Iterable<A> iterable,
Evaluable2<? super A,? super A> equivTest)
equivTest.
A - iterable - May be empty.equivTest - a predicate used to determine if two elements are equivalent
trueif all element are the same object.
false otherwise. As a particular case of this rule, if
this collection is empty or has only one element, it will return
true.
public static <A> boolean allEqual(@NonNull
Iterable<A> iterable)
A - iterable - non null. May be empty.
trueif all element are equal. false
otherwise. As a particular case of this rule, if this collection is
empty or has only one element, it will return true.
public static <A> boolean allSame(@NonNull
Iterable<A> iterable)
A - iterable - non null. May be empty.
trueif all element are the same object.
false otherwise. As a particular case of this rule, if
this collection is empty or has only one element, it will return
true.
public static <A> boolean any(@NonNull
Iterable<A> iterable,
Evaluable<? super A> predicate)
Iterable satisfy the
given predicate
A - iterable - predicate - the predicate that will be used to evaluate each element
true if at least one element satisfies the given
predicate, false otherwise. As a particular case of
this rule, this method will return false for empty
iterables, regardless of the predicate.
public static <A> boolean isEmpty(@NonNull
Iterable<A> iterable)
Iterable is empty or not, that is, if its
iterator returns at least one element.
A - the iterable element typeiterable -
public static <A> boolean isNullOrEmpty(Iterable<A> iterable)
Iterable is empty or null
A - the iterable element typeiterable -
isEmpty(Iterable)public static <A> boolean isNullOrEmpty(Collection<A> collection)
Collection is empty or null
A - the collection element typecollection -
public static int size(@NonNull
Iterable<?> iterable)
iterable, that is, the number of
elements it retrieves
iterable -
Iterable
public static <A> int countOf(@NonNull
Iterable<A> iterable,
Evaluable<? super A> predicate)
predicate -
filter(predicate).size()
public static <A> boolean equiv(@NonNull
Iterable<? extends A> iterable1,
@NonNull
Iterable<? extends A> iterable2)
A - iterable1 - first iterableiterable2 - second iterable
true if iterable1 has the same number of
elements that iterable2, and each Tuple2 formed by
elements of both iterables at same position are equal.
false otherwise
public static <A> boolean equivBy(@NonNull
Iterable<? extends A> iterable1,
@NonNull
Iterable<? extends A> iterable2,
Evaluable2<? super A,? super A> equivTest)
equalityTest for determining equality
of elements.
equivTest - equalityTest -
true if iterable1 has the same number of
elements that iterable2, and each Tuple2 formed by
elements of both iterables at same position are equivalent using
the given eqivTest. false otherwise
@NonNull
public static <A,B> List<B> map(@NonNull
Collection<A> collection,
@NonNull
Applicable<? super A,? extends B> function)
Collection into a new List, using the given
function
The resulting list contains contains the result of applying the given
function to each element retrieved from the original
iterable
A - the element type of the given collectionB - the element type of the resulting listcollection - the source of the mapping.function - an Applicable applied to each element of the source
collection.
List. As a particular case, this method
will return an empty list if the given collection is empty,
regardless of the given Applicable
@NonNull
public static <A,B> List<B> map(@NonNull
Iterable<A> iterable,
@NonNull
Applicable<? super A,? extends B> function)
Iterable into a new list, using the given
function.
The returned list contains contains the result of applying the given
function to each element retrieved from the original
iterable
A - the element type of the given iterablesB - the element type of the resulting listiterable - the source of the mapping.function - the function applied to each element of the source iterable.
List. As a particular case, this method
will return an empty list if the given iterable is empty
@NonNull
public static <A,B> List<B> flatMap(@NonNull
Iterable<A> iterable,
@NonNull
Applicable<? super A,? extends Iterable<B>> function)
iterable into a list of iterables using the
given function, and flattens the result, concatenating all the
resulting iterables into a List
A - B - iterable - function -
List
@NonNull
public static <A> List<A> toSortedList(@NonNull
Iterable<A> iterable,
@NonNull
Comparator<? super A> comparator)
iterable into a new list, using the given
comparator.
A - iterable - the the collection.comparator -
Iterable was empty.
@NonNull
public static <A> SortedSet<A> toSortedSet(@NonNull
Iterable<A> iterable,
@NonNull
Comparator<? super A> comparator)
iterable using the given Comparator into
a new SortedSet
A - iterable - the Iterable to sortcomparator -
comparator
@NonNull
public static <A extends Comparable<A>> SortedSet<A> toSortedSet(@NonNull
Iterable<A> iterable)
iterable using its natural ordering
A - iterable - the Iterable to sort
comparatorpublic static <A> Set<A> toSet(A... elements)
Set.
A - elements -
Set that contains the elements given without
repetitionspublic static <A> Set<A> toSet(Collection<A> collection)
Set. If the collection is
already a set, it just returns it
A - collection -
Set that contains all the elements from the given
collection, or the given collection, if it is already a set
@NonNull
public static <A> Set<A> toSet(@NonNull
Iterable<A> iterable)
Iterable into a Set. If the
iterable is already a set, it just returns it
A - iterable -
Set that contains all the elements from the given
iterable, or the given iterable, if it is
already a set
public static <A> List<A> toList(@NonNull
Collection<A> collection)
List. If the collection is
already a list, it just returns it
A - collection -
List that contains all the elements from the given
collection, or the given collection, if it is already a listpublic static <A> List<A> toList(Iterable<A> iterable)
List. If the iterable is already
a list, it just returns it
A - iterable -
List that contains all the elements from the given
iterable, or the given collection, if it is already a list
public static <A> Tuple2<List<A>,List<A>> partition(@NonNull
Iterable<A> iterable,
Evaluable<? super A> predicate)
Lists, the first one
containing those elements that satisfy the given predicate, and the second
one containing those elements that do not satisfy it.
For example, the following code:
Iterables.partition(Arrays.asList(4, 8, 5, 20, 1), Predicate.greaterThan(5));will return a Tuple2 equal to:
_(Arrays.asList(8, 20), Arrays.asList(4, 5, 1))
A - iterable - predicate - the Evaluable used to determine if an elements goes into
the first or second list
public static <A> A get(@NonNull
Iterable<A> iterable,
int n)
throws IndexOutOfBoundsException
n-th element of the given
Iterable, according to its iteration order.
A - iterable - n -
IndexOutOfBoundsException - if n is greater or equal than the size of the
iterable
public static <A> int indexOf(@NonNull
Iterable<A> iterable,
A element)
A - iterable - element -
public static <A> boolean isBefore(@NonNull
Iterable<A> iterable,
A previous,
A next)
A - iterable - previous - the element to test if exist in the iterable and is be before nextnext - the element to test if exists in the iterable and is after
previous
public static <A> boolean containsBefore(@NonNull
Iterable<A> iterable,
A element,
A next)
element is contained by the given iterable, and its first
occurrence is before the first occurrence of the second one, if any.
This message is similar to #isBefore(Object, Object), but may be
true even if the reference is not present at the iterable. It will be
always true if both element and reference are equal, too.
Examples:
List
iterable - element - next - element - the element to test whether it is contained by the given iterable, and
appears before referencereference -
Iterable, and the
first is before the second one.
public static <A> boolean containsBeforeIndex(@NonNull
Iterable<A> iterable,
A element,
@NotNegative
int index)
element is contained by the given iterable, and its first
occurrence is before the given index.
As a particular case, if index is 0, return always
false
iterable - element - index -
@NonNull
public static <A,B,C> List<C> zip(@NonNull
Iterable<A> iterable1,
@NonNull
Iterable<B> iterable2,
Applicable2<A,B,C> function)
List formed by the result of applying the given
function to each Tuple2 of elements from the two iterables
given. If any if the Iterables is shorter than the other one, the
remaining elements are discarded.
A - first iterable element typeB - second iterable element typeC - the resulting list elementiterable1 - iterable2 - function - the function to apply to each Tuple2
Applicable2 to each
Tuple2 of element retrieved from the given iterables. The resulting
list size is the minimum of both iterables sizes. As a particular
case, if any of both iterables is empty, returns an empty list,
regardless of the given functionzip(Iterable, Iterable)
@NonNull
public static <A,B> List<Tuple2<A,B>> zip(@NonNull
Iterable<A> iterable1,
@NonNull
Iterable<B> iterable2)
List formed by Tuple2 of elements from the two iterables.
If any if the Iterables is shorter than the other one, the
remaining elements are discarded.
For example, the following code:
Iterables.zip(Arrays.asList(10, 12, 14, 23), Arrays.asList(8, 7, 6))will return a list equal to:
Arrays.asList(_(10, 8), _(12, 7), _(14, 6))
A - first iterable element typeB - second iterable element typeiterable1 - iterable2 -
zip(Iterable, Iterable, Applicable2)
@NonNull
public static <A> A sum(@NonNull
Iterable<A> iterable,
@NonNull
NumberType<A> type)
Iterable,
using the given NumberType to implement the addition. If the given
iterable is empty, it returns 0.
For example, the following code:
import static net.sf.staccatocommons.lang.number.NumberTypes.*; ... Iterables.sum(Arrays.asList(10, 60, 21), integer());will produce the result
(Integer) 91
A - iterable - type -
fold(iterable, type.zero(), type.add())
@NonNull
public static <A> A product(@NonNull
Iterable<A> iterable,
@NonNull
NumberType<A> type)
Iterable,
using the given NumberType to implement the multiplication. If the
given iterable is empty, it returns 1.
For example, the following code:
import static net.sf.staccatocommons.lang.number.NumberTypes.*; ... Iterables.product(Arrays.asList(2L, 4L, 8L, 3L), long_());will produce the result
(Long) 192L
A - iterable - type -
fold(iterable, type.one(), type.multiply())
@NonNull
public static final <A,B> List<Tuple2<A,B>> cross(@NonNull
Iterable<A> iterable1,
@NonNull
Iterable<B> iterable2)
Iterables.
For example, the following code:
Iterables.cross((Iterable<Integer>) Arrays.asList(1, 2), Arrays.asList('a', 'b'));
Will produce a list equal to
Arrays.asList(_(1, 'a'), _(1, 'b'), _(2, 'a'), _(2, 'b'))
A - B - iterable1 - iterable2 -
@NonNull
public static final <A,B> List<Tuple2<A,B>> cross(@NonNull
Collection<A> collection1,
@NonNull
Collection<B> collection2)
Collections.
For example, the following code:
Iterables.cross(Arrays.asList(1, 2), Arrays.asList('a', 'b'));
Will produce a list equal to
Arrays.asList(_(1, 'a'), _(1, 'b'), _(2, 'a'), _(2, 'b'))
A - B - collection1 - collection2 -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||