jdk/src/share/classes/java/util/function/Function.java
changeset 17695 032254f2467b
parent 16011 890a7ed97f6c
child 18584 430628bf6412
--- a/jdk/src/share/classes/java/util/function/Function.java	Wed May 15 10:25:59 2013 +0200
+++ b/jdk/src/share/classes/java/util/function/Function.java	Fri May 17 10:36:04 2013 -0700
@@ -24,14 +24,15 @@
  */
 package java.util.function;
 
+import java.util.Objects;
 
 /**
  * Apply a function to the input argument, yielding an appropriate result.  A
  * function may variously provide a mapping between types, object instances or
  * keys and values or any other form of transformation upon the input.
  *
- * @param <T> the type of the input to the {@code apply} operation.
- * @param <R> the type of the result of the {@code apply} operation.
+ * @param <T> the type of the input to the {@code apply} operation
+ * @param <R> the type of the result of the {@code apply} operation
  *
  * @since 1.8
  */
@@ -44,5 +45,50 @@
      * @param t the input object
      * @return the function result
      */
-    public R apply(T t);
+    R apply(T t);
+
+    /**
+     * Returns a new function which applies the provided function followed by
+     * this function.  If either function throws an exception, it is relayed
+     * to the caller.
+     *
+     * @param <V> type of input objects to the combined function. May be the
+     * same type as {@code <T>} or {@code <R>}
+     * @param before an additional function to be applied before this function
+     * is applied
+     * @return a function which performs the provided function followed by this
+     * function
+     * @throws NullPointerException if before is null
+     */
+    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
+        Objects.requireNonNull(before);
+        return (V v) -> apply(before.apply(v));
+    }
+
+    /**
+     * Returns a new function which applies this function followed by the
+     * provided function.  If either function throws an exception, it is relayed
+     * to the caller.
+     *
+     * @param <V> type of output objects to the combined function. May be the
+     * same type as {@code <T>} or {@code <R>}
+     * @param after an additional function to be applied after this function is
+     * applied
+     * @return a function which performs this function followed by the provided
+     * function
+     * @throws NullPointerException if after is null
+     */
+    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
+        Objects.requireNonNull(after);
+        return (T t) -> after.apply(apply(t));
+    }
+
+    /**
+     * Returns a {@code Function} whose {@code apply} method returns its input.
+     *
+     * @param <T> the type of the input and output objects to the function
+     */
+    static <T> Function<T, T> identity() {
+        return t -> t;
+    }
 }