@GwtCompatible public final class Functions extends Object
Function instances.
All methods return serializable functions as long as they're given serializable parameters.
See the Guava User Guide article on the use of Function.
| Modifier and Type | Method and Description |
|---|---|
static <A |
compose(Function
Returns the composition of two functions.
|
static <E> Function |
constant(E value)
Creates a function that returns
value for any input.
|
static <K |
forMap(Map
Returns a function which performs a map lookup with a default value.
|
static <K |
forMap(Map
Returns a function which performs a map lookup.
|
static <T> Function |
forPredicate(Predicate
Creates a function that returns the same boolean output as the given predicate for all inputs.
|
static <T> Function |
forSupplier(Supplier
Returns a function that always returns the result of invoking
Supplier on
supplier, regardless of its input.
|
static <E> Function |
identity()
Returns the identity function.
|
static Function |
toStringFunction()
Returns a function that calls
toString() on its argument.
|
public static Function<Object ,String > toStringFunction()
toString() on its argument. The function does not accept nulls; it will throw a
NullPointerException when applied to
null.
Warning: The returned function may not be consistent with equals (as documented at Function). For example, this function yields different results for the two equal instances ImmutableSet.of(1, 2) and ImmutableSet.of(2, 1).
public static <E> Function<E ,E> identity()
public static <K,V> Function <K ,V> forMap(Map <K ,V> map)
IllegalArgumentException if given a key that does not exist in the map. See also
forMap(Map, Object), which returns a default value in this case.
Note: if map is a BiMap (or can be one), you can use Maps.asConverter instead to get a function that also supports reverse conversion.
public static <K,V> Function <K ,V> forMap(Map <K ,? extends V> map, V defaultValue)
defaultValue for all inputs that do not belong to the map's key set. See also
forMap(Map), which throws an exception in this case.
map - source map that determines the function behavior
defaultValue - the value to return for inputs that aren't map keys
map.get(a) when
a is a key, or
defaultValue otherwise
public static <A,B ,C> Function <A ,C> compose(Function <B ,C> g, Function <A ,? extends B> f)
f: A->B and
g: B->C, composition is defined as the function h such that
h(a) == g(f(a)) for each
a.
g - the second function to apply
f - the first function to apply
f and
g
public static <T> Function<T ,Boolean > forPredicate(Predicate <T> predicate)
The returned function is consistent with equals (as documented at Function) if and only if predicate is itself consistent with equals.
public static <E> Function<Object ,E> constant(E value)
value for any input.
value - the constant value for the function to return
value
@Beta public static <T> Function<Object ,T> forSupplier(Supplier <T> supplier)
Supplier.get() on
supplier, regardless of its input.