1 /*-
2 * #%L
3 * io.earcam.unexceptional
4 * %%
5 * Copyright (C) 2016 - 2017 earcam
6 * %%
7 * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8 *
9 * You <b>must</b> choose to accept, in full - any individual or combination of
10 * the following licenses:
11 * <ul>
12 * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13 * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14 * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15 * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16 * </ul>
17 * #L%
18 */
19 package io.earcam.unexceptional;
20
21 import java.util.Objects;
22
23 /**
24 * A checked parallel of {@link java.util.function.Function}
25 *
26 * @param <T> argument type
27 * @param <R> return type
28 * @param <E> the type of Throwable declared
29 *
30 * @see java.util.function.Function
31 */
32 @FunctionalInterface
33 public interface CheckedFunction<T, R, E extends Throwable> {
34
35 /**
36 * See {@link java.util.function.Function#apply(Object)}
37 *
38 * @param t the function argument
39 * @return the function result
40 * @throws Throwable any throwable
41 *
42 * @since 0.2.0
43 */
44 public abstract R apply(T t) throws E;
45
46
47 /**
48 * See {@link java.util.function.Function#compose(java.util.function.Function)}
49 *
50 * @param <V> the type of input to the {@code before} function, and to the
51 * composed function
52 * @param before the function to apply before this function is applied
53 * @return a composed function that first applies the {@code before}
54 * function and then applies this function
55 * @throws NullPointerException if {@code before} is {@code null}
56 *
57 * @since 0.2.0
58 *
59 * @see java.util.function.Function#compose(java.util.function.Function)
60 */
61 public default <V> CheckedFunction<V, R, E> compose(CheckedFunction<? super V, ? extends T, ? extends E> before)
62 {
63 Objects.requireNonNull(before);
64 return (V v) -> apply(before.apply(v));
65 }
66
67
68 /**
69 * See {@link java.util.function.Function#andThen(java.util.function.Function)}
70 *
71 * @param <V> the type of output of the {@code after} function, and of the
72 * composed function
73 * @param after the function to apply after this function is applied
74 * @return a composed function that first applies this function and then
75 * applies the {@code after} function
76 * @throws NullPointerException if {@code after} is {@code null}
77 *
78 * @since 0.2.0
79 */
80 public default <V> CheckedFunction<T, V, E> andThen(CheckedFunction<? super R, ? extends V, ? extends E> after)
81 {
82 Objects.requireNonNull(after);
83 return (T t) -> after.apply(apply(t));
84 }
85
86
87 /**
88 * See {@link java.util.function.Function#identity()}
89 *
90 * @param <T> argument and return type
91 * @return the argument as given
92 *
93 * @since 0.3.0
94 */
95 public static <T, E extends Throwable> CheckedFunction<T, T, E> identity()
96 {
97 return t -> t;
98 }
99 }