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

Field Summary
static Function<Object,String> TO_STRING
          A function that returns Object.toString() of its argument.
 
Method Summary
static
<A,B,C> Function<A,C>
compose(Function<B,C> g, Function<A,? extends B> f)
          Returns a compostion g<sup>0</sup>f : A->C of two functions, f: A->B and g: B->C.
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<A,? extends B> map, B defaultValue)
          Returns a function which performs key-to-value lookup on map.
static
<A,B> Function<A,B>
forMap(Map<A,B> map)
          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
<A,B> Function<A,B>
narrow(Function<? super A,? extends B> function)
          Casts a Function to a more restrictive type for use with methods requiring unnecessarily specific Function types.
 
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 returns Object.toString() of its argument. Note that this function is not @Nullable: it will throw a NullPointerException when applied to null.

Note also that this is assignable to variables of type Function<? super E, String>, but not Function<E, String>.

Method Detail

identity

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


forMap

public static <A,B> Function<A,B> forMap(Map<A,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 its 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<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<B,C> g,
                                            Function<A,? extends B> f)
Returns a compostion g<sup>0</sup>f : A->C of two functions, f: A->B and g: B->C. Compostion is defined as a 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.

narrow

public static <A,B> Function<A,B> narrow(@Nullable
                                         Function<? super A,? extends B> function)
Casts a Function to a more restrictive type for use with methods requiring unnecessarily specific Function types.

If you find that a Function (such as constant(Object), a Function<Object, Bar>) does not work with your code because you require a Function<Foo, Bar> to pass to a method, then that method should require a Function<? super Foo, ? extends Bar>. Any such Function is capable of taking a Foo as an argument and returning a Bar.

If the code you are calling is part of a third-party library and cannot be fixed, you can, as a last resort, use this method to cast the returned Function to the required type.

Parameters:
function - the original Function
Returns:
a Function with the same behavior but more restrictive type parameters