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.BiFunction;
22  import java.util.function.Function;
23  
24  /**
25   * A functional interface like {@link BiFunction} that declares a {@link Throwable}.
26   *
27   * @param <T> Input type 1.
28   * @param <U> Input type 2.
29   * @param <R> Return type.
30   * @param <E> The kind of thrown exception or error.
31   * @since 3.11
32   */
33  @FunctionalInterface
34  public interface FailableBiFunction<T, U, R, E extends Throwable> {
35  
36      /** NOP singleton */
37      @SuppressWarnings("rawtypes")
38      FailableBiFunction NOP = (t, u) -> null;
39  
40      /**
41       * Returns The NOP singleton.
42       *
43       * @param <T> Consumed type 1.
44       * @param <U> Consumed type 2.
45       * @param <R> Return type.
46       * @param <E> The kind of thrown exception or error.
47       * @return The NOP singleton.
48       */
49      @SuppressWarnings("unchecked")
50      static <T, U, R, E extends Throwable> FailableBiFunction<T, U, R, E> nop() {
51          return NOP;
52      }
53  
54      /**
55       * Returns a composed {@link FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
56       *
57       * @param <V> the output type of the {@code after} function, and of the composed function.
58       * @param after the operation to perform after this one.
59       * @return a composed {@link FailableBiFunction} that like {@link BiFunction#andThen(Function)}.
60       * @throws NullPointerException when {@code after} is null.
61       */
62      default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
63          Objects.requireNonNull(after);
64          return (final T t, final U u) -> after.apply(apply(t, u));
65      }
66  
67      /**
68       * Applies this function.
69       *
70       * @param input1 the first input for the function
71       * @param input2 the second input for the function
72       * @return the result of the function
73       * @throws E Thrown when the function fails.
74       */
75      R apply(T input1, U input2) throws E;
76  }