1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.functors;
18
19 import java.util.Collection;
20 import java.util.Objects;
21 import java.util.function.Consumer;
22 import java.util.function.Function;
23
24 import org.apache.commons.collections4.Predicate;
25
26
27
28
29
30
31 final class FunctorUtils {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @SuppressWarnings("unchecked")
48 static <R extends java.util.function.Predicate<T>, P extends java.util.function.Predicate<? super T>, T> R coerce(final P predicate) {
49 return (R) predicate;
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @SuppressWarnings("unchecked")
66 static <R extends Function<I, O>, P extends Function<? super I, ? extends O>, I, O> R coerce(final P transformer) {
67 return (R) transformer;
68 }
69
70
71
72
73
74
75
76 @SuppressWarnings("unchecked")
77 static <T extends Consumer<?>> T[] copy(final T... consumers) {
78 if (consumers == null) {
79 return null;
80 }
81 return consumers.clone();
82 }
83
84
85
86
87
88
89
90
91
92 @SuppressWarnings("unchecked")
93 static <T extends java.util.function.Predicate<?>> T[] copy(final T... predicates) {
94 if (predicates == null) {
95 return null;
96 }
97 return predicates.clone();
98 }
99
100
101
102
103
104
105
106 @SuppressWarnings("unchecked")
107 static <T extends Function<?, ?>> T[] copy(final T... transformers) {
108 if (transformers == null) {
109 return null;
110 }
111 return transformers.clone();
112 }
113
114
115
116
117
118
119
120 static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) {
121 Objects.requireNonNull(predicates, "predicates");
122
123 @SuppressWarnings("unchecked")
124 final Predicate<? super T>[] preds = new Predicate[predicates.size()];
125 int i = 0;
126 for (final java.util.function.Predicate<? super T> predicate : predicates) {
127 preds[i] = (Predicate<? super T>) predicate;
128 if (preds[i] == null) {
129 throw new NullPointerException("predicates[" + i + "]");
130 }
131 i++;
132 }
133 return preds;
134 }
135
136
137
138
139
140
141 static void validate(final Consumer<?>... consumers) {
142 Objects.requireNonNull(consumers, "closures");
143 for (int i = 0; i < consumers.length; i++) {
144 if (consumers[i] == null) {
145 throw new NullPointerException("closures[" + i + "]");
146 }
147 }
148 }
149
150
151
152
153
154
155 static void validate(final Function<?, ?>... functions) {
156 Objects.requireNonNull(functions, "functions");
157 for (int i = 0; i < functions.length; i++) {
158 if (functions[i] == null) {
159 throw new NullPointerException("functions[" + i + "]");
160 }
161 }
162 }
163
164
165
166
167
168
169 static void validate(final java.util.function.Predicate<?>... predicates) {
170 Objects.requireNonNull(predicates, "predicates");
171 for (int i = 0; i < predicates.length; i++) {
172 if (predicates[i] == null) {
173 throw new NullPointerException("predicates[" + i + "]");
174 }
175 }
176 }
177
178
179
180
181 private FunctorUtils() {
182 }
183
184 }