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 @SuppressWarnings("unchecked")
46 static <R extends java.util.function.Predicate<T>, P extends java.util.function.Predicate<? super T>, T> R coerce(final P predicate) {
47 return (R) predicate;
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61 @SuppressWarnings("unchecked")
62 static <R extends Function<I, O>, P extends Function<? super I, ? extends O>, I, O> R coerce(final P transformer) {
63 return (R) transformer;
64 }
65
66
67
68
69
70
71
72 @SuppressWarnings("unchecked")
73 static <T extends Consumer<?>> T[] copy(final T... consumers) {
74 if (consumers == null) {
75 return null;
76 }
77 return consumers.clone();
78 }
79
80
81
82
83
84
85
86
87
88 @SuppressWarnings("unchecked")
89 static <T extends java.util.function.Predicate<?>> T[] copy(final T... predicates) {
90 if (predicates == null) {
91 return null;
92 }
93 return predicates.clone();
94 }
95
96
97
98
99
100
101
102 @SuppressWarnings("unchecked")
103 static <T extends Function<?, ?>> T[] copy(final T... transformers) {
104 if (transformers == null) {
105 return null;
106 }
107 return transformers.clone();
108 }
109
110
111
112
113
114
115
116 static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) {
117 Objects.requireNonNull(predicates, "predicates");
118
119 @SuppressWarnings("unchecked")
120 final Predicate<? super T>[] preds = new Predicate[predicates.size()];
121 int i = 0;
122 for (final java.util.function.Predicate<? super T> predicate : predicates) {
123 preds[i] = (Predicate<? super T>) predicate;
124 if (preds[i] == null) {
125 throw new NullPointerException("predicates[" + i + "]");
126 }
127 i++;
128 }
129 return preds;
130 }
131
132
133
134
135
136
137 static void validate(final Consumer<?>... consumers) {
138 Objects.requireNonNull(consumers, "closures");
139 for (int i = 0; i < consumers.length; i++) {
140 if (consumers[i] == null) {
141 throw new NullPointerException("closures[" + i + "]");
142 }
143 }
144 }
145
146
147
148
149
150
151 static void validate(final Function<?, ?>... functions) {
152 Objects.requireNonNull(functions, "functions");
153 for (int i = 0; i < functions.length; i++) {
154 if (functions[i] == null) {
155 throw new NullPointerException("functions[" + i + "]");
156 }
157 }
158 }
159
160
161
162
163
164
165 static void validate(final java.util.function.Predicate<?>... predicates) {
166 Objects.requireNonNull(predicates, "predicates");
167 for (int i = 0; i < predicates.length; i++) {
168 if (predicates[i] == null) {
169 throw new NullPointerException("predicates[" + i + "]");
170 }
171 }
172 }
173
174
175
176
177 private FunctorUtils() {
178 }
179
180 }