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 edu.internet2.middleware.grouperClientExt.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 {@code Throwable}.
25 *
26 * @param <T> Input type 1.
27 * @param <R> Return type.
28 * @param <E> Thrown exception.
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 * Returns a function that always returns its input argument.
40 *
41 * @param <T> the type of the input and output objects to the function
42 * @param <E> Thrown exception.
43 * @return a function that always returns its input argument
44 */
45 static <T, E extends Throwable> FailableFunction<T, T, E> identity() {
46 return t -> t;
47 }
48
49 /**
50 * Returns The NOP singleton.
51 *
52 * @param <T> Consumed type 1.
53 * @param <R> Return type.
54 * @param <E> Thrown exception.
55 * @return The NOP singleton.
56 */
57 static <T, R, E extends Throwable> FailableFunction<T, R, E> nop() {
58 return NOP;
59 }
60
61 /**
62 * Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
63 *
64 * @param <V> the output type of the {@code after} function, and of the composed function.
65 * @return a composed {@code FailableFunction} like {@link Function#andThen(Function)}.
66 * @param after the operation to perform after this one.
67 * @throws NullPointerException when {@code after} is null.
68 */
69 default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) {
70 Objects.requireNonNull(after);
71 return (final T t) -> after.apply(apply(t));
72 }
73
74 /**
75 * Applies this function.
76 *
77 * @param input the input for the function
78 * @return the result of the function
79 * @throws E Thrown when the function fails.
80 */
81 R apply(T input) throws E;
82
83 /**
84 * Returns a composed {@code FailableFunction} like {@link Function#compose(Function)}.
85 *
86 * @param <V> the input type to the {@code before} function, and to the composed function.
87 * @param before the operator to apply before this one.
88 * @return a a composed {@code FailableFunction} like {@link Function#compose(Function)}.
89 * @throws NullPointerException if before is null.
90 * @see #andThen(FailableFunction)
91 */
92 default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) {
93 Objects.requireNonNull(before);
94 return (final V v) -> apply(before.apply(v));
95 }
96 }