Class Iterables
java.lang.Object
io.atlassian.fugue.Iterables
Contains static utility methods that operate on or return objects of type
{code}Iterable{code}.
The iterables produced from the functions in this class are safe to reuse
multiple times. Iterables#iterator returns a new iterator each time.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classCollectingIterable, filters and transforms in one.(package private) static final class(package private) static final classIterable that only shows a small range of the original Iterable.(package private) static final class(package private) static final class(package private) static class(package private) static final classInfinite iterable that repeatedly appliesftostart(package private) static final class(package private) static classClass supports the implementation ofmemoize(Iterable)and is not intended for general use.(package private) static final class(package private) static final classMemoizing iterable, maintains a lazily computed linked list of nodes.(package private) static final classMerges multiple sorted Iterables into one, sorted iterable.(package private) static final classIterable that only shows a small range of the original Iterable.(package private) static final classIterable that repeatedly appliesfuntil it returnsnone()(package private) static classIterable that combines two iterables using a combiner function. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> booleanaddAll(Collection<A> collectionToModify, Iterable<? extends A> elementsToAdd) Add all the elements of the iterable to the collectionstatic <A> booleanCheck if all elements in the input iterable match the input predicatestatic <A> booleanCheck if the iterable contains any elements that match the predicate.static <A,B> Iterable <B> Performs function application within an iterable (applicative functor pattern).static <A,B> Iterable <B> Filters and maps (aka transforms) the unfiltered iterable.static <T,A, R> R static <A> Iterable<A> Concatenate a series of iterables into a single iterable.static <A> intReturn the size of the elements inside an iterable that meets a specified predicate.static <A> Iterable<A> cycle(A... as) Return an infinite iterable that cycles through the input values in order in a loop.static <A> Iterable<A> Return an infinite iterable that cycles through the input values in order in a loop.static <A> Iterable<A> Drop the firstnasand return the rest.static <A> Iterable<A> Drop elements ofasuntil an element returns false for p#teststatic <T> Iterable<T> Returns an empty iterable, that is, anIterablewith anIteratorfor whichhasNext()always returnsfalse, and the other methods throw appropriate exceptions if called.static <A> Iterable<A> Remove elements from the input iterable for which the predicate returns falsestatic <T> Option<T> Finds the first item that matches the predicate.Partial application of the predicate argument tofindFirst(Iterable, Predicate)returning a function that takes anIterableas its argumentstatic <A> Option<A> Ifasis empty, returnsnone().static <A,B> Iterable <B> Appliesfto each element ofcollection, then concatenates the result.static <T,R> R foldLeft(Iterable<T> elements, BiFunction<? super R, ? super T, ? extends R> foldFunction, R initial) Folds theIterableelements into the value of potentially another type.static <A> Iterable<A> intersperse(Iterable<? extends A> as, A a) Intersperse an element between all the elements in an iterable.static <A> Iterable<A> intersperse(Iterable<? extends A> as, Supplier<A> a) Intersperse an element between all the elements in an iterable.isEmpty()Predicate that checks if an iterable is empty.static <A> Iterable<A> iterable(A... as) Creates an Iterable from the underlying array of elements.static <A> Iterable<A> Returns an infinite Iterable constructed by applying the given iteration function starting at the given value.static <A> Iterable<A> Join Iterable<Iterable<A>> down to Iterable<A>.static <A> StringmakeString(Iterable<? extends A> as, String start, String sep, String end) Pretty print an Iterable.static <A> StringmakeString(Iterable<? extends A> as, String start, String sep, String end, int maxLength) Pretty print an Iterable.static <A,B> Iterable <B> Apply the input function to each of the elements of the input iterable returning a new iterablestatic <A> Iterable<A> Makes a lazy copy ofxs.static <A extends Comparable<A>>
Iterable<A> mergeSorted(Iterable<? extends Iterable<A>> xss) Merge a number of already sorted collections of elements into a single collection of elements, using the elements natural ordering.static <A> Iterable<A> mergeSorted(Iterable<? extends Iterable<A>> xss, Comparator<A> ordering) Merge a number of already sorted collections of elements into a single collection of elements.Filter anIterableinto aPairofIterable's.rangeTo(int start, int end) Creates a sequence ofintegersfrom start up to and including end.rangeTo(int start, int end, int step) Creates a sequence ofintegersfrom start up to and including end with the the supplied step between them.rangeUntil(int start, int end) Creates a sequence ofintegersfrom start up to but not including end.rangeUntil(int start, int end, int step) Creates a sequence ofintegersfrom start up to but not including end with the the supplied step between them.static <A,B> Iterable <B> Applies each function infstoarg.static <A> intReturn the size of an iterable.static <A> Iterable<A> Aakes the firstnasand returns them.static <A> Iterable<A> Return a new iterable containing only the first elements ofasfor whichp#testreturns true.static <A,B> Iterable <B> Deprecated.function provided to make migration easier prefer to use #map where possiblestatic <A,B> Iterable <A> Builds an Iterable from a seed value untilfreturnsnone().Zips two iterables into a single iterable that producespairs.static <A,B, C> BiFunction <Iterable<A>, Iterable<B>, Iterable<C>> zipWith(BiFunction<A, B, C> f) Takes a two-arg function that returns a third type and reurn a new function that takes iterables of the two input types and combines them into a new iterable.zipWithIndex(Iterable<A> as) Takes an Iterable, and returns an Iterable of a Pair of the original element and its index starting at zero.
-
Field Details
-
EMPTY
-
-
Constructor Details
-
Iterables
private Iterables()
-
-
Method Details
-
emptyIterable
Returns an empty iterable, that is, anIterablewith anIteratorfor whichhasNext()always returnsfalse, and the other methods throw appropriate exceptions if called. Intended to be used as a more idiomatic replacement forCollections.emptyList()in code that otherwise deals only with iterables.- Type Parameters:
T- the type- Returns:
- an empty iterable
- Since:
- 1.2
-
iterable
Creates an Iterable from the underlying array of elements.- Type Parameters:
A- The type of the elements- Parameters:
as- the elements to iterate over- Returns:
- an Iterable across the underlying elements
- Since:
- 2.3
-
findFirst
Finds the first item that matches the predicate. Traditionally, this should be named find; in this case it is named findFirst to avoid clashing with static imports from Guava's com.google.common.collect.Iterables.- Type Parameters:
T- the type- Parameters:
elements- the iterable to search for a matching elementp- the predicate to use to determine if an element is eligible to be returned- Returns:
- the first item in elements that matches predicate
- Since:
- 1.0
-
findFirst
Partial application of the predicate argument tofindFirst(Iterable, Predicate)returning a function that takes anIterableas its argument- Type Parameters:
A- the type- Parameters:
predicate- the predicate to use to determine if an element is eligible to be returned- Returns:
- a Function that takes an
Iterableas its argument, and returns the first element that satisfies the predicate - Since:
- 2.2
-
first
Ifasis empty, returnsnone(). Otherwise, returnssome(get(as, 0)).- Type Parameters:
A- type of elements inas- Parameters:
as- elements to get the first value of, must not be null- Returns:
none()ifasis empty.some(get(as, 0))otherwise- Since:
- 1.1
-
ap
Performs function application within an iterable (applicative functor pattern).- Parameters:
as- an iterablefs- The iterable of functions to apply.- Returns:
- A new iterable after applying the given stream of functions through as.
-
flatMap
public static <A,B> Iterable<B> flatMap(Iterable<A> collection, Function<? super A, ? extends Iterable<? extends B>> f) Appliesfto each element ofcollection, then concatenates the result.- Type Parameters:
A- type of elements incollectionB- type elements in the newIterablefwill transform elements to- Parameters:
collection- elements to applyftof-Functionto apply to elements ofcollection- Returns:
- concatenated result of applying
fto each element ofcollection - Since:
- 1.1
-
revMap
Applies each function infstoarg.- Type Parameters:
A- the argument typeB- the function output and type of the elements of the final iterable.- Parameters:
fs- an iterable of functions that the arg will be applied toarg- the argument to apply to the functions- Returns:
- the results of the functions when applied to the arg
- Since:
- 1.1
-
isEmpty
-
collect
public static <A,B> Iterable<B> collect(Iterable<? extends A> from, Function<? super A, Option<B>> partial) Filters and maps (aka transforms) the unfiltered iterable. Applies the given partial function to each element of the unfiltered iterable. If the application returns none, the element will be left out; otherwise, the transformed object contained in the Option will be added to the result.- Type Parameters:
A- the input typeB- the output type- Parameters:
from- the input iterable, must not be null and must not contain nullpartial- the collecting function- Returns:
- the collected iterable
- Since:
- 2.2
-
collect
- Type Parameters:
T- the type of elements in the originalIterableA- accumulator typeR- return type- Parameters:
elements- the iterable to run the Collector on. Can not be null.collector- theCollector. Can not be null.- Returns:
- the result of the reduction, using the given
Collector - Since:
- 3.1
- See Also:
-
foldLeft
public static <T,R> R foldLeft(Iterable<T> elements, BiFunction<? super R, ? super T, ? extends R> foldFunction, R initial) Folds theIterableelements into the value of potentially another type. Starting the computation with the specified initial value.- Type Parameters:
T- the type of elements in the originalIterableR- return type- Parameters:
elements- the iterable to run the foldLeft operation on. Can not be null.foldFunction- theBiFunctionused for reducing. Can not be null.initial- the initial value the folding is started with- Returns:
- the result of the reduction, using the given
BiFunction - Since:
- 6.1
-
partition
public static <A> Pair<Iterable<A>, Iterable<A>> partition(Iterable<A> iterable, Predicate<? super A> p) Filter anIterableinto aPairofIterable's.- Type Parameters:
A- the type- Parameters:
iterable- to be filteredp- to filter each element- Returns:
- a pair where the left matches the predicate, and the right does not.
- Since:
- 1.2
-
take
-
drop
Drop the firstnasand return the rest.- Type Parameters:
A- type ofas- Parameters:
n- number ofasto drop, must greater than or equal to zeroas- list of values, must not be null and must not contain null- Returns:
- remaining
asafter dropping the firstn - Since:
- 1.1
-
dropWhile
Drop elements ofasuntil an element returns false for p#test- Type Parameters:
A- type ofas- Parameters:
as- iterable to remove the first elements ofas, must not be nullp- predicate used to test which elements to drop from the iterable, must not be null- Returns:
- remaining elements of
asafter removing the starting elements that for whichp#testreturns true - Since:
- 3.0
- See Also:
-
takeWhile
Return a new iterable containing only the first elements ofasfor whichp#testreturns true.- Type Parameters:
A- type ofas- Parameters:
as- iterable to source the first elements ofas, must not be nullp- predicate used to test which elements to include in the new iterable, must not be null- Returns:
- a new iterable containing elements of
asstarting from the first element ofasuntilp#testreturns false - Since:
- 3.0
- See Also:
-
zip
-
zipWith
public static <A,B, BiFunction<Iterable<A>, Iterable<B>, Iterable<C>> zipWithC> (BiFunction<A, B, C> f) Takes a two-arg function that returns a third type and reurn a new function that takes iterables of the two input types and combines them into a new iterable.- Type Parameters:
A- LHS typeB- RHS typeC- result type- Parameters:
f- combiner function, must not be null- Returns:
- an Function that takes two iterables and zips them using the supplied function. The input iterables must not be null and must not contain null
- Since:
- 1.2
-
zipWithIndex
Takes an Iterable, and returns an Iterable of a Pair of the original element and its index starting at zero.- Type Parameters:
A- the type- Parameters:
as- the original iterable- Returns:
- the decorated iterable that generates pairs.
- Since:
- 1.2
-
unzip
-
rangeUntil
-
rangeUntil
Creates a sequence ofintegersfrom start up to but not including end with the the supplied step between them.- Parameters:
start- from (inclusive)end- to (exclusive)step- size to step – must not be zero, must be positive if end is greater than start, neagtive otherwise- Returns:
- a sequence of
integers - Since:
- 1.2
-
rangeTo
-
rangeTo
Creates a sequence ofintegersfrom start up to and including end with the the supplied step between them.- Parameters:
start- from (inclusive), must be greater than zero and less than endend- to (inclusive)step- size to step – must not be zero, must be positive if end is greater than start, negative otherwise- Returns:
- a sequence of
integers - Since:
- 1.2
-
intersperse
Intersperse an element between all the elements in an iterable.- Type Parameters:
A- the type of the elements.- Parameters:
as- the source iterable.a- the element to intersperse between the source elements.- Returns:
- a new Iterable that intersperses the element between the source.
- Since:
- 2.3
-
intersperse
Intersperse an element between all the elements in an iterable.- Type Parameters:
A- the type of the elements.- Parameters:
as- the source iterable.a- the supplier of elements to intersperse between the source elements.- Returns:
- a new Iterable that intersperses the element between the source.
- Since:
- 2.3
-
size
Return the size of an iterable. In most cases this function is required to walk the entire iterable to determine the result. Consider this an O(n) complexity function.- Type Parameters:
A- element type- Parameters:
as- iterable to compute the size of- Returns:
- number of elements in the iterable
- Since:
- 3.0
-
count
Return the size of the elements inside an iterable that meets a specified predicate.- Type Parameters:
A- element type- Parameters:
as- the source iterablep- predicate to filter by- Returns:
- number of elements in the iterable that meets a prediction
- Since:
- 6.1
-
transform
@Deprecated public static <A,B> Iterable<B> transform(Iterable<A> as, Function<? super A, ? extends B> f) Deprecated.function provided to make migration easier prefer to use #map where possibleTransform an interable by mapping a function across each of its elements- Type Parameters:
A- original iterable typeB- output iterable type- Parameters:
as- the source iterablef- function to apply to all the elements of as- Returns:
- new iterable containing the transformed values produced by f#apply
- Since:
- 3.0
-
map
Apply the input function to each of the elements of the input iterable returning a new iterable- Type Parameters:
A- original iterable typeB- output iterable type- Parameters:
as- the source iterablef- function to apply to all the elements of as- Returns:
- new iterable containing values produced by f#apply called on each element
- Since:
- 3.0
-
filter
Remove elements from the input iterable for which the predicate returns false- Type Parameters:
A- element type- Parameters:
as- original iterablep- predicate to filter by- Returns:
- new iterable containing only those elements for which p#test returns true
- Since:
- 3.0
-
join
Join Iterable<Iterable<A>> down to Iterable<A>. The resulting iterable will exhaust the first input iterable in order before returning values from the second. Input iterables must not be null and must not contain null.- Type Parameters:
A- element type- Parameters:
ias- one or more iterable to merge into the final iterable result, must not be null and must not return null- Returns:
- single level iterable with all the elements of the original iterables
- Since:
- 3.0
-
concat
Concatenate a series of iterables into a single iterable. Returns an empty iterable if no iterables are supplied. Input iterables must not be null and must not contain null.- Type Parameters:
A- super type of contained by all input iterables- Parameters:
as- any number of iterables containing A- Returns:
- new iterable containing all the elements of the input iterables
- Since:
- 3.0
-
any
Check if the iterable contains any elements that match the predicate.- Type Parameters:
A- type of elements inside the input iterable- Parameters:
as- iterable to compare for matching elementsp- predicate to test for matching elements- Returns:
- true if any element in the iterable returns true for the input predicate otherwise false. False for an empty iterable.
- Since:
- 3.0
-
all
Check if all elements in the input iterable match the input predicate- Type Parameters:
A- type of elements inside the input iterable- Parameters:
as- iterable to compare for matching elementsp- predicate to test for matching elements- Returns:
- true if all elements in the iterable return true for the input predicate otherwise false. True for an empty iterable.
- Since:
- 3.0
-
iterate
Returns an infinite Iterable constructed by applying the given iteration function starting at the given value.- Type Parameters:
A- type of the elements- Parameters:
f- The iteration function, must not return nullstart- The value to begin iterating from.- Returns:
- An infinite Iterable of repeated applications of
ftostart. - Since:
- 2.4
-
unfold
Builds an Iterable from a seed value untilfreturnsnone().- Type Parameters:
A- type of the returned elements.B- type of the elements for whichfis applied.- Parameters:
f- The function that returns some(pair(a, b)), in which caseais the next element of the resulting Iterable andbis used as the input value for the next call off, ornone()if it is done producing the elements. f must not be null and must not return a pair containing null.seed- The start value to begin the unfold.- Returns:
- An Iterable that is a result of unfolding.
- Since:
- 2.4
-
mergeSorted
public static <A extends Comparable<A>> Iterable<A> mergeSorted(Iterable<? extends Iterable<A>> xss) Merge a number of already sorted collections of elements into a single collection of elements, using the elements natural ordering.- Type Parameters:
A- collection type- Parameters:
xss- collection of already sorted collections, must not be null and must not return null- Returns:
xssmerged in a sorted order- Since:
- 1.1
-
mergeSorted
public static <A> Iterable<A> mergeSorted(Iterable<? extends Iterable<A>> xss, Comparator<A> ordering) Merge a number of already sorted collections of elements into a single collection of elements.- Type Parameters:
A- type of the elements- Parameters:
xss- already sorted collection of collections, must not be null and must not return nullordering- ordering to use when comparing elements, must not be null- Returns:
xssmerged in a sorted order- Since:
- 1.1
-
addAll
public static <A> boolean addAll(Collection<A> collectionToModify, Iterable<? extends A> elementsToAdd) Add all the elements of the iterable to the collection- Type Parameters:
A- element type- Parameters:
collectionToModify- collection to add elements toelementsToAdd- source of addtional elements- Returns:
- true if the collectionToModify was changed
- Since:
- 3.0
-
cycle
Return an infinite iterable that cycles through the input values in order in a loop. If no elements are provided returns an empty iterable. Does not support element removal via Iterator#remove.- Type Parameters:
A- returned elements- Parameters:
as- input values to cycle through- Returns:
- an infinite iterable containing the original elements
- Since:
- 3.0
-
cycle
Return an infinite iterable that cycles through the input values in order in a loop. If no elements are provided returns an empty iterable. Does not support element removal via Iterator#remove.- Type Parameters:
A- returned elements- Parameters:
as- input values to cycle through must not be null- Returns:
- an infinite iterable containing the original elements
- Since:
- 3.0
-
makeString
public static <A> String makeString(Iterable<? extends A> as, String start, String sep, String end, int maxLength) Pretty print an Iterable. Printing following this pattern: <start><element><sep><element><end> If the iterable would result in a String with length more than maxLength printing follows this pattern: <start><element><sep><element>...<end>- Type Parameters:
A- type of the elements in the iterable- Parameters:
as- the iterable to print must not be nullstart- prefix to start the printing withsep- separator to use between each elementend- suffic to end the printing withmaxLength- limit the length of the resulting string- Returns:
- a pretty printed copy of the input iterable
- Since:
- 3.0
-
makeString
Pretty print an Iterable. Printing following this pattern: <start><element><sep><element><end> If the iterable would result in a String with length more than 100 characters printing follows this pattern: <start><element><sep><element>...<end>- Type Parameters:
A- type of the elements in the iterable- Parameters:
as- the iterable to print must not be nullstart- prefix to start the printing withsep- separator to use between each elementend- suffic to end the printing with- Returns:
- a pretty printed copy of the input iterable
- Since:
- 3.0
- See Also:
-
memoize
-