com.google.common.base
Class Functions

java.lang.Object
  extended by com.google.common.base.Functions

public final class Functions
extends Object

Useful functions.

Author:
Mike Bostock, Vlad Patryshev, Jared Levy

Field Summary
static Function<Object,String> TO_STRING
          A function that calls toString() on its argument.
 
Method Summary
static
<A,B,C> Function<A,C>
compose(Function<? super B,? extends C> g, Function<? super A,? extends B> f)
          Returns the composition of two functions.
static
<E> Function<Object,E>
constant(E value)
          Creates a function that returns value for any input.
static
<A,B> Function<A,B>
forMap(Map<? super A,? extends B> map)
          Returns a function which performs a map lookup.
static
<A,B> Function<A,B>
forMap(Map<? super A,? extends B> map, B defaultValue)
          Returns a function which performs a map lookup with a default value.
static
<T> Function<T,Boolean>
forPredicate(Predicate<? super T> predicate)
          Creates a function that returns the same boolean output as the given predicate for all inputs.
static
<E> Function<E,E>
identity()
          Returns the identity function.
static Function<Object,Integer> toHashCode()
          Returns a function that determines the hash code of its argument.
static
<F> Function<F,String>
toStringFunction()
          Returns a function that calls toString() on its argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TO_STRING

public static final Function<Object,String> TO_STRING
A function that calls toString() on its argument. This function does not accept nulls; it will throw a NullPointerException when applied to null.

TODO: Consider deprecating this in favor of toStringFunction().

Method Detail

toStringFunction

public static <F> Function<F,String> toStringFunction()
Returns a function that calls toString() on its argument. The function does not accept nulls; it will throw a NullPointerException when applied to null.


toHashCode

public static Function<Object,Integer> toHashCode()
Returns a function that determines the hash code of its argument. For null arguments, the function returns 0, for consistency with the hash code calculations in the Java Collections classes.


identity

public static <E> Function<E,E> identity()
Returns the identity function.


forMap

public static <A,B> Function<A,B> forMap(Map<? super A,? extends B> map)
Returns a function which performs a map lookup.

The difference between a map and a function is that a map is defined on a set of keys, while a function is defined on all inputs of the correct type. The function created by this method returns null for all inputs that do not belong to the map's key set.

Parameters:
map - source map that determines the function behavior
Returns:
function that returns map.get(a) for each a

forMap

public static <A,B> Function<A,B> forMap(Map<? super A,? extends B> map,
                                         @Nullable
                                         B defaultValue)
Returns a function which performs a map lookup with a default value. The function created by this method returns defaultValue for all inputs that do not belong to the map's key set.

Parameters:
map - source map that determines the function behavior
defaultValue - the value to return for inputs that aren't map keys
Returns:
function that returns map.get(a) when a is a key, or defaultValue otherwise

compose

public static <A,B,C> Function<A,C> compose(Function<? super B,? extends C> g,
                                            Function<? super A,? extends B> f)
Returns the composition of two functions. For f: A->B and g: B->C, composition is defined as the function h such that h(a) == g(f(a)) for each a.

Parameters:
g - the second function to apply
f - the first function to apply
Returns:
the composition of f and g
See Also:
function composition

forPredicate

public static <T> Function<T,Boolean> forPredicate(Predicate<? super T> predicate)
Creates a function that returns the same boolean output as the given predicate for all inputs.


constant

public static <E> Function<Object,E> constant(@Nullable
                                              E value)
Creates a function that returns value for any input.

Parameters:
value - the constant value for the function to return
Returns:
a function that always returns value