org.qi4j.functional
Class Functions

java.lang.Object
  extended by org.qi4j.functional.Functions

public final class Functions
extends java.lang.Object

Utility functions. Combine these with methods in Iterables, for example. See FunctionsTest for usages.


Constructor Summary
Functions()
           
 
Method Summary
static
<T> java.util.Comparator<T>
comparator(Function<T,java.lang.Comparable> comparableFunction)
          Creates a comparator that takes a function as input.
static
<A,B,C> Function2<Function<? super B,C>,Function<A,B>,Function<A,C>>
compose()
           
static
<FROM,MIDDLE,TO>
Function<FROM,TO>
compose(Function<? super MIDDLE,TO> outer, Function<FROM,MIDDLE> inner)
          compose(F1(M,T),F2(F,M)) = F1(F2(F)) -> T
static
<T> Function<T,java.lang.Integer>
count(Specification<T> specification)
          Count the number of items in an iterable that matches a given specification.
static
<T> Function<T,T>
filteredMap(Specification<T> specification, Function<T,T> function)
          Only apply given function on objects that satisfies the given specification.
static
<FROM,TO> Function<FROM,TO>
fromMap(java.util.Map<FROM,TO> map)
           
static
<TO,FROM extends TO>
Function<FROM,TO>
identity()
           
static
<T> Function<T,java.lang.Integer>
indexOf(Specification<T> specification)
          Find out the index of an item matching a given specification in an iterable.
static
<T> int
indexOf(T item, java.lang.Iterable<T> iterable)
          Find out the index of an item in an iterable.
static Function<java.lang.Number,java.lang.Integer> intSum()
           
static Function<java.lang.Number,java.lang.Long> longSum()
           
static
<T> Function<T,T>
withDefault(T defaultValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Functions

public Functions()
Method Detail

compose

public static <A,B,C> Function2<Function<? super B,C>,Function<A,B>,Function<A,C>> compose()

compose

public static <FROM,MIDDLE,TO> Function<FROM,TO> compose(Function<? super MIDDLE,TO> outer,
                                                         Function<FROM,MIDDLE> inner)
compose(F1(M,T),F2(F,M)) = F1(F2(F)) -> T

Type Parameters:
FROM -
MIDDLE -
TO -
Parameters:
outer -
inner -
Returns:

identity

public static <TO,FROM extends TO> Function<FROM,TO> identity()

fromMap

public static <FROM,TO> Function<FROM,TO> fromMap(java.util.Map<FROM,TO> map)

withDefault

public static <T> Function<T,T> withDefault(T defaultValue)

longSum

public static Function<java.lang.Number,java.lang.Long> longSum()

intSum

public static Function<java.lang.Number,java.lang.Integer> intSum()

count

public static <T> Function<T,java.lang.Integer> count(Specification<T> specification)
Count the number of items in an iterable that matches a given specification. Sample usage: last( map( count( in( "X" ) ), iterable( "X","Y","X","X","Y" ) ) ) Returns: 3

Type Parameters:
T -
Parameters:
specification -
Returns:

indexOf

public static <T> Function<T,java.lang.Integer> indexOf(Specification<T> specification)
Find out the index of an item matching a given specification in an iterable. Returns -1 if it is not found.

Type Parameters:
T -
Parameters:
specification -
Returns:

indexOf

public static <T> int indexOf(T item,
                              java.lang.Iterable<T> iterable)
Find out the index of an item in an iterable.

Type Parameters:
T -
Parameters:
item -
iterable -
Returns:

filteredMap

public static <T> Function<T,T> filteredMap(Specification<T> specification,
                                            Function<T,T> function)
Only apply given function on objects that satisfies the given specification.

Type Parameters:
T -
Parameters:
specification -
function -
Returns:

comparator

public static <T> java.util.Comparator<T> comparator(Function<T,java.lang.Comparable> comparableFunction)
Creates a comparator that takes a function as input. The returned comparator will use the function once for each item in the list to be sorted by Collections.sort. This should be used if the function to generate the sort key from an object is expensive, so that it is not done many times for each item in a list.

Type Parameters:
T -
Parameters:
comparableFunction -
Returns: