001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import com.google.common.annotations.GwtCompatible;
018import com.google.errorprone.annotations.CanIgnoreReturnValue;
019import javax.annotation.CheckForNull;
020import org.checkerframework.checker.nullness.qual.Nullable;
021
022/**
023 * Determines an output value based on an input value; a pre-Java-8 version of {@link
024 * java.util.function.Function java.util.function.Function}.
025 *
026 * <p>The {@link Functions} class provides common functions and related utilities.
027 *
028 * <p>See the Guava User Guide article on <a
029 * href="https://github.com/google/guava/wiki/FunctionalExplained">the use of {@code Function}</a>.
030 *
031 * <h3>For Java 8+ users</h3>
032 *
033 * <p>This interface is now a legacy type. Use {@code java.util.function.Function} (or the
034 * appropriate primitive specialization such as {@code ToIntFunction}) instead whenever possible.
035 * Otherwise, at least reduce <i>explicit</i> dependencies on this type by using lambda expressions
036 * or method references instead of classes, leaving your code easier to migrate in the future.
037 *
038 * <p>To use an existing function (say, named {@code function}) in a context where the <i>other
039 * type</i> of function is expected, use the method reference {@code function::apply}. A future
040 * version of {@code com.google.common.base.Function} will be made to <i>extend</i> {@code
041 * java.util.function.Function}, making conversion code necessary only in one direction. At that
042 * time, this interface will be officially discouraged.
043 *
044 * @author Kevin Bourrillion
045 * @since 2.0
046 */
047@GwtCompatible
048@ElementTypesAreNonnullByDefault
049public interface Function<F extends @Nullable Object, T extends @Nullable Object> {
050  /**
051   * Returns the result of applying this function to {@code input}. This method is <i>generally
052   * expected</i>, but not absolutely required, to have the following properties:
053   *
054   * <ul>
055   *   <li>Its execution does not cause any observable side effects.
056   *   <li>The computation is <i>consistent with equals</i>; that is, {@link Objects#equal
057   *       Objects.equal}{@code (a, b)} implies that {@code Objects.equal(function.apply(a),
058   *       function.apply(b))}.
059   * </ul>
060   *
061   * @throws NullPointerException if {@code input} is null and this function does not accept null
062   *     arguments
063   */
064  @CanIgnoreReturnValue // TODO(kevinb): remove this
065  @ParametricNullness
066  T apply(@ParametricNullness F input);
067
068  /**
069   * <i>May</i> return {@code true} if {@code object} is a {@code Function} that behaves identically
070   * to this function.
071   *
072   * <p><b>Warning: do not depend</b> on the behavior of this method.
073   *
074   * <p>Historically, {@code Function} instances in this library have implemented this method to
075   * recognize certain cases where distinct {@code Function} instances would in fact behave
076   * identically. However, as code migrates to {@code java.util.function}, that behavior will
077   * disappear. It is best not to depend on it.
078   */
079  @Override
080  boolean equals(@CheckForNull Object object);
081}