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