View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.function;
19  
20  import java.util.Objects;
21  import java.util.function.Function;
22  
23  /**
24   * A functional interface like {@link Function} that declares a {@link Throwable}.
25   *
26   * @param <T> Input type 1.
27   * @param <R> Return type.
28   * @param <E> The kind of thrown exception or error.
29   * @since 3.11
30   */
31  @FunctionalInterface
32  public interface FailableFunction<T, R, E extends Throwable> {
33  
34      /** NOP singleton */
35      @SuppressWarnings("rawtypes")
36      FailableFunction NOP = t -> null;
37  
38      /**
39       * Starts a fluent chain like {@code function(foo::bar).andThen(...).andThen(...).apply(...);}
40       *
41       * @param <T> Input type.
42       * @param <R> Return type.
43       * @param <E> The kind of thrown exception or error.
44       * @param function   the argument to return.
45       * @return the argument
46       * @since 3.14.0
47       */
48      static <T, R, E extends Throwable> FailableFunction<T, R, E> function(final FailableFunction<T, R, E> function) {
49          return function;
50      }
51  
52      /**
53       * Returns a function that always returns its input argument.
54       *
55       * @param <T> the type of the input and output objects to the function
56       * @param <E> The kind of thrown exception or error.
57       * @return a function that always returns its input argument
58       */
59      static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
60          return t -> t;
61      }
62  
63      /**
64       * Returns The NOP singleton.
65       *
66       * @param <T> Consumed type.
67       * @param <R> Return type.
68       * @param <E> The kind of thrown exception or error.
69       * @return The NOP singleton.
70       */
71      @SuppressWarnings("unchecked")
72      static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
73          return NOP;
74      }
75  
76      /**
77       * Returns a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
78       *
79       * @param <V> the output type of the {@code after} function, and of the composed function.
80       * @return a composed {@link FailableFunction} like {@link Function#andThen(Function)}.
81       * @param after the operation to perform after this one.
82       * @throws NullPointerException when {@code after} is null.
83       */
84      default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
85          Objects.requireNonNull(after);
86          return (final T t) -> after.apply(apply(t));
87      }
88  
89      /**
90       * Applies this function.
91       *
92       * @param input the input for the function
93       * @return the result of the function
94       * @throws E Thrown when the function fails.
95       */
96      R apply(T input) throws E;
97  
98      /**
99       * Returns a composed {@link FailableFunction} like {@link Function#compose(Function)}.
100      *
101      * @param <V> the input type to the {@code before} function, and to the composed function.
102      * @param before the operator to apply before this one.
103      * @return a composed {@link FailableFunction} like {@link Function#compose(Function)}.
104      * @throws NullPointerException if before is null.
105      * @see #andThen(FailableFunction)
106      */
107     default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
108         Objects.requireNonNull(before);
109         return (final V v) -> apply(before.apply(v));
110     }
111 }