Functions.java revision 090f9b4c879985bc747c214f82c62471e60c7742
1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
2090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copyright (C) 2007 Google Inc.
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 com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkArgument;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport static com.google.common.base.Preconditions.checkNotNull;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Map;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Useful functions.
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>All methods returns serializable functions as long as they're given
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * serializable parameters.
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Bostock
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Vlad Patryshev
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Functions {
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Functions() {}
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a function that calls {@code toString()} on its argument. The
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * function does not accept nulls; it will throw a
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link NullPointerException} when applied to {@code null}.
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static Function<Object, String> toStringFunction() {
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return ToStringFunction.INSTANCE;
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // enum singleton pattern
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private enum ToStringFunction implements Function<Object, String> {
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public String apply(Object o) {
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return o.toString();
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "toString";
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the identity function.
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> Function<E, E> identity() {
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Function<E, E>) IdentityFunction.INSTANCE;
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // enum singleton pattern
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private enum IdentityFunction implements Function<Object, Object> {
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    INSTANCE;
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Object apply(Object o) {
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return o;
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "identity";
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a function which performs a map lookup. The returned function
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * throws an {@link IllegalArgumentException} if given a key that does not
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * exist in the map.
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Function<K, V> forMap(Map<K, V> map) {
91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new FunctionForMapNoDefault<K, V>(map);
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class FunctionForMapNoDefault<K, V>
95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Function<K, V>, Serializable {
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Map<K, V> map;
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    FunctionForMapNoDefault(Map<K, V> map) {
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.map = checkNotNull(map);
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public V apply(K key) {
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      V result = map.get(key);
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkArgument(result != null || map.containsKey(key),
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          "Key '%s' not present in map", key);
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object o) {
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (o instanceof FunctionForMapNoDefault) {
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        FunctionForMapNoDefault<?, ?> that = (FunctionForMapNoDefault<?, ?>) o;
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return map.equals(that.map);
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return map.hashCode();
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "forMap(" + map + ")";
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a function which performs a map lookup with a default value. The
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * function created by this method returns {@code defaultValue} for all
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * inputs that do not belong to the map's key set.
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param map source map that determines the function behavior
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param defaultValue the value to return for inputs that aren't map keys
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return function that returns {@code map.get(a)} when {@code a} is a key,
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     or {@code defaultValue} otherwise
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <K, V> Function<K, V> forMap(
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Map<K, ? extends V> map, @Nullable V defaultValue) {
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new ForMapWithDefault<K, V>(map, defaultValue);
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class ForMapWithDefault<K, V>
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Function<K, V>, Serializable {
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Map<K, ? extends V> map;
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final V defaultValue;
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ForMapWithDefault(Map<K, ? extends V> map, V defaultValue) {
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.map = checkNotNull(map);
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.defaultValue = defaultValue;
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public V apply(K key) {
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return map.containsKey(key) ? map.get(key) : defaultValue;
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object o) {
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (o instanceof ForMapWithDefault) {
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        ForMapWithDefault<?, ?> that = (ForMapWithDefault<?, ?>) o;
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return map.equals(that.map)
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            && Objects.equal(defaultValue, that.defaultValue);
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Objects.hashCode(map, defaultValue);
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "forMap(" + map + ", defaultValue=" + defaultValue + ")";
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the composition of two functions. For {@code f: A->B} and
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code g: B->C}, composition is defined as the function h such that
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code h(a) == g(f(a))} for each {@code a}.
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @see <a href="//en.wikipedia.org/wiki/Function_composition">
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * function composition</a>
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param g the second function to apply
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param f the first function to apply
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the composition of {@code f} and {@code g}
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <A, B, C> Function<A, C> compose(
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Function<B, C> g, Function<A, ? extends B> f) {
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new FunctionComposition<A, B, C>(g, f);
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class FunctionComposition<A, B, C>
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Function<A, C>, Serializable {
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Function<B, C> g;
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Function<A, ? extends B> f;
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public FunctionComposition(Function<B, C> g,
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Function<A, ? extends B> f) {
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.g = checkNotNull(g);
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.f = checkNotNull(f);
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public C apply(A a) {
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return g.apply(f.apply(a));
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof FunctionComposition) {
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        FunctionComposition<?, ?, ?> that = (FunctionComposition<?, ?, ?>) obj;
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return f.equals(that.f) && g.equals(that.g);
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return f.hashCode() ^ g.hashCode();
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return g.toString() + "(" + f.toString() + ")";
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Creates a function that returns the same boolean output as the given
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * predicate for all inputs.
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Function<T, Boolean> forPredicate(Predicate<T> predicate) {
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new PredicateFunction<T>(predicate);
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** @see Functions#forPredicate */
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class PredicateFunction<T>
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Function<T, Boolean>, Serializable {
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Predicate<T> predicate;
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private PredicateFunction(Predicate<T> predicate) {
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.predicate = checkNotNull(predicate);
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Boolean apply(T t) {
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return predicate.apply(t);
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof PredicateFunction) {
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        PredicateFunction<?> that = (PredicateFunction<?>) obj;
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return predicate.equals(that.predicate);
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return predicate.hashCode();
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "forPredicate(" + predicate + ")";
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Creates a function that returns {@code value} for any input.
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param value the constant value for the function to return
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a function that always returns {@code value}
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> Function<Object, E> constant(@Nullable E value) {
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new ConstantFunction<E>(value);
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class ConstantFunction<E>
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      implements Function<Object, E>, Serializable {
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final E value;
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public ConstantFunction(@Nullable E value) {
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.value = value;
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E apply(Object from) {
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return value;
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean equals(Object obj) {
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (obj instanceof ConstantFunction) {
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        ConstantFunction<?> that = (ConstantFunction<?>) obj;
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return Objects.equal(value, that.value);
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return false;
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int hashCode() {
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return (value == null) ? 0 : value.hashCode();
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "constant(" + value + ")";
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private static final long serialVersionUID = 0;
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
286