Class Functions
-
- com.google.common.base.Functions
-
@GwtCompatible public final class Functions extends
Static utility methods pertaining tocom.google.common.base.Function
instances; see that class for information about migrating tojava.util.function
.All methods return serializable functions as long as they're given serializable parameters.
See the Guava User Guide article on .
- Since:
- 2.0
- Author:
- Mike Bostock, Jared Levy
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,B,C>
Function<A,C>compose(Function<B,C> g, Function<A,? extends B> f)
Returns the composition of two functions.static <E> ,E>
E value)
Returns a function that ignores its input and always returnsvalue
.static <K,V>
Function<K,V><K,? extends V> map, V defaultValue)
Returns a function which performs a map lookup with a default value.static <K,V>
Function<K,V><K,V> map)
Returns a function which performs a map lookup.static <T> >
forPredicate(Predicate<T> predicate)
Creates a function that returns the same boolean output as the given predicate for all inputs.static <T> ,T>
forSupplier(Supplier<T> supplier)
Returns a function that ignores its input and returns the result ofsupplier.get()
.static <E> Function<E,E>
identity()
Returns the identity function.static ,>
toStringFunction()
A function equivalent to the method referenceObject::toString
, for users not yet using Java 8.
-
-
-
Method Detail
-
toStringFunction
public static ,> toStringFunction()
A function equivalent to the method referenceObject::toString
, for users not yet using Java 8. The function simply invokestoString
on its argument and returns the result. It throws a on null input.Warning: The returned function may not be consistent with equals (as documented at
Function.apply(F)
). For example, this function yields different results for the two equal instancesImmutableSet.of(1, 2)
andImmutableSet.of(2, 1)
.Warning: as with all function types in this package, avoid depending on the specific
equals
,hashCode
ortoString
behavior of the returned function. A future migration tojava.util.function
will not preserve this behavior.For Java 8 users: use the method reference
Object::toString
instead. In the future, when this class requires Java 8, this method will be deprecated. SeeFunction
for more important information about the Java 8 transition.
-
forMap
public static <K,V> <K,V> map)
Returns a function which performs a map lookup. The returned function throws an if given a key that does not exist in the map. See alsoforMap(Map, Object)
, which returns a default value in this case.Note: if
map
is aBiMap
(or can be one), you can useMaps.asConverter
instead to get a function that also supports reverse conversion.Java 8 users: if you are okay with
null
being returned for an unrecognized key (instead of an exception being thrown), you can use the method referencemap::get
instead.
-
forMap
public static <K,V> <K,? extends V> map, V defaultValue)
Returns a function which performs a map lookup with a default value. The function created by this method returnsdefaultValue
for all inputs that do not belong to the map's key set. See alsoforMap(Map)
, which throws an exception in this case.Java 8 users: you can just write the lambda expression
k -> map.getOrDefault(k, defaultValue)
instead.- Parameters:
map
- source map that determines the function behaviordefaultValue
- the value to return for inputs that aren't map keys- Returns:
- function that returns
map.get(a)
whena
is a key, ordefaultValue
otherwise
-
compose
public static <A,B,C> Function<A,C> compose(Function<B,C> g, Function<A,? extends B> f)
Returns the composition of two functions. Forf: A->B
andg: B->C
, composition is defined as the function h such thath(a) == g(f(a))
for eacha
.Java 8 users: use
g.compose(f)
or (probably clearer)f.andThen(g)
instead.- Parameters:
g
- the second function to applyf
- the first function to apply- Returns:
- the composition of
f
andg
- See Also:
-
forPredicate
public static <T> > forPredicate(Predicate<T> predicate)
Creates a function that returns the same boolean output as the given predicate for all inputs.The returned function is consistent with equals (as documented at
Function.apply(F)
) if and only ifpredicate
is itself consistent with equals.Java 8 users: use the method reference
predicate::test
instead.
-
constant
public static <E> ,E> E value)
Returns a function that ignores its input and always returnsvalue
.Java 8 users: use the lambda expression
o -> value
instead.- Parameters:
value
- the constant value for the function to return- Returns:
- a function that always returns
value
-
forSupplier
public static <T> ,T> forSupplier(Supplier<T> supplier)
Returns a function that ignores its input and returns the result ofsupplier.get()
.Java 8 users: use the lambda expression
o -> supplier.get()
instead.- Since:
- 10.0
-
-