1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * Internal utilities for functors.
28 *
29 * @since 3.0
30 */
31 final class FunctorUtils {
32
33 /**
34 * A very simple method that coerces Predicate<? super T> to Predicate<T>.
35 * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
36 * able to be coerced to Predicate<T> without casting issues.
37 * <p>
38 * This method exists
39 * simply as centralized documentation and atomic unchecked warning
40 * suppression.
41 * </p>
42 *
43 * @param <T> the type of object the returned predicate should "accept"
44 * @param predicate the predicate to coerce.
45 * @return the coerced predicate.
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 * A very simple method that coerces Transformer<? super I, ? extends O> to Transformer<I, O>.
54 * <p>
55 * This method exists
56 * simply as centralized documentation and atomic unchecked warning
57 * suppression.
58 * </p>
59 *
60 * @param <I> the type of object the returned transformer should "accept"
61 * @param <O> the type of object the returned transformer should "produce"
62 * @param transformer the transformer to coerce.
63 * @return the coerced transformer.
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 * Clones the consumers to ensure that the internal references can't be updated.
72 *
73 * @param consumers the consumers to copy.
74 * @return the cloned consumers.
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 * Clone the predicates to ensure that the internal reference can't be messed with.
86 * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
87 * able to be coerced to Predicate<T> without casting issues.
88 *
89 * @param predicates the predicates to copy
90 * @return the cloned predicates
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 * Copy method.
102 *
103 * @param transformers the transformers to copy
104 * @return a clone of the transformers
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 * Validate the predicates to ensure that all is well.
116 *
117 * @param predicates the predicates to validate
118 * @return predicate array
119 */
120 static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) {
121 Objects.requireNonNull(predicates, "predicates");
122 // convert to array like this to guarantee iterator() ordering
123 @SuppressWarnings("unchecked") // OK
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 * Validates the consumers to ensure that all is well.
138 *
139 * @param consumers the consumers to validate.
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 * Validate method
152 *
153 * @param functions the transformers to validate
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 * Validate the predicates to ensure that all is well.
166 *
167 * @param predicates the predicates to validate
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 * Restricted constructor.
180 */
181 private FunctorUtils() {
182 }
183
184 }