org.qi4j.api.util
Class Functions

java.lang.Object
  extended by org.qi4j.api.util.Functions

public class Functions
extends Object

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


Constructor Summary
Functions()
           
 
Method Summary
static
<T> Comparator<T>
comparator(Function<T,Comparable> comparableFunction)
          Creates a comparator that takes a function as input.
static
<T> Function<T,Integer>
count(Specification<T> specification)
          Count the number of items in an iterable that matches a given specification.
static
<T> Function<T,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, Iterable<T> iterable)
          Find out the index of an item in an iterable.
static Function<Number,Integer> intSum()
           
static Function<Number,Long> longSum()
           
 
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

longSum

public static Function<Number,Long> longSum()

intSum

public static Function<Number,Integer> intSum()

count

public static <T> Function<T,Integer> count(Specification<T> specification)
Count the number of items in an iterable that matches a given specification. Sample usage: last( map( indexOf( in( "D" ) ), iterable( "A","B","C","D","D" ) ) ) Returns: 3

Type Parameters:
T -
Parameters:
specification -
Returns:

indexOf

public static <T> Function<T,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,
                              Iterable<T> iterable)
Find out the index of an item in an iterable.

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

comparator

public static <T> Comparator<T> comparator(Function<T,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: