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
+ }
0 commit comments