Skip to content

Commit 3681020

Browse files
committed
[jOOQ#395] Predicate 0-16 (similar to Function 0-16)
1 parent e6f1457 commit 3681020

40 files changed

+6172
-28
lines changed

jOOL-java-8/src/main/java/org/jooq/lambda/function/Function0.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
*/
1616
package org.jooq.lambda.function;
1717

18-
import java.util.function.Supplier;
19-
2018
import org.jooq.lambda.tuple.Tuple0;
2119

20+
import java.util.concurrent.Callable;
21+
import java.util.function.Supplier;
22+
2223
/**
2324
* A function with 0 arguments.
2425
*
2526
* @author Lukas Eder
2627
*/
2728
@FunctionalInterface
28-
public interface Function0<R> extends Supplier<R> {
29+
public interface Function0<R> extends Supplier<R>, Callable<R> {
2930

3031
/**
3132
* Apply this function to the arguments.
@@ -63,4 +64,7 @@ static <R> Function0<R> from(Supplier<R> supplier) {
6364
return supplier::get;
6465
}
6566

66-
}
67+
@Override default R call () {
68+
return get();
69+
}
70+
}

jOOL-java-8/src/main/java/org/jooq/lambda/function/Functions.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,37 @@
1515
*/
1616
package org.jooq.lambda.function;
1717

18-
import java.util.function.Predicate;
19-
2018
import org.jooq.lambda.Seq;
2119

20+
import java.util.function.Predicate;
21+
2222
/**
2323
* @author Lukas Eder
2424
*/
2525
public final class Functions {
26+
private Functions () {}
27+
2628

2729
/**
2830
* Negate a predicate.
2931
*/
30-
public static final <T> Predicate<T> not(Predicate<T> predicate) {
32+
public static <T> Predicate<T> not(Predicate<T> predicate) {
3133
return predicate.negate();
3234
}
3335

3436
/**
35-
* Negate a predicate.
37+
* AND all predicates.
3638
*/
3739
@SafeVarargs
38-
public static final <T> Predicate<T> and(Predicate<T>... predicates) {
39-
return Seq.of(predicates).reduce(t -> true, (t1, t2) -> t1.and(t2));
40+
public static <T> Predicate<T> and(Predicate<T>... predicates) {
41+
return Seq.of(predicates).reduce(t -> true, Predicate::and);
4042
}
4143

4244
/**
43-
* Negate a predicate.
45+
* OR all predicates.
4446
*/
4547
@SafeVarargs
46-
public static final <T> Predicate<T> or(Predicate<T>... predicates) {
47-
return Seq.of(predicates).reduce(t -> false, (t1, t2) -> t1.or(t2));
48+
public static <T> Predicate<T> or(Predicate<T>... predicates) {
49+
return Seq.of(predicates).reduce(t -> false, Predicate::or);
4850
}
49-
}
51+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.jooq.lambda.function;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
Common ancestor for all our Predicates 0..16.
7+
And dangerous dynamic (no help from compiler! All errors in runtime only!) invocations.
8+
<br><br>
9+
!!! For very special cases ONLY!!!
10+
11+
@author Andrej Fink
12+
*/
13+
public interface GenericPredicate {
14+
15+
/**
16+
args.length is used to cast generic variable predicateWithArgsLenArgs to real PredicateX.
17+
18+
E.g. if args.length == 1 → Predicate1 will be called.
19+
If predicateWithArgsLenArgs contains another type (e.g. Predicate3) → ClassCastException will be thrown.
20+
*/
21+
@SuppressWarnings({"unchecked", "rawtypes"})
22+
static boolean testDynamicArgCntToPredicate (GenericPredicate predicateWithArgsLenArgs, Object... args) throws ClassCastException, IllegalArgumentException {
23+
if (args == null || args.length == 0) {
24+
return ((Predicate0) predicateWithArgsLenArgs).test();
25+
}
26+
switch (args.length) {
27+
case 1: return ((Predicate1) predicateWithArgsLenArgs).test(args[0]);
28+
case 2: return ((Predicate2) predicateWithArgsLenArgs).test(args[0],args[1]);
29+
case 3: return ((Predicate3) predicateWithArgsLenArgs).test(args[0],args[1],args[2]);
30+
case 4: return ((Predicate4) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3]);
31+
case 5: return ((Predicate5) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4]);
32+
case 6: return ((Predicate6) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5]);
33+
case 7: return ((Predicate7) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
34+
case 8: return ((Predicate8) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
35+
case 9: return ((Predicate9) predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
36+
case 10: return ((Predicate10)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
37+
case 11: return ((Predicate11)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
38+
case 12: return ((Predicate12)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
39+
case 13: return ((Predicate13)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
40+
case 14: return ((Predicate14)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13]);
41+
case 15: return ((Predicate15)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14]);
42+
case 16: return ((Predicate16)predicateWithArgsLenArgs).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14],args[15]);
43+
default: throw new IllegalArgumentException("Args.length must be in range [0..16], but "+args.length+": "+ Arrays.toString(args));
44+
}
45+
}
46+
47+
/**
48+
A runtime object-type referenced from masterPredicateToSatisfy is used (e.g. MyPredicate1) to call it
49+
with required quantity of args.
50+
If optArgs.length is less than required - missing arguments will be filled with null:
51+
<pre><code>
52+
masterPredicateToSatisfy = new MyPredicate2();
53+
→ `Predicate2.test(a)` → Predicate2.test(a, null)
54+
</code></pre>
55+
*/
56+
@SuppressWarnings({"unchecked", "rawtypes"})
57+
static boolean testDynamicPredicateVarArgs (GenericPredicate masterPredicateToSatisfy, Object... optArgs) throws ClassCastException, IllegalArgumentException, NullPointerException {
58+
final Object[] args = new Object[16];// 0..15 >= optArgs.length; extra args are null
59+
if (optArgs != null && optArgs.length>0) {
60+
System.arraycopy(optArgs, 0, args, 0, Math.min(16, optArgs.length));
61+
}
62+
63+
if (masterPredicateToSatisfy instanceof Predicate0) {
64+
return ((Predicate0) masterPredicateToSatisfy).test();
65+
} else if (masterPredicateToSatisfy instanceof Predicate1) {
66+
return ((Predicate1) masterPredicateToSatisfy).test(args[0]);
67+
} else if (masterPredicateToSatisfy instanceof Predicate2) {
68+
return ((Predicate2) masterPredicateToSatisfy).test(args[0],args[1]);
69+
} else if (masterPredicateToSatisfy instanceof Predicate3) {
70+
return ((Predicate3) masterPredicateToSatisfy).test(args[0],args[1],args[2]);
71+
} else if (masterPredicateToSatisfy instanceof Predicate4) {
72+
return ((Predicate4) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3]);
73+
} else if (masterPredicateToSatisfy instanceof Predicate5) {
74+
return ((Predicate5) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4]);
75+
} else if (masterPredicateToSatisfy instanceof Predicate6) {
76+
return ((Predicate6) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5]);
77+
} else if (masterPredicateToSatisfy instanceof Predicate7) {
78+
return ((Predicate7) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
79+
} else if (masterPredicateToSatisfy instanceof Predicate8) {
80+
return ((Predicate8) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
81+
} else if (masterPredicateToSatisfy instanceof Predicate9) {
82+
return ((Predicate9) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
83+
} else if (masterPredicateToSatisfy instanceof Predicate10) {
84+
return ((Predicate10) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
85+
} else if (masterPredicateToSatisfy instanceof Predicate11) {
86+
return ((Predicate11) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
87+
} else if (masterPredicateToSatisfy instanceof Predicate12) {
88+
return ((Predicate12) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
89+
} else if (masterPredicateToSatisfy instanceof Predicate13) {
90+
return ((Predicate13) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
91+
} else if (masterPredicateToSatisfy instanceof Predicate14) {
92+
return ((Predicate14) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13]);
93+
} else if (masterPredicateToSatisfy instanceof Predicate15) {
94+
return ((Predicate15) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14]);
95+
} else if (masterPredicateToSatisfy instanceof Predicate16) {
96+
return ((Predicate16) masterPredicateToSatisfy).test(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14],args[15]);
97+
}
98+
throw new IllegalArgumentException("Unknown GenericPredicate successor: "+masterPredicateToSatisfy+", args: "+Arrays.toString(args));
99+
}
100+
101+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright (c), Data Geekery GmbH, contact@datageekery.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.jooq.lambda.function;
17+
18+
import org.jooq.lambda.tuple.Tuple0;
19+
20+
import java.util.concurrent.Callable;
21+
import java.util.function.BooleanSupplier;
22+
import java.util.function.Supplier;
23+
24+
/**
25+
* A Predicate with 0 arguments.
26+
*
27+
* @author Lukas Eder, Andrej Fink
28+
*/
29+
@FunctionalInterface
30+
public interface Predicate0 extends Supplier<Boolean>, Function0<Boolean>, Callable<Boolean>, BooleanSupplier, GenericPredicate {
31+
32+
@Override boolean getAsBoolean ();
33+
34+
35+
/**
36+
* Apply this Predicate to the arguments.
37+
*/
38+
default boolean test() {
39+
return getAsBoolean();
40+
}
41+
42+
/**
43+
* Apply this Predicate to the arguments.
44+
*
45+
* @param args The arguments as a tuple.
46+
*/
47+
default boolean test(Tuple0 args) {
48+
return getAsBoolean();
49+
}
50+
51+
/**
52+
* Predicate as {@link Supplier}
53+
*/
54+
@Override
55+
default Boolean get() {
56+
return getAsBoolean();
57+
}
58+
59+
/**
60+
* Predicate as {@link Callable}
61+
*/
62+
@Override default Boolean call () {
63+
return getAsBoolean();
64+
}
65+
66+
/**
67+
* Convert this Predicate to a {@link java.util.function.Supplier}
68+
*/
69+
@Override default Supplier<Boolean> toSupplier() {
70+
return this;
71+
}
72+
73+
/**
74+
* Convert to this Predicate from a {@link java.util.function.Supplier}
75+
*/
76+
static Predicate0 from(Supplier<Boolean> supplier) {
77+
return supplier::get;
78+
}
79+
80+
/**
81+
* Convert this Predicate to a {@link java.util.function.Supplier}
82+
*/
83+
default BooleanSupplier toBooleanSupplier() {
84+
return this;
85+
}
86+
87+
/**
88+
* Convert to this Predicate from a {@link java.util.function.Supplier}
89+
*/
90+
static Predicate0 from(BooleanSupplier supplier) {
91+
return supplier::getAsBoolean;
92+
}
93+
94+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright (c) Data Geekery GmbH, contact@datageekery.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.jooq.lambda.function;
17+
18+
import org.jooq.lambda.tuple.Tuple1;
19+
20+
import java.util.function.Function;
21+
import java.util.function.Predicate;
22+
23+
/**
24+
* A Predicate with 1 arguments.
25+
*
26+
* @author Lukas Eder, Andrej Fink
27+
*/
28+
@FunctionalInterface
29+
public interface Predicate1<T1> extends Predicate<T1>, GenericPredicate {
30+
31+
/**
32+
* Apply this Predicate to the arguments.
33+
*
34+
* @param args The arguments as a tuple.
35+
*/
36+
default boolean test(Tuple1<? extends T1> args) {
37+
return test(args.v1);
38+
}
39+
40+
/**
41+
* Apply this Predicate to the arguments.
42+
*/
43+
@Override
44+
boolean test(T1 v1);
45+
46+
/**
47+
* Convert this Predicate to a {@link java.util.function.Predicate}.
48+
*/
49+
default Predicate<T1> toPredicate() {
50+
return this;
51+
}
52+
53+
/**
54+
* Convert to this Predicate from a {@link java.util.function.Predicate}.
55+
*/
56+
static <T1> Predicate1<T1> from(Predicate<? super T1> jufPredicate) {
57+
return jufPredicate::test;
58+
}
59+
60+
/**
61+
* Partially apply this Predicate to the arguments.
62+
*/
63+
default Predicate0 applyPartially(T1 v1) {
64+
return () -> test(v1);
65+
}
66+
67+
/**
68+
* Partially apply this Predicate to the arguments.
69+
*/
70+
default Predicate0 applyPartially(Tuple1<? extends T1> args) {
71+
return () -> test(args.v1);
72+
}
73+
74+
/**
75+
* Convert this Predicate to a {@link Function1}.
76+
*/
77+
default Function1<T1, Boolean> toFunction() {
78+
return this::test;
79+
}
80+
81+
/**
82+
* Convert to this Predicate from a {@link java.util.function.Function}.
83+
*/
84+
static <T1> Predicate1<T1> fromFunction (Function<? super T1, Boolean> function) {
85+
return function::apply;
86+
}
87+
}

0 commit comments

Comments
 (0)