org.apache.tapestry5.func
Class F

java.lang.Object
  extended by org.apache.tapestry5.func.F

public class F
extends java.lang.Object

Functional operations on collections with generics support. The core interface is Flow to which operations and transformations (in terms of Predicates, Mappers and Reducers) to create new Flows. Flows are initially created using flow(Collection) and flow(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
static Mapper2<java.lang.Integer,java.lang.Integer,java.lang.Integer> ADD_INTS
           
static Reducer<java.lang.Integer,java.lang.Integer> SUM_INTS
           
 
Constructor Summary
F()
           
 
Method Summary
static
<T> Worker<T>
addToCollection(java.util.Collection<T> coll)
           
static
<S,T> Mapper<S,T>
always(T fixedResult)
          Returns a Mapper that ignores its input value and always returns a predetermined result.
static Predicate<java.lang.Number> eq(long value)
           
static
<T> Predicate<T>
eql(T value)
           
static
<T> Flow<T>
flow(java.util.Collection<T> values)
          Extracts the values from the collection to form a Flow.
static
<T> Flow<T>
flow(java.lang.Iterable<T> iterable)
          Creates a lazy Flow from the Iterator obtained from the iterable.
static
<T> Flow<T>
flow(T... values)
          Creates a new Flow from the values.
static Predicate<java.lang.Number> gt(long value)
           
static Predicate<java.lang.Number> gteq(long value)
           
static
<S> Mapper<S,S>
identity()
          The identity mapper simply returns the input unchanged.
static
<T> Predicate<T>
isNull()
           
static
<T> Flow<T>
iterate(T initial, Mapper<T,T> mapper)
          Creates a lazy, infinte Flow consisting of the initial value, then the result of passing the initial value through the Mapper, and so forth, which each step value passed through the mapper to form the next step value.
static
<T> Flow<T>
lazy(LazyFunction<T> function)
          Creates a Flow from a lazy function.
static Predicate<java.lang.Number> lt(long value)
           
static Predicate<java.lang.Number> lteq(long value)
           
static Predicate<java.lang.Number> neq(long value)
           
static
<T> Predicate<T>
notNull()
           
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 of select(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)
          Mapper factory that combines a Predicate with two Mappers; 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 of select(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
<T> Mapper<T,java.lang.String>
stringValueOf()
           
static
<S> Predicate<S>
toPredicate(Mapper<S,java.lang.Boolean> mapper)
          Allows a Mapper that maps to boolean to be used as a Predicate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUM_INTS

public static Reducer<java.lang.Integer,java.lang.Integer> SUM_INTS

ADD_INTS

public static Mapper2<java.lang.Integer,java.lang.Integer,java.lang.Integer> ADD_INTS
Constructor Detail

F

public F()
Method Detail

eql

public static <T> Predicate<T> eql(T value)

eq

public static Predicate<java.lang.Number> eq(long value)

neq

public static Predicate<java.lang.Number> neq(long value)

gt

public static Predicate<java.lang.Number> gt(long value)

gteq

public static Predicate<java.lang.Number> gteq(long value)

lt

public static Predicate<java.lang.Number> lt(long value)

lteq

public static Predicate<java.lang.Number> lteq(long value)

isNull

public static <T> Predicate<T> isNull()

notNull

public static <T> Predicate<T> notNull()

stringValueOf

public static <T> Mapper<T,java.lang.String> stringValueOf()

always

public static <S,T> Mapper<S,T> always(T fixedResult)
Returns a Mapper that 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)
Mapper factory that combines a Predicate with two Mappers; evaluating the predicate selects one of the two mappers.

Parameters:
predicate - evaluated to selected a coercion
ifAccepted - used when predicate evaluates to true
ifRejected - used when predicate evaluates to false

select

public static <S,T> Mapper<S,T> select(Predicate<? super S> predicate,
                                       Mapper<S,T> ifAccepted)
Override of select(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 of select(Predicate, Mapper) where rejected values are replaced with a fixed value.


identity

public static <S> Mapper<S,S> identity()
The identity mapper simply returns the input 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 a Flow. 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 the Iterator obtained 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.


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 a Flow from a lazy function.


series

public static Flow<java.lang.Integer> series(int start,
                                             int delta)
Creates an infinite series of numbers.

Attempting to get the Flow.count() of the series will form an infinite loop.


iterate

public static <T> Flow<T> iterate(T initial,
                                  Mapper<T,T> mapper)
Creates a lazy, infinte Flow consisting of the initial value, then the result of passing the initial value through the Mapper, and so forth, which each step value passed through the mapper to form the next step value.


addToCollection

public static <T> Worker<T> addToCollection(java.util.Collection<T> coll)


Copyright © 2010-2011 Apache Software Foundation. All Rights Reserved.