1 /* 2 * Copyright (C) 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.base; 18 19 import com.google.common.annotations.GwtCompatible; 20 21 import javax.annotation.Nullable; 22 23 /** 24 * A transformation from one object to another. For example, a 25 * {@code StringToIntegerFunction} may implement 26 * <code>Function<String,Integer></code> and transform integers in 27 * {@code String} format to {@code Integer} format. 28 * 29 * <p>The transformation on the source object does not necessarily result in 30 * an object of a different type. For example, a 31 * {@code FarenheitToCelsiusFunction} may implement 32 * <code>Function<Float,Float></code>. 33 * 34 * <p>Implementations which may cause side effects upon evaluation are strongly 35 * encouraged to state this fact clearly in their API documentation. 36 * 37 * @param <F> the type of the function input 38 * @param <T> the type of the function output 39 * @author Kevin Bourrillion 40 * @author Scott Bonneau 41 * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library) 42 */ 43 @GwtCompatible 44 public interface Function<F, T> { 45 46 /** 47 * Applies the function to an object of type {@code F}, resulting in an object 48 * of type {@code T}. Note that types {@code F} and {@code T} may or may not 49 * be the same. 50 * 51 * @param from the source object 52 * @return the resulting object 53 */ 54 T apply(@Nullable F from); 55 56 /** 57 * Indicates whether some other object is equal to this {@code Function}. 58 * This method can return {@code true} <i>only</i> if the specified object is 59 * also a {@code Function} and, for every input object {@code o}, it returns 60 * exactly the same value. Thus, {@code function1.equals(function2)} implies 61 * that either {@code function1.apply(o)} and {@code function2.apply(o)} are 62 * both null, or {@code function1.apply(o).equals(function2.apply(o))}. 63 * 64 * <p>Note that it is always safe <em>not</em> to override 65 * {@link Object#equals}. 66 */ 67 boolean equals(@Nullable Object obj); 68 } 69