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
          See toStringFunction().
static Function<String,String> TRIM_STRING
          See trimString().
 
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)
          Returns 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 key-to-value lookup on map.
static
<A,B> Function<A,B>
forMap(Map<? super A,? extends B> map, B defaultValue)
          Returns a function which performs key-to-value lookup on map.
static
<T> Function<T,Boolean>
forPredicate(Predicate<? super T> predicate)
          Returns a boolean-valued function that evaluates to the same result as the given predicate.
static
<E> Function<E,E>
identity()
          Returns the identity function.
static Function<Object,Integer> toHashCode()
          Returns a function that determines the Object.hashCode() of its argument.
static
<F> Function<F,String>
toStringFunction()
          Returns a function that returns the Object.toString() value of its argument.
static Function<String,String> trimString()
          A function that returns the result of calling String.trim() 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
See toStringFunction(). TODO: Consider deprecating this in favor of an accessor method like toStringFunction().


TRIM_STRING

public static final Function<String,String> TRIM_STRING
See trimString(). TODO: Deprecate this and use trimString().

Method Detail

toStringFunction

public static <F> Function<F,String> toStringFunction()
Returns a function that returns the Object.toString() value of its argument. This function is not nullable; invoking apply(null) on it throws a NullPointerException.


toHashCode

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


trimString

public static Function<String,String> trimString()
A function that returns the result of calling String.trim() on its argument. Note that this function is not @Nullable: it will throw a NullPointerException when applied to null.


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 key-to-value lookup on map.

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 a type. The function built by this method returns null for all arguments that do not belong to the map's keyset.

Parameters:
map - Map<A,B> source map
Returns:
Function<A,B> function that returns map.get(a) for each A a

forMap

public static <A,B> Function<A,B> forMap(Map<? super A,? extends B> map,
                                         @Nullable
                                         B defaultValue)
Returns a function which performs key-to-value lookup on map. The function built by this method returns defaultValue for all its arguments that do not belong to the map's keyset.

Parameters:
map - Map<A,B>
defaultValue - B
Returns:
Function f such that f(a)=map.get(a) if map.containsKey(x), and defaultValue otherwise.
See Also:
forMap(Map)

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(x) == g(f(x)) for each x.

Parameters:
g - Function<B,C>
f - Function<A,B>
Returns:
Function<A,C> composition of f and g
See Also:
function composition

forPredicate

public static <T> Function<T,Boolean> forPredicate(Predicate<? super T> predicate)
Returns a boolean-valued function that evaluates to the same result as the given predicate.


constant

public static <E> Function<Object,E> constant(@Nullable
                                              E value)
Returns 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.