Skip to content

Commit d928a69

Browse files
committed
Add Tri- and QuadFunction interfaces
Used for Ops with three/four primary/secondary input parameters. More interfaces along these lines may be coming in the near future
1 parent 71d4054 commit d928a69

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.scijava.ops.core;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
/**
7+
* Represents a function that accepts three arguments and produces a result.
8+
* This is the four-arity specialization of {@link Function}.
9+
*
10+
* <p>
11+
* This is a <a href="package-summary.html">functional interface</a> whose
12+
* functional method is {@link #apply(Object, Object)}.
13+
*
14+
* @param <I1>
15+
* the type of the first argument to the function
16+
* @param <I2>
17+
* the type of the second argument to the function
18+
* @param <I3>
19+
* the type of the third argument to the function
20+
* @param <I4>
21+
* the type of the fourth argument to the function
22+
* @param <O>
23+
* the type of the output of the function
24+
*
25+
* @see Function
26+
* @since 1.8
27+
*/
28+
@FunctionalInterface
29+
public interface QuadFunction<I1, I2, I3, I4, O> {
30+
31+
/**
32+
* Applies this function to the given arguments.
33+
*
34+
* @param t
35+
* the first function argument
36+
* @param u
37+
* the second function argument
38+
* @param v
39+
* the third function argument
40+
* @param w
41+
* the fourth function argument
42+
* @return the function output
43+
*/
44+
O apply(I1 t, I2 u, I3 v, I4 w);
45+
46+
/**
47+
* Returns a composed function that first applies this function to its input,
48+
* and then applies the {@code after} function to the result. If evaluation of
49+
* either function throws an exception, it is relayed to the caller of the
50+
* composed function.
51+
*
52+
* @param <O2>
53+
* the type of output of the {@code after} function, and of the
54+
* composed function
55+
* @param after
56+
* the function to apply after this function is applied
57+
* @return a composed function that first applies this function and then applies
58+
* the {@code after} function
59+
* @throws NullPointerException
60+
* if after is null
61+
*/
62+
default <O2> QuadFunction<I1, I2, I3, I4, O2> andThen(Function<? super O, ? extends O2> after) {
63+
Objects.requireNonNull(after);
64+
return (I1 in1, I2 in2, I3 in3, I4 in4) -> after.apply(apply(in1, in2, in3, in4));
65+
}
66+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.scijava.ops.core;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
/**
7+
* Represents a function that accepts three arguments and produces a result.
8+
* This is the three-arity specialization of {@link Function}.
9+
*
10+
* <p>
11+
* This is a <a href="package-summary.html">functional interface</a> whose
12+
* functional method is {@link #apply(Object, Object)}.
13+
*
14+
* @param <I1>
15+
* the type of the first argument to the function
16+
* @param <I2>
17+
* the type of the second argument to the function
18+
* @param <I3>
19+
* the type of the third argument to the function
20+
* @param <O>
21+
* the type of the output of the function
22+
*
23+
* @see Function
24+
* @since 1.8
25+
*/
26+
@FunctionalInterface
27+
public interface TriFunction<I1, I2, I3, O> {
28+
29+
/**
30+
* Applies this function to the given arguments.
31+
*
32+
* @param t
33+
* the first function argument
34+
* @param u
35+
* the second function argument
36+
* @param v
37+
* the third function argument
38+
* @return the function output
39+
*/
40+
O apply(I1 t, I2 u, I3 v);
41+
42+
/**
43+
* Returns a composed function that first applies this function to its input,
44+
* and then applies the {@code after} function to the result. If evaluation of
45+
* either function throws an exception, it is relayed to the caller of the
46+
* composed function.
47+
*
48+
* @param <O2>
49+
* the type of output of the {@code after} function, and of the
50+
* composed function
51+
* @param after
52+
* the function to apply after this function is applied
53+
* @return a composed function that first applies this function and then applies
54+
* the {@code after} function
55+
* @throws NullPointerException
56+
* if after is null
57+
*/
58+
default <O2> TriFunction<I1, I2, I3, O2> andThen(Function<? super O, ? extends O2> after) {
59+
Objects.requireNonNull(after);
60+
return (I1 in1, I2 in2, I3 in3) -> after.apply(apply(in1, in2, in3));
61+
}
62+
}

0 commit comments

Comments
 (0)