Package org.apache.tapestry5.func
Class F
- java.lang.Object
-
- org.apache.tapestry5.func.F
-
public class F extends java.lang.Object
Functional operations on collections with generics support. The core interface isFlowto which operations and transformations (in terms ofPredicates,Mappers andReducers) to create new Flows. Flows are initially created usingflow(Collection)andflow(Object...). F will be used a bit, thus it has a short name (for those who don't like static imports). It provides a base set of Predicate, Mapper and Reducer factories. A good development pattern for applications is to provide a similar, application-specific, set of such factories.- Since:
- 5.2.0
-
-
Field Summary
Fields Modifier and Type Field Description static Mapper2<java.lang.Integer,java.lang.Integer,java.lang.Integer>ADD_INTSA two-input Mapper used to add the values from two Flows of Integers into a Flow of Integer sums.static Predicate<java.lang.String>IS_BLANKPredicate that returns true if the provided string is blank (null or all whitespace).static Reducer<java.lang.Integer,java.lang.Integer>SUM_INTSA Reducer that operates on a Flow of Integers and is used to sum the values.
-
Constructor Summary
Constructors Constructor Description F()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Worker<T>addToCollection(java.util.Collection<T> coll)A Worker factory; the returnedWorker adds the values to a provided collection.static <S,T>
Mapper<S,T>always(T fixedResult)A Mapper factory; the returned Mapper ignores its input value and always returns a predetermined result.static <T> Predicate<T>and(Predicate<? super T>... delegates)Combines any number of delegates as a logical and operation.static <A,B,C>
Mapper<A,C>combine(Mapper<A,B> abMapper, Mapper<B,C> bcMapper)Combines two mappers into a composite mapping from type A to type C via type B.static <T> Worker<T>combine(Worker<? super T>... delegates)Combines several compatible workers together into a composite.static Predicate<java.lang.String>endsWith(java.lang.String suffix)A Predicate factory for matching String elements with a given suffix.static Predicate<java.lang.String>endsWithIgnoringCase(java.lang.String suffix)As withendsWith(String)but ignores case.static <T extends java.lang.Comparable<T>>
Predicate<T>eq(T value)A Predicate factory for comparison of a Comparable element from a flow against a fixed value.static <T> Predicate<T>eql(T value)A Predicate factory for equality of an element from a flow against a specified value.static <T> Flow<T>flow(java.lang.Iterable<T> iterable)Creates a lazy Flow from theIteratorobtained from the iterable.static <T> Flow<T>flow(java.util.Collection<T> values)Extracts the values from the collection to form aFlow.static <T> Flow<T>flow(java.util.Iterator<T> iterator)Creates a lazy Flow from theIterator.static <T> Flow<T>flow(T... values)Creates a new Flow from the values.static <T extends java.lang.Comparable<T>>
Predicate<T>gt(T value)A Predicate factory for comparison of a Comparable against a fixed value; true if the flow element is greater than the provided value.static <T extends java.lang.Comparable<T>>
Predicate<T>gteq(T value)A Predicate factory for comparison of a Comparable against a fixed value; true if the flow element is greater than or equal to the value.static <S> Mapper<S,S>identity()A Mapper factory; the Mapper returns the the flow value unchanged.static <T> Predicate<T>isNull()A Predicate factory; returns true if the value from the Flow is null.static <T> Flow<T>iterate(T initial, Mapper<T,T> function)Creates an infinite lazy flow from an initial value and a function to map from the current value to the next value.static <T> Flow<T>lazy(LazyFunction<T> function)Creates aFlowfrom a lazy function.static <T extends java.lang.Comparable<T>>
Predicate<T>lt(T value)A Predicate factory for comparison of a Comparable against a fixed value; true if the element is less than the value.static <T extends java.lang.Comparable<T>>
Predicate<T>lteq(T value)A Predicate factory for comparison of a Comprable element against a fixed value; true if the element is less than or equal to the value.static <T extends java.lang.Comparable<T>>
Predicate<T>neq(T value)A Predicate factory for comparison of a Comparable element against a fixed value.static <T> Predicate<T>not(Predicate<? super T> delegate)Inverts a predicate.static <T> Predicate<T>notNull()A Predicate factory; returns true if the value from the Flow is not null.static <T> Predicate<T>or(Predicate<? super T>... delegates)Combines any number of delegates as a logical or operation.static <A extends java.lang.Comparable<A>,B>
java.util.Comparator<Tuple<A,B>>orderByFirst()Creates a Comparator for the Tuples of aZippedFlowthat sorts the Tuple elements based on the first value in the Tuple.static <A,B extends java.lang.Comparable<B>>
java.util.Comparator<Tuple<A,B>>orderBySecond()Creates a Comparator for the Tuples of aZippedFlowthat sorts the Tuple elements based on the first value in the Tuple.static Flow<java.lang.Integer>range(int lower, int upper)Creates a lazy Flow that returns integers in the given range.static <S,T>
Mapper<S,T>select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted)Override ofselect(Predicate, Mapper, Mapper)where rejected values are replaced with null.static <S,T>
Mapper<S,T>select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted, Mapper<S,T> ifRejected)A Mapper factory that combines a Predicate with twoMappers; evaluating the predicate selects one of the two mappers.static <S,T>
Mapper<S,T>select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted, T ifRejectedValue)Override ofselect(Predicate, Mapper)where rejected values are replaced with a fixed value.static Flow<java.lang.Integer>series(int start, int delta)Creates an infinite series of numbers.static Predicate<java.lang.String>startsWith(java.lang.String prefix)A Predicate factory for matching String elements with a given prefix.static Predicate<java.lang.String>startsWithIgnoringCase(java.lang.String prefix)AsstartsWith(String), but ignores case.static <T> Mapper<T,java.lang.String>stringValueOf()A Mapper factory that gets the string value of the flow value usingString.valueOf(Object).static <S> Predicate<S>toPredicate(Mapper<S,java.lang.Boolean> mapper)Allows a Mapper that maps to boolean to be used as a Predicate.static <A,B>
ZippedFlow<A,B>zippedFlow(java.util.Map<A,B> map)Creates a ZippedFlow from the provided map; the order of the tuples in the ZippedFlow is defined by the iteration order of the map entries.
-
-
-
Field Detail
-
IS_BLANK
public static Predicate<java.lang.String> IS_BLANK
Predicate that returns true if the provided string is blank (null or all whitespace).
-
SUM_INTS
public static Reducer<java.lang.Integer,java.lang.Integer> SUM_INTS
A Reducer that operates on a Flow of Integers and is used to sum the values.
-
-
Constructor Detail
-
F
public F()
-
-
Method Detail
-
eql
public static <T> Predicate<T> eql(T value)
A Predicate factory for equality of an element from a flow against a specified value.
-
eq
public static <T extends java.lang.Comparable<T>> Predicate<T> eq(T value)
A Predicate factory for comparison of a Comparable element from a flow against a fixed value.
-
neq
public static <T extends java.lang.Comparable<T>> Predicate<T> neq(T value)
A Predicate factory for comparison of a Comparable element against a fixed value.
-
gt
public static <T extends java.lang.Comparable<T>> Predicate<T> gt(T value)
A Predicate factory for comparison of a Comparable against a fixed value; true if the flow element is greater than the provided value.
-
gteq
public static <T extends java.lang.Comparable<T>> Predicate<T> gteq(T value)
A Predicate factory for comparison of a Comparable against a fixed value; true if the flow element is greater than or equal to the value.
-
lt
public static <T extends java.lang.Comparable<T>> Predicate<T> lt(T value)
A Predicate factory for comparison of a Comparable against a fixed value; true if the element is less than the value.
-
lteq
public static <T extends java.lang.Comparable<T>> Predicate<T> lteq(T value)
A Predicate factory for comparison of a Comprable element against a fixed value; true if the element is less than or equal to the value.
-
isNull
public static <T> Predicate<T> isNull()
A Predicate factory; returns true if the value from the Flow is null.
-
notNull
public static <T> Predicate<T> notNull()
A Predicate factory; returns true if the value from the Flow is not null.
-
stringValueOf
public static <T> Mapper<T,java.lang.String> stringValueOf()
A Mapper factory that gets the string value of the flow value usingString.valueOf(Object).
-
always
public static <S,T> Mapper<S,T> always(T fixedResult)
A Mapper factory; the returned Mapper ignores its input value and always returns a predetermined result.
-
select
public static <S,T> Mapper<S,T> select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted, Mapper<S,T> ifRejected)
A Mapper factory that combines a Predicate with twoMappers; evaluating the predicate selects one of the two mappers.- Parameters:
predicate- evaluated to selected a coercionifAccepted- used when predicate evaluates to trueifRejected- used when predicate evaluates to false
-
select
public static <S,T> Mapper<S,T> select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted)
Override ofselect(Predicate, Mapper, Mapper)where rejected values are replaced with null.
-
select
public static <S,T> Mapper<S,T> select(Predicate<? super S> predicate, Mapper<S,T> ifAccepted, T ifRejectedValue)
Override ofselect(Predicate, Mapper)where rejected values are replaced with a fixed value.
-
identity
public static <S> Mapper<S,S> identity()
A Mapper factory; the Mapper returns the the flow value unchanged.
-
toPredicate
public static <S> Predicate<S> toPredicate(Mapper<S,java.lang.Boolean> mapper)
Allows a Mapper that maps to boolean to be used as a Predicate.
-
flow
public static <T> Flow<T> flow(java.util.Collection<T> values)
Extracts the values from the collection to form aFlow. The Collection may change after the Flow is created without affecting the Flow.
-
flow
public static <T> Flow<T> flow(T... values)
Creates a new Flow from the values. You should not change the values array after invoking this method (i.e., no defensive copy of the values is made).
-
flow
public static <T> Flow<T> flow(java.lang.Iterable<T> iterable)
Creates a lazy Flow from theIteratorobtained from the iterable. The Flow will be threadsafe as long as the iterable yields a new Iterator on each invocation and the underlying iterable object is not modified while the Flow is evaluating. In other words, not extremely threadsafe.
-
flow
public static <T> Flow<T> flow(java.util.Iterator<T> iterator)
Creates a lazy Flow from theIterator. The Flow will be threadsafe as long as the underlying iterable object is not modified while the Flow is evaluating. In other words, not extremely threadsafe.- Since:
- 5.3
-
zippedFlow
public static <A,B> ZippedFlow<A,B> zippedFlow(java.util.Map<A,B> map)
Creates a ZippedFlow from the provided map; the order of the tuples in the ZippedFlow is defined by the iteration order of the map entries.- Type Parameters:
A- type of key and first tuple valueB- type of value and second tuple value- Parameters:
map- source of tuples- Returns:
- zipped flow created from map
- Since:
- 5.3
-
range
public static Flow<java.lang.Integer> range(int lower, int upper)
Creates a lazy Flow that returns integers in the given range. The range starts with the lower value and counts by 1 up to the upper range (which is not part of the Flow). If lower equals upper, the Flow is empty. If upper is less than lower, the Flow counts down instead.- Parameters:
lower- start of range (inclusive)upper- end of range (exclusive)
-
lazy
public static <T> Flow<T> lazy(LazyFunction<T> function)
Creates aFlowfrom a lazy function.
-
iterate
public static <T> Flow<T> iterate(T initial, Mapper<T,T> function)
Creates an infinite lazy flow from an initial value and a function to map from the current value to the next value.- Parameters:
initial- initial value in flowfunction- maps from current value in flow to next value in flow- Returns:
- lazy flow
-
series
public static Flow<java.lang.Integer> series(int start, int delta)
Creates an infinite series of numbers. Attempting to get the FlowOperations.count() of the series will form an infinite loop.
-
addToCollection
public static <T> Worker<T> addToCollection(java.util.Collection<T> coll)
A Worker factory; the returnedWorker adds the values to a provided collection.
-
startsWith
public static Predicate<java.lang.String> startsWith(java.lang.String prefix)
A Predicate factory for matching String elements with a given prefix.- Since:
- 5.3
-
startsWithIgnoringCase
public static Predicate<java.lang.String> startsWithIgnoringCase(java.lang.String prefix)
AsstartsWith(String), but ignores case.- Since:
- 5.3
-
endsWith
public static Predicate<java.lang.String> endsWith(java.lang.String suffix)
A Predicate factory for matching String elements with a given suffix.- Since:
- 5.3
-
endsWithIgnoringCase
public static Predicate<java.lang.String> endsWithIgnoringCase(java.lang.String suffix)
As withendsWith(String)but ignores case.- Since:
- 5.3
-
orderByFirst
public static <A extends java.lang.Comparable<A>,B> java.util.Comparator<Tuple<A,B>> orderByFirst()
Creates a Comparator for the Tuples of aZippedFlowthat sorts the Tuple elements based on the first value in the Tuple.- Since:
- 5.3
-
orderBySecond
public static <A,B extends java.lang.Comparable<B>> java.util.Comparator<Tuple<A,B>> orderBySecond()
Creates a Comparator for the Tuples of aZippedFlowthat sorts the Tuple elements based on the first value in the Tuple.- Since:
- 5.3
-
not
public static <T> Predicate<T> not(Predicate<? super T> delegate)
Inverts a predicate.- Parameters:
delegate- the predicate to invert- Returns:
- a new predicate that is inverse to the existing predicate
- Since:
- 5.3
-
combine
public static <A,B,C> Mapper<A,C> combine(Mapper<A,B> abMapper, Mapper<B,C> bcMapper)
Combines two mappers into a composite mapping from type A to type C via type B.- Parameters:
abMapper- maps from A to BbcMapper- maps from B to C- Returns:
- mapper from A to C
-
and
public static <T> Predicate<T> and(Predicate<? super T>... delegates)
Combines any number of delegates as a logical and operation. Evaluation terminates with the first delegate predicate that returns false.- Parameters:
delegates- to evaluate- Returns:
- combined delegate
- Since:
- 5.3
-
or
public static <T> Predicate<T> or(Predicate<? super T>... delegates)
Combines any number of delegates as a logical or operation. Evaluation terminates with the first delegate predicate that returns true.- Parameters:
delegates- to evaluate- Returns:
- combined delegate
- Since:
- 5.3
-
-