• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.base;
16 
17 import com.google.common.annotations.GwtCompatible;
18 import javax.annotation.CheckForNull;
19 import org.checkerframework.checker.nullness.qual.Nullable;
20 
21 /**
22  * Determines an output value based on an input value; a pre-Java-8 version of {@link
23  * java.util.function.Function java.util.function.Function}.
24  *
25  * <p>The {@link Functions} class provides common functions and related utilities.
26  *
27  * <p>See the Guava User Guide article on <a
28  * href="https://github.com/google/guava/wiki/FunctionalExplained">the use of {@code Function}</a>.
29  *
30  * <h3>For Java 8+ users</h3>
31  *
32  * <p>This interface is now a legacy type. Use {@code java.util.function.Function} (or the
33  * appropriate primitive specialization such as {@code ToIntFunction}) instead whenever possible.
34  * Otherwise, at least reduce <i>explicit</i> dependencies on this type by using lambda expressions
35  * or method references instead of classes, leaving your code easier to migrate in the future.
36  *
37  * <p>To use an existing function (say, named {@code function}) in a context where the <i>other
38  * type</i> of function is expected, use the method reference {@code function::apply}. A future
39  * version of {@code com.google.common.base.Function} will be made to <i>extend</i> {@code
40  * java.util.function.Function}, making conversion code necessary only in one direction. At that
41  * time, this interface will be officially discouraged.
42  *
43  * @author Kevin Bourrillion
44  * @since 2.0
45  */
46 @GwtCompatible
47 @ElementTypesAreNonnullByDefault
48 public interface Function<F extends @Nullable Object, T extends @Nullable Object> {
49   /**
50    * Returns the result of applying this function to {@code input}. This method is <i>generally
51    * expected</i>, but not absolutely required, to have the following properties:
52    *
53    * <ul>
54    *   <li>Its execution does not cause any observable side effects.
55    *   <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equal
56    *       Objects.equal}{@code (a, b)} implies that {@code Objects.equal(function.apply(a),
57    *       function.apply(b))}.
58    * </ul>
59    *
60    * @throws NullPointerException if {@code input} is null and this function does not accept null
61    *     arguments
62    */
63   @ParametricNullness
apply(@arametricNullness F input)64   T apply(@ParametricNullness F input);
65 
66   /**
67    * <i>May</i> return {@code true} if {@code object} is a {@code Function} that behaves identically
68    * to this function.
69    *
70    * <p><b>Warning: do not depend</b> on the behavior of this method.
71    *
72    * <p>Historically, {@code Function} instances in this library have implemented this method to
73    * recognize certain cases where distinct {@code Function} instances would in fact behave
74    * identically. However, as code migrates to {@code java.util.function}, that behavior will
75    * disappear. It is best not to depend on it.
76    */
77   @Override
equals(@heckForNull Object object)78   boolean equals(@CheckForNull Object object);
79 }
80