1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.base; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkArgument; 20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull; 21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Map; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Static utility methods pertaining to {@code Function} instances. 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 337dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>All methods return serializable functions as long as they're given serializable parameters. 347dd252788645e940eada959bdde927426e2531c9Paul Duffin * 357dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>See the Guava User Guide article on <a href= 367dd252788645e940eada959bdde927426e2531c9Paul Duffin * "http://code.google.com/p/guava-libraries/wiki/FunctionalExplained">the use of {@code 377dd252788645e940eada959bdde927426e2531c9Paul Duffin * Function}</a>. 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Bostock 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Functions { 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Functions() {} 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function that calls {@code toString()} on its argument. The function does not accept 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * nulls; it will throw a {@link NullPointerException} when applied to {@code null}. 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> The returned function may not be <i>consistent with equals</i> (as 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * documented at {@link Function#apply}). For example, this function yields different results for 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the two equal instances {@code ImmutableSet.of(1, 2)} and {@code ImmutableSet.of(2, 1)}. 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static Function<Object, String> toStringFunction() { 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ToStringFunction.INSTANCE; 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // enum singleton pattern 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private enum ToStringFunction implements Function<Object, String> { 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson INSTANCE; 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 630888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public String apply(Object o) { 650888a09821a98ac0680fad765217302858e70fa4Paul Duffin checkNotNull(o); // eager for GWT. 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return o.toString(); 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 690888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "toString"; 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the identity function. 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 770888a09821a98ac0680fad765217302858e70fa4Paul Duffin // implementation is "fully variant"; E has become a "pass-through" type 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <E> Function<E, E> identity() { 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Function<E, E>) IdentityFunction.INSTANCE; 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // enum singleton pattern 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private enum IdentityFunction implements Function<Object, Object> { 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson INSTANCE; 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 870888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 887dd252788645e940eada959bdde927426e2531c9Paul Duffin @Nullable 897dd252788645e940eada959bdde927426e2531c9Paul Duffin public Object apply(@Nullable Object o) { 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return o; 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 930888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "identity"; 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function which performs a map lookup. The returned function throws an {@link 1000888a09821a98ac0680fad765217302858e70fa4Paul Duffin * IllegalArgumentException} if given a key that does not exist in the map. See also {@link 1010888a09821a98ac0680fad765217302858e70fa4Paul Duffin * #forMap(Map, Object)}, which returns a default value in this case. 1020888a09821a98ac0680fad765217302858e70fa4Paul Duffin * 1030888a09821a98ac0680fad765217302858e70fa4Paul Duffin * <p>Note: if {@code map} is a {@link com.google.common.collect.BiMap BiMap} (or can be one), you 1040888a09821a98ac0680fad765217302858e70fa4Paul Duffin * can use {@link com.google.common.collect.Maps#asConverter Maps.asConverter} instead to get a 1050888a09821a98ac0680fad765217302858e70fa4Paul Duffin * function that also supports reverse conversion. 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> Function<K, V> forMap(Map<K, V> map) { 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new FunctionForMapNoDefault<K, V>(map); 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class FunctionForMapNoDefault<K, V> implements Function<K, V>, Serializable { 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Map<K, V> map; 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionForMapNoDefault(Map<K, V> map) { 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = checkNotNull(map); 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1180888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 1197dd252788645e940eada959bdde927426e2531c9Paul Duffin public V apply(@Nullable K key) { 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson V result = map.get(key); 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(result != null || map.containsKey(key), "Key '%s' not present in map", key); 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return result; 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1250888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object o) { 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (o instanceof FunctionForMapNoDefault) { 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionForMapNoDefault<?, ?> that = (FunctionForMapNoDefault<?, ?>) o; 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.equals(that.map); 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1330888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.hashCode(); 135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1370888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forMap(" + map + ")"; 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function which performs a map lookup with a default value. The function created by 1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * this method returns {@code defaultValue} for all inputs that do not belong to the map's key 1470888a09821a98ac0680fad765217302858e70fa4Paul Duffin * set. See also {@link #forMap(Map)}, which throws an exception in this case. 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param map source map that determines the function behavior 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param defaultValue the value to return for inputs that aren't map keys 1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return function that returns {@code map.get(a)} when {@code a} is a key, or {@code 1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * defaultValue} otherwise 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <K, V> Function<K, V> forMap(Map<K, ? extends V> map, @Nullable V defaultValue) { 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new ForMapWithDefault<K, V>(map, defaultValue); 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ForMapWithDefault<K, V> implements Function<K, V>, Serializable { 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Map<K, ? extends V> map; 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final V defaultValue; 161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ForMapWithDefault(Map<K, ? extends V> map, @Nullable V defaultValue) { 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = checkNotNull(map); 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.defaultValue = defaultValue; 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1670888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 1687dd252788645e940eada959bdde927426e2531c9Paul Duffin public V apply(@Nullable K key) { 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert V result = map.get(key); 1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (result != null || map.containsKey(key)) ? result : defaultValue; 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1730888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object o) { 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (o instanceof ForMapWithDefault) { 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ForMapWithDefault<?, ?> that = (ForMapWithDefault<?, ?>) o; 1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return map.equals(that.map) && Objects.equal(defaultValue, that.defaultValue); 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1810888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Objects.hashCode(map, defaultValue); 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1850888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forMap(" + map + ", defaultValue=" + defaultValue + ")"; 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the composition of two functions. For {@code f: A->B} and {@code g: B->C}, composition 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * is defined as the function h such that {@code h(a) == g(f(a))} for each {@code a}. 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param g the second function to apply 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param f the first function to apply 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the composition of {@code f} and {@code g} 1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @see <a href="//en.wikipedia.org/wiki/Function_composition">function composition</a> 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <A, B, C> Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f) { 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new FunctionComposition<A, B, C>(g, f); 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class FunctionComposition<A, B, C> implements Function<A, C>, Serializable { 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Function<B, C> g; 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Function<A, ? extends B> f; 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public FunctionComposition(Function<B, C> g, Function<A, ? extends B> f) { 210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.g = checkNotNull(g); 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.f = checkNotNull(f); 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2140888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 2157dd252788645e940eada959bdde927426e2531c9Paul Duffin public C apply(@Nullable A a) { 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return g.apply(f.apply(a)); 217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2190888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object obj) { 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof FunctionComposition) { 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionComposition<?, ?, ?> that = (FunctionComposition<?, ?, ?>) obj; 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.equals(that.f) && g.equals(that.g); 223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2270888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.hashCode() ^ g.hashCode(); 229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2310888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 2320888a09821a98ac0680fad765217302858e70fa4Paul Duffin return g + "(" + f + ")"; 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates a function that returns the same boolean output as the given predicate for all inputs. 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The returned function is <i>consistent with equals</i> (as documented at {@link 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Function#apply}) if and only if {@code predicate} is itself consistent with equals. 243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Function<T, Boolean> forPredicate(Predicate<T> predicate) { 245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new PredicateFunction<T>(predicate); 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Functions#forPredicate */ 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class PredicateFunction<T> implements Function<T, Boolean>, Serializable { 250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Predicate<T> predicate; 251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private PredicateFunction(Predicate<T> predicate) { 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.predicate = checkNotNull(predicate); 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2560888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 2577dd252788645e940eada959bdde927426e2531c9Paul Duffin public Boolean apply(@Nullable T t) { 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.apply(t); 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2610888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object obj) { 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof PredicateFunction) { 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson PredicateFunction<?> that = (PredicateFunction<?>) obj; 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.equals(that.predicate); 265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2690888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.hashCode(); 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2730888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forPredicate(" + predicate + ")"; 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a function that returns {@code value} for any input. 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param value the constant value for the function to return 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a function that always returns {@code value} 285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <E> Function<Object, E> constant(@Nullable E value) { 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new ConstantFunction<E>(value); 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ConstantFunction<E> implements Function<Object, E>, Serializable { 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final E value; 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ConstantFunction(@Nullable E value) { 294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.value = value; 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2970888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public E apply(@Nullable Object from) { 299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return value; 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3020888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object obj) { 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof ConstantFunction) { 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ConstantFunction<?> that = (ConstantFunction<?>) obj; 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Objects.equal(value, that.value); 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3100888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (value == null) ? 0 : value.hashCode(); 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3140888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "constant(" + value + ")"; 316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 0; 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function that always returns the result of invoking {@link Supplier#get} on {@code 3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * supplier}, regardless of its input. 3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) { 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new SupplierFunction<T>(supplier); 3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** @see Functions#forSupplier*/ 3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class SupplierFunction<T> implements Function<Object, T>, Serializable { 3340888a09821a98ac0680fad765217302858e70fa4Paul Duffin 3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Supplier<T> supplier; 3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private SupplierFunction(Supplier<T> supplier) { 3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.supplier = checkNotNull(supplier); 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3410888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public T apply(@Nullable Object input) { 3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return supplier.get(); 3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3440888a09821a98ac0680fad765217302858e70fa4Paul Duffin 3450888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public boolean equals(@Nullable Object obj) { 3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof SupplierFunction) { 3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SupplierFunction<?> that = (SupplierFunction<?>) obj; 3481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this.supplier.equals(that.supplier); 3491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return false; 3511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3520888a09821a98ac0680fad765217302858e70fa4Paul Duffin 3530888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public int hashCode() { 3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return supplier.hashCode(); 3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3560888a09821a98ac0680fad765217302858e70fa4Paul Duffin 3570888a09821a98ac0680fad765217302858e70fa4Paul Duffin @Override public String toString() { 3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return "forSupplier(" + supplier + ")"; 3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3600888a09821a98ac0680fad765217302858e70fa4Paul Duffin 361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 364