Skip to content

Commit 3f69b71

Browse files
author
vsilaev
committed
Adding continuable version of functional interfaces; updating copyright headers
1 parent a2947f3 commit 3f69b71

13 files changed

+570
-6
lines changed

net.tascalate.javaflow.extras/src/main/java/org/apache/commons/javaflow/extras/CloseableIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2013-2017 Valery Silaev (http://vsilaev.com)
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

net.tascalate.javaflow.extras/src/main/java/org/apache/commons/javaflow/extras/CoIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2013-2017 Valery Silaev (http://vsilaev.com)
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.extras;
17+
18+
import java.util.Objects;
19+
20+
import org.apache.commons.javaflow.api.continuable;
21+
22+
/**
23+
* Represents a continuable operation that accepts two input arguments and returns no
24+
* result. This is the two-arity specialization of {@link ContinuableConsumer}.
25+
* Unlike most other functional interfaces, {@code ContinuableBiConsumer} is expected
26+
* to operate via side-effects.
27+
*
28+
* <p>This is a functional interface
29+
* whose functional method is {@link #accept(Object, Object)}.
30+
*
31+
* @param <T> the type of the first argument to the operation
32+
* @param <U> the type of the second argument to the operation
33+
*
34+
* @see ContinuableConsumer
35+
*/
36+
@FunctionalInterface
37+
public interface ContinuableBiConsumer<T, U> {
38+
39+
/**
40+
* Performs this operation on the given arguments.
41+
*
42+
* @param t the first input argument
43+
* @param u the second input argument
44+
*/
45+
@continuable void accept(T t, U u);
46+
47+
/**
48+
* Returns a composed {@code ContinuableBiConsumer} that performs, in sequence, this
49+
* operation followed by the {@code after} operation. If performing either
50+
* operation throws an exception, it is relayed to the caller of the
51+
* composed operation. If performing this operation throws an exception,
52+
* the {@code after} operation will not be performed.
53+
*
54+
* @param after the operation to perform after this operation
55+
* @return a composed {@code ContinuableBiConsumer} that performs in sequence this
56+
* operation followed by the {@code after} operation
57+
* @throws NullPointerException if {@code after} is null
58+
*/
59+
default ContinuableBiConsumer<T, U> andThen(ContinuableBiConsumer<? super T, ? super U> after) {
60+
Objects.requireNonNull(after);
61+
return new ContinuableBiConsumer<T, U>() {
62+
@Override
63+
public void accept(T t, U u) {
64+
ContinuableBiConsumer.this.accept(t, u);
65+
after.accept(t, u);
66+
}
67+
};
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.extras;
17+
18+
import java.util.Objects;
19+
import org.apache.commons.javaflow.api.continuable;
20+
21+
/**
22+
* Represents a function that accepts two arguments and produces a result.
23+
* This is the two-arity specialization of {@link ContinuableFunction}.
24+
*
25+
* <p>This is a functional interface
26+
* whose functional method is {@link #apply(Object, Object)}.
27+
*
28+
* @param <T> the type of the first argument to the function
29+
* @param <U> the type of the second argument to the function
30+
* @param <R> the type of the result of the function
31+
*
32+
* @see ContinuableFunction
33+
*/
34+
@FunctionalInterface
35+
public interface ContinuableBiFunction<T, U, R> {
36+
37+
/**
38+
* Applies this function to the given arguments.
39+
*
40+
* @param t the first function argument
41+
* @param u the second function argument
42+
* @return the function result
43+
*/
44+
@continuable R apply(T t, U u);
45+
46+
/**
47+
* Returns a composed function that first applies this function to
48+
* its input, and then applies the {@code after} function to the result.
49+
* If evaluation of either function throws an exception, it is relayed to
50+
* the caller of the composed function.
51+
*
52+
* @param <V> the type of output of the {@code after} function, and of the
53+
* composed function
54+
* @param after the function to apply after this function is applied
55+
* @return a composed function that first applies this function and then
56+
* applies the {@code after} function
57+
* @throws NullPointerException if after is null
58+
*/
59+
default <V> ContinuableBiFunction<T, U, V> andThen(ContinuableFunction<? super R, ? extends V> after) {
60+
Objects.requireNonNull(after);
61+
ContinuableBiFunction<T, U, R> self = this;
62+
return new ContinuableBiFunction<T, U, V>() {
63+
@Override
64+
public V apply(T t, U u) {
65+
return after.apply(self.apply(t, u));
66+
}
67+
};
68+
}
69+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.extras;
17+
18+
import java.util.Comparator;
19+
import java.util.Objects;
20+
21+
22+
/**
23+
* Represents an continuable operation upon two operands of the same type, producing a result
24+
* of the same type as the operands. This is a specialization of
25+
* {@link ContinuableBiFunction} for the case where the operands and the result are all of
26+
* the same type.
27+
*
28+
* <p>This is a functional interface
29+
* whose functional method is {@link #apply(Object, Object)}.
30+
*
31+
* @param <T> the type of the operands and result of the operator
32+
*
33+
* @see ContinuableBiFunction
34+
* @see ContinuableUnaryOperator
35+
*/
36+
@FunctionalInterface
37+
public interface ContinuableBinaryOperator<T> extends ContinuableBiFunction<T,T,T> {
38+
/**
39+
* Returns a {@link ContinuableBinaryOperator} which returns the lesser of two elements
40+
* according to the specified {@code Comparator}.
41+
*
42+
* @param <T> the type of the input arguments of the comparator
43+
* @param comparator a {@code Comparator} for comparing the two values
44+
* @return a {@code ContinuableBinaryOperator} which returns the lesser of its operands,
45+
* according to the supplied {@code Comparator}
46+
* @throws NullPointerException if the argument is null
47+
*/
48+
public static <T> ContinuableBinaryOperator<T> minBy(Comparator<? super T> comparator) {
49+
Objects.requireNonNull(comparator);
50+
return new ContinuableBinaryOperator<T>() {
51+
@Override
52+
public T apply(T a, T b) {
53+
return comparator.compare(a, b) <= 0 ? a : b;
54+
}
55+
56+
};
57+
58+
}
59+
60+
/**
61+
* Returns a {@link ContinuableBinaryOperator} which returns the greater of two elements
62+
* according to the specified {@code Comparator}.
63+
*
64+
* @param <T> the type of the input arguments of the comparator
65+
* @param comparator a {@code Comparator} for comparing the two values
66+
* @return a {@code ContinuableBinaryOperator} which returns the greater of its operands,
67+
* according to the supplied {@code Comparator}
68+
* @throws NullPointerException if the argument is null
69+
*/
70+
public static <T> ContinuableBinaryOperator<T> maxBy(Comparator<? super T> comparator) {
71+
Objects.requireNonNull(comparator);
72+
return new ContinuableBinaryOperator<T>() {
73+
@Override
74+
public T apply(T a, T b) {
75+
return comparator.compare(a, b) >= 0 ? a : b;
76+
}
77+
78+
};
79+
}
80+
}

net.tascalate.javaflow.extras/src/main/java/org/apache/commons/javaflow/extras/ContinuableConsumer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2013-2017 Valery Silaev (http://vsilaev.com)
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public interface ContinuableConsumer<T> {
5454
default ContinuableConsumer<T> andThen(ContinuableConsumer<? super T> after) {
5555
Objects.requireNonNull(after);
5656
return new ContinuableConsumer<T>() {
57-
public @continuable void accept(T t) {
57+
public void accept(T t) {
5858
ContinuableConsumer.this.accept(t);
5959
after.accept(t);
6060
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright 2013-2018 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.extras;
17+
18+
import java.util.Objects;
19+
20+
import org.apache.commons.javaflow.api.continuable;
21+
22+
/**
23+
* Represents a continuable function that accepts one argument and produces a result.
24+
*
25+
* <p>This is a functional interface
26+
* whose functional method is {@link #apply(Object)}.
27+
*
28+
* @param <T> the type of the input to the function
29+
* @param <R> the type of the result of the function
30+
*
31+
*/
32+
@FunctionalInterface
33+
public interface ContinuableFunction<T, R> {
34+
35+
/**
36+
* Applies this function to the given argument.
37+
*
38+
* @param t the function argument
39+
* @return the function result
40+
*/
41+
@continuable R apply(T t);
42+
43+
/**
44+
* Returns a composed function that first applies the {@code before}
45+
* function to its input, and then applies this function to the result.
46+
* If evaluation of either function throws an exception, it is relayed to
47+
* the caller of the composed function.
48+
*
49+
* @param <V> the type of input to the {@code before} function, and to the
50+
* composed function
51+
* @param before the function to apply before this function is applied
52+
* @return a composed function that first applies the {@code before}
53+
* function and then applies this function
54+
* @throws NullPointerException if before is null
55+
*
56+
* @see #andThen(ContinuableFunction)
57+
*/
58+
default <V> ContinuableFunction<V, R> compose(ContinuableFunction<? super V, ? extends T> before) {
59+
Objects.requireNonNull(before);
60+
ContinuableFunction<T, R> self = this;
61+
return new ContinuableFunction<V, R>() {
62+
@Override
63+
public R apply(V v) {
64+
return self.apply(before.apply(v));
65+
}
66+
};
67+
}
68+
69+
/**
70+
* Returns a composed function that first applies this function to
71+
* its input, and then applies the {@code after} function to the result.
72+
* If evaluation of either function throws an exception, it is relayed to
73+
* the caller of the composed function.
74+
*
75+
* @param <V> the type of output of the {@code after} function, and of the
76+
* composed function
77+
* @param after the function to apply after this function is applied
78+
* @return a composed function that first applies this function and then
79+
* applies the {@code after} function
80+
* @throws NullPointerException if after is null
81+
*
82+
* @see #compose(ContinuableFunction)
83+
*/
84+
default <V> ContinuableFunction<T, V> andThen(ContinuableFunction<? super R, ? extends V> after) {
85+
Objects.requireNonNull(after);
86+
ContinuableFunction<T, R> self = this;
87+
return new ContinuableFunction<T, V>() {
88+
@Override
89+
public V apply(T v) {
90+
return after.apply(self.apply(v));
91+
}
92+
};
93+
}
94+
95+
/**
96+
* Returns a function that always returns its input argument.
97+
*
98+
* @param <T> the type of the input and output objects to the function
99+
* @return a function that always returns its input argument
100+
*/
101+
static <T> ContinuableFunction<T, T> identity() {
102+
return new ContinuableFunction<T, T>() {
103+
@Override
104+
public T apply(T v) {
105+
return v;
106+
}
107+
};
108+
}
109+
}

0 commit comments

Comments
 (0)