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 * 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>All methods returns serializable functions as long as they're given serializable parameters. 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Bostock 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Functions { 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Functions() {} 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function that calls {@code toString()} on its argument. The function does not accept 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * nulls; it will throw a {@link NullPointerException} when applied to {@code null}. 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> The returned function may not be <i>consistent with equals</i> (as 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * documented at {@link Function#apply}). For example, this function yields different results for 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the two equal instances {@code ImmutableSet.of(1, 2)} and {@code ImmutableSet.of(2, 1)}. 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static Function<Object, String> toStringFunction() { 52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ToStringFunction.INSTANCE; 53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // enum singleton pattern 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private enum ToStringFunction implements Function<Object, String> { 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson INSTANCE; 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public String apply(Object o) { 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(o); // eager for GWT. 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return o.toString(); 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "toString"; 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the identity function. 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <E> Function<E, E> identity() { 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Function<E, E>) IdentityFunction.INSTANCE; 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // enum singleton pattern 79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private enum IdentityFunction implements Function<Object, Object> { 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson INSTANCE; 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Object apply(Object o) { 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return o; 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "identity"; 89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function which performs a map lookup. The returned function throws an {@link 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * IllegalArgumentException} if given a key that does not exist in the map. 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <K, V> Function<K, V> forMap(Map<K, V> map) { 97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new FunctionForMapNoDefault<K, V>(map); 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class FunctionForMapNoDefault<K, V> implements Function<K, V>, Serializable { 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Map<K, V> map; 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionForMapNoDefault(Map<K, V> map) { 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = checkNotNull(map); 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public V apply(K key) { 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson V result = map.get(key); 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(result != null || map.containsKey(key), "Key '%s' not present in map", key); 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return result; 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object o) { 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (o instanceof FunctionForMapNoDefault) { 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionForMapNoDefault<?, ?> that = (FunctionForMapNoDefault<?, ?>) o; 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.equals(that.map); 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return map.hashCode(); 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forMap(" + map + ")"; 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function which performs a map lookup with a default value. The function created by 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * this method returns {@code defaultValue} for all inputs that do not belong to the map's key 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * set. 137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param map source map that determines the function behavior 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param defaultValue the value to return for inputs that aren't map keys 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return function that returns {@code map.get(a)} when {@code a} is a key, or {@code 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * defaultValue} otherwise 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <K, V> Function<K, V> forMap(Map<K, ? extends V> map, @Nullable V defaultValue) { 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new ForMapWithDefault<K, V>(map, defaultValue); 145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ForMapWithDefault<K, V> implements Function<K, V>, Serializable { 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Map<K, ? extends V> map; 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final V defaultValue; 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ForMapWithDefault(Map<K, ? extends V> map, @Nullable V defaultValue) { 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.map = checkNotNull(map); 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.defaultValue = defaultValue; 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public V apply(K key) { 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert V result = map.get(key); 1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (result != null || map.containsKey(key)) ? result : defaultValue; 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object o) { 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (o instanceof ForMapWithDefault) { 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ForMapWithDefault<?, ?> that = (ForMapWithDefault<?, ?>) o; 1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return map.equals(that.map) && Objects.equal(defaultValue, that.defaultValue); 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Objects.hashCode(map, defaultValue); 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forMap(" + map + ", defaultValue=" + defaultValue + ")"; 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the composition of two functions. For {@code f: A->B} and {@code g: B->C}, composition 1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * is defined as the function h such that {@code h(a) == g(f(a))} for each {@code a}. 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param g the second function to apply 186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param f the first function to apply 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the composition of {@code f} and {@code g} 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @see <a href="//en.wikipedia.org/wiki/Function_composition">function composition</a> 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <A, B, C> Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f) { 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new FunctionComposition<A, B, C>(g, f); 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class FunctionComposition<A, B, C> implements Function<A, C>, Serializable { 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Function<B, C> g; 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Function<A, ? extends B> f; 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public FunctionComposition(Function<B, C> g, Function<A, ? extends B> f) { 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.g = checkNotNull(g); 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.f = checkNotNull(f); 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public C apply(A a) { 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return g.apply(f.apply(a)); 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof FunctionComposition) { 210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson FunctionComposition<?, ?, ?> that = (FunctionComposition<?, ?, ?>) obj; 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.equals(that.f) && g.equals(that.g); 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return f.hashCode() ^ g.hashCode(); 218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return g.toString() + "(" + f.toString() + ")"; 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates a function that returns the same boolean output as the given predicate for all inputs. 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The returned function is <i>consistent with equals</i> (as documented at {@link 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Function#apply}) if and only if {@code predicate} is itself consistent with equals. 232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Function<T, Boolean> forPredicate(Predicate<T> predicate) { 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new PredicateFunction<T>(predicate); 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** @see Functions#forPredicate */ 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class PredicateFunction<T> implements Function<T, Boolean>, Serializable { 239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Predicate<T> predicate; 240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private PredicateFunction(Predicate<T> predicate) { 242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.predicate = checkNotNull(predicate); 243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Boolean apply(T t) { 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.apply(t); 248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof PredicateFunction) { 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson PredicateFunction<?> that = (PredicateFunction<?>) obj; 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.equals(that.predicate); 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return predicate.hashCode(); 260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "forPredicate(" + predicate + ")"; 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Creates a function that returns {@code value} for any input. 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param value the constant value for the function to return 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a function that always returns {@code value} 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <E> Function<Object, E> constant(@Nullable E value) { 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new ConstantFunction<E>(value); 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ConstantFunction<E> implements Function<Object, E>, Serializable { 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final E value; 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public ConstantFunction(@Nullable E value) { 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.value = value; 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public E apply(@Nullable Object from) { 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return value; 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (obj instanceof ConstantFunction) { 293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ConstantFunction<?> that = (ConstantFunction<?>) obj; 294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Objects.equal(value, that.value); 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public int hashCode() { 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (value == null) ? 0 : value.hashCode(); 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "constant(" + value + ")"; 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final long serialVersionUID = 0; 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a function that always returns the result of invoking {@link Supplier#get} on {@code 3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * supplier}, regardless of its input. 3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) { 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new SupplierFunction<T>(supplier); 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** @see Functions#forSupplier*/ 3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class SupplierFunction<T> implements Function<Object, T>, Serializable { 3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Supplier<T> supplier; 3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private SupplierFunction(Supplier<T> supplier) { 3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.supplier = checkNotNull(supplier); 3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public T apply(@Nullable Object input) { 3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return supplier.get(); 3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean equals(@Nullable Object obj) { 3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (obj instanceof SupplierFunction) { 3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SupplierFunction<?> that = (SupplierFunction<?>) obj; 3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return this.supplier.equals(that.supplier); 3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return false; 3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public int hashCode() { 3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return supplier.hashCode(); 3441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public String toString() { 3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return "forSupplier(" + supplier + ")"; 3481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final long serialVersionUID = 0; 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 353