|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectutil.Functional
public final class Functional
A few utility methods for functional-ish programming, including concise
string manipulation and composition. Along with Builders,
should be useful in cutting down on lengthy code duplication in library
classes.
| Nested Class Summary | |
|---|---|
static class |
Functional.Iteration<T>
An adaptor for iterating in a foreach loop, given an iterator. |
static class |
Functional.Range
An iterable representing a range of integers. |
static class |
Functional.Zipper<A,B>
An iterable over two iterables, pairing them element by element. |
| Method Summary | ||
|---|---|---|
static
|
backward(Iterable<T> coll)
Iterate over a given iterable backwards. |
|
static
|
backward(Iterator<T> iter)
Iterate over a given iterator backwards. |
|
static
|
backward(List<T> list)
Iterate over a given list backwards. |
|
static
|
enumerate(Iterable<T> coll)
Iterate over (index, element) pairs corresponding to each element of an iterable with its index. |
|
static
|
enumerate(T[] arr)
Iterate over (index, element) pairs corresponding to each element of an array with its index. |
|
static
|
join(Collection<T> coll,
String sep)
Concatenate the string represetations of several objects, with a separator. |
|
static
|
join(T[] arr,
String sep)
Join an array into a string, separating the elements with the given separator string. |
|
static
|
quoteAndJoin(Collection<T> coll,
String sep)
Put typographer's (Unicode) quotes around a collection of objects, delimited by some separator. |
|
static
|
quoteAndJoin(T[] coll,
String sep)
Put typographer's (Unicode) quotes around an array of objects, delimited by some separator. |
|
static Functional.Range |
range(int stop)
Construct the integer range [0, stop), as an efficient
iterable. |
|
static Functional.Range |
range(int start,
int stop)
Construct the integer range [ start, stop),
as an efficient iterable. |
|
static Functional.Range |
range(int start,
int stop,
int step)
Construct the integer range from start (inclusive) to
stop (exclusive), in increments of step,
as an efficient iterable. |
|
static
|
separate(C coll,
Predicate<T> pred)
Divide a collection into those elements satisfying and those not satisfying a given predicate, returning two arrays. |
|
static
|
separate(C coll,
Predicate<T> pred,
C1 tgtTrue,
C2 tgtFalse)
Divide a collection into those elements satisfying and those not satisfying a given predicate. |
|
static
|
separate(T[] arr,
Predicate<T> pred)
Divide an array into those elements satisfying and those not satisfying a given predicate, returning two arrays. |
|
static
|
separate(T[] arr,
Predicate<T> pred,
C1 tgtTrue,
C2 tgtFalse)
Divide an array into those elements satisfying and those not satisfying a given predicate. |
|
static
|
span(C coll,
Predicate<T> pred)
Split a collection at the first element not satisfying a predicate, returning two arrays. |
|
static
|
span(C coll,
Predicate<T> pred,
C1 tgt1,
C2 tgt2)
Split a collection at the first element not satisfying a predicate. |
|
static
|
span(T[] arr,
Predicate<T> pred)
Split an array at the first element not satisfying a predicate, returning two arrays. |
|
static
|
span(T[] arr,
Predicate<T> pred,
C1 tgt1,
C2 tgt2)
Split an array at the first element not satisfying a predicate. |
|
static
|
wrapEach(Collection<T> coll,
String prefix,
String suffix)
Add a prefix and/or suffix to the string representation of each element in a collection. |
|
static
|
wrapEach(Iterable<T> coll,
String prefix,
String suffix)
Add a prefix and/or suffix to the string representation of each element in any iterable. |
|
static
|
wrapEach(T[] arr,
String prefix,
String suffix)
Add a prefix and/or suffix to the string representation of each element in an array. |
|
static
|
zip(A[] arrA,
B[] arrB)
Iterate over two arrays at once. |
|
static
|
zip(Iterable<A> collA,
Iterable<B> collB)
Iterate over two iterables at once, pairing them element by element. |
|
static
|
zip(Iterator<A> iterA,
Iterator<B> iterB)
Iterate over two iterators at once. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <T> String join(Collection<T> coll,
String sep)
See the example under join(Object[], String).
T - The type of the objects to concatenate.coll - The objects to concatenate.sep - The separator string.
public static <T> String join(T[] arr,
String sep)
String[] things = { "eggs", "bacon", "spam" };
System.out.println(Functional.join(things, ", ");
System.out.println(Functional.join(things, " ... ");
Expected output:
eggs, bacon, spam
eggs ... bacon ... spam
T - The element type of the array.arr - The array with the elements to concatenate.sep - The string to separate the elements.
public static <T> String[] wrapEach(Collection<T> coll,
String prefix,
String suffix)
See the example under wrapEach(Object[], String, String).
T - The element type of the collection.coll - The collection.prefix - The string to put before each element.suffix - The string to put after each element.
public static <T> String[] wrapEach(Iterable<T> coll,
String prefix,
String suffix)
See the example under wrapEach(Object[], String, String).
T - The element type of the iterable.coll - The iterable.prefix - The string to put before each element.suffix - The string to put after each element.
public static <T> String[] wrapEach(T[] arr,
String prefix,
String suffix)
For example, say we want to generate Java code to count from 1 to 10.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
String[] lines = Functional.wrapEach(numbers,
"System.out.println(", ");");
for (String line : lines)
System.out.println(line);
Expected output (abbreviated):
System.out.println(1);
System.out.println(2);
...
System.out.println(10);
T - The element type of the array.arr - The array.prefix - The string to put before each element.suffix - The string to put after each element.
public static <T> String quoteAndJoin(Collection<T> coll,
String sep)
T - The element type of the collection.coll - The collection.sep - The string to separate the elements.
public static <T> String quoteAndJoin(T[] coll,
String sep)
T - The element type of the array.coll - The array.sep - The string to separate the elements.
public static <T> Iterable<T> backward(Iterator<T> iter)
Note that this is likely to be inefficient, as a list of the values given by the iterator must be built at construction and then kept for the duration of the iteration.
T - The element type of the iterator.iter - The iterator to reverse.
iter, only
backwards.public static <T> Iterable<T> backward(List<T> list)
If the list is of a type that implements RandomAccess,
this is efficient, since one need only go through the indices
backwards. Otherwise, a temporary ArrayList is required.
T - The element type of the iterator.list - The list over which to iterate.
list in reverse.public static <T> Iterable<T> backward(Iterable<T> coll)
T - The element type of the iterable.coll - The iterable to reverse.
coll would
give, only in reverse.
public static Functional.Range range(int start,
int stop,
int step)
start (inclusive) to
stop (exclusive), in increments of step,
as an efficient iterable.
start - The first integer in the range.stop - The first integer outside of the range.step - The increment between each element.
Iterable<Integer> over the range.
public static Functional.Range range(int start,
int stop)
start, stop),
as an efficient iterable.
start - The first integer in the range.stop - The first integer outside the range.
Iterable<Integer> over [start,
stop).public static Functional.Range range(int stop)
stop), as an efficient
iterable.
stop - The first integer outside the range.
Iterable<Integer> over [0, stop).
public static <A,B> Functional.Zipper<A,B> zip(Iterable<A> collA,
Iterable<B> collB)
A - The type of the first elements.B - The type of the second elements.collA - The first iterable over which to iterate.collB - The second iterable.
public static <A,B> Functional.Zipper<A,B> zip(Iterator<A> iterA,
Iterator<B> iterB)
A - The element type of the first iterator.B - The element type of the second iterator.iterA - The first iterator.iterB - The second iterator.
public static <A,B> Functional.Zipper<A,B> zip(A[] arrA,
B[] arrB)
A - The element type of the first array.B - The element type of the second array.arrA - The first array.arrB - The second array.
public static <T> Iterable<Pair<Integer,T>> enumerate(Iterable<T> coll)
T - The element type.coll - The collection to enumerate.
(index, element) for element ==
coll.get(index).public static <T> Iterable<Pair<Integer,T>> enumerate(T[] arr)
T - The element type.arr - The array to enumerate.
(index, element) for element ==
arr[index].
public static <T> Pair<T[],T[]> separate(T[] arr,
Predicate<T> pred)
For example, given the predicate isEven in the example
from Predicate:
Integer[] numbers = { 1, 2, 3, 4, 5 };
Pair<Integer> evens_odds = Functional.separate(numbers, isEven);
Integer[] evens = evens_odds.a, odds = evens_odds.b;
System.out.format("Evens: %s\nOdds: %s\n",
Functional.join(evens, ", "), Functional.join(odds, ", "));
(see join(Object[], String))
The output should be:
Evens: 2, 4
Odds: 1, 3, 5
T - The element type of the array.arr - The array to divide.pred - The predicate to apply.
public static <T,C extends Collection<T>> Pair<T[],T[]> separate(C coll,
Predicate<T> pred)
See the example under
separate(Object[], Predicate).
T - The element type of the collection.C - The collection type.coll - The collection to divide.pred - The predicate to apply.
public static <T,C1 extends Collection<T>,C2 extends Collection<T>> Pair<C1,C2> separate(T[] arr,
Predicate<T> pred,
C1 tgtTrue,
C2 tgtFalse)
For example, given the predicate isEven in the example
from Predicate:
Integer[] numbers = { 1, 2, 3, 4, 5 };
List<Integer> evens, odds;
evens = new ArrayList<Integer>();
odds = new ArrayList<Integer>();
Functional.separate(numbers, isEven, evens, odds);
System.out.format("Evens: %s\nOdds: %s\n", evens, odds);
The output should be:
Evens: [2, 4]
Odds: [1, 3, 5]
T - The element type of the array.C1 - The type of the first target collection.C2 - The type of the second target collection.arr - The array to divide.pred - The predicate to apply.tgtTrue - The collection receiving those elements satisfying the
predicate.tgtFalse - The collection receiving those elements not satisfying
the predicate.
Pair.of(tgtTrue, tgtFalse), as a convenience.
public static <T,C extends Collection<T>,C1 extends Collection<T>,C2 extends Collection<T>> Pair<C1,C2> separate(C coll,
Predicate<T> pred,
C1 tgtTrue,
C2 tgtFalse)
See the example under
separate(Object[], Predicate, Collection, Collection).
T - The element type of the collection.C - The collection type.C1 - The type of the first target collection.C2 - The type of the second target collection.coll - The collection to divide.pred - The predicate to apply.tgtTrue - The collection receiving those elements satisfying the
predicate.tgtFalse - The collection receiving those elements not satisfying
the predicate.
Pair.of(tgtTrue, tgtFalse), as a convenience.
public static <T> Pair<T[],T[]> span(T[] arr,
Predicate<T> pred)
For example, given the predicate isEven in the example
from Predicate:
Integer[] numbers = { 2, 4, 6, 7, 8, 9, 10 };
Pair<Integer[], Integer[]> evenSpan_rest =
Functional.span(numbers, isEven);
Integer[] evenSpan = evenSpan_rest.a, rest = evenSpan.rest.b;
System.out.format("Even span: %s\nRest: %s\n",
Functional.join(evenSpan, ", "), Functional.join(rest, ", "));
(see join(Object[], String))
The output should be:
Even span: 2, 4, 6
Rest: 7, 8, 9, 10
T - The element type of the array.arr - The array to split.pred - The predicate to apply.
Pair.of(tgt1, tgt2), as a convenience.
public static <T,C extends Collection<T>> Pair<T[],T[]> span(C coll,
Predicate<T> pred)
See span(Object[], Predicate, Collection, Collection) for an
example.
T - The element type of the collection.C - The collection type.coll - The collection to split.pred - The predicate to apply.
Pair.of(tgt1, tgt2), as a convenience.
public static <T,C1 extends Collection<T>,C2 extends Collection<T>> Pair<C1,C2> span(T[] arr,
Predicate<T> pred,
C1 tgt1,
C2 tgt2)
For example, given the predicate isEven in the example
from Predicate:
List<Integer> evenSpan, rest;
Integer[] numbers = { 2, 4, 6, 7, 8, 9, 10 };
evenSpan = new ArrayList<Integer>();
rest = new ArrayList<Integer>();
Functional.span(numbers, isEven, evenSpan, rest);
System.out.format("Even span: %s\nRest: %s\n", evenSpan, rest);
The output should be:
Even span: [2, 4, 6]
Rest: [7, 8, 9, 10]
T - The element type of the array.C1 - The type of the first target collection.C2 - The type of the second target collection.arr - The array to split.pred - The predicate to apply.tgt1 - The collection receiving the initial sublist of elements
satisfying the predicate.tgt2 - The collection receiving the rest.
Pair.of(tgt1, tgt2), as a convenience.
public static <T,C extends Collection<T>,C1 extends Collection<T>,C2 extends Collection<T>> Pair<C1,C2> span(C coll,
Predicate<T> pred,
C1 tgt1,
C2 tgt2)
See span(Object[], Predicate, Collection, Collection) for an
example.
T - The element type of the collection.C - The collection type.C1 - The type of the first target collection.C2 - The type of the second target collection.coll - The collection to split.pred - The predicate to apply.tgt1 - The collection receiving the initial sublist of elements
satisfying the predicate.tgt2 - The collection receiving the rest.
Pair.of(tgt1, tgt2), as a convenience.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||