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 * https://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 * Clones the given array or returns null on null input.
35 *
36 * @param <T> The array type.
37 * @param array The array to clone.
38 * @return The cloned array or null.
39 */
40 private static <T> T[] clone(final T... array) {
41 return array != null ? array.clone() : null;
42 }
43
44 /**
45 * A very simple method that coerces Predicate<? super T> to Predicate<T>.
46 * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
47 * able to be coerced to Predicate<T> without casting issues.
48 * <p>
49 * This method exists
50 * simply as centralized documentation and atomic unchecked warning
51 * suppression.
52 * </p>
53 *
54 * @param <T> The type of object the returned predicate should "accept"
55 * @param predicate The predicate to coerce.
56 * @return The coerced predicate.
57 */
58 @SuppressWarnings("unchecked")
59 static <R extends java.util.function.Predicate<T>, P extends java.util.function.Predicate<? super T>, T> R coerce(final P predicate) {
60 return (R) predicate;
61 }
62
63 /**
64 * A very simple method that coerces {@code Transformer<? super I, ? extends O>} to {@code Transformer<I, O>}.
65 * <p>
66 * This method exists
67 * simply as centralized documentation and atomic unchecked warning
68 * suppression.
69 * </p>
70 *
71 * @param <I> The type of object the returned transformer should "accept"
72 * @param <O> The type of object the returned transformer should "produce"
73 * @param transformer The transformer to coerce.
74 * @return The coerced transformer.
75 */
76 @SuppressWarnings("unchecked")
77 static <R extends Function<I, O>, P extends Function<? super I, ? extends O>, I, O> R coerce(final P transformer) {
78 return (R) transformer;
79 }
80
81 /**
82 * Clones the consumers to ensure that the internal references can't be updated.
83 *
84 * @param consumers The consumers to copy.
85 * @return The cloned consumers.
86 */
87 @SuppressWarnings("unchecked")
88 static <T extends Consumer<?>> T[] copy(final T... consumers) {
89 return clone(consumers);
90 }
91
92 /**
93 * Clone the predicates to ensure that the internal reference can't be messed with.
94 * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
95 * able to be coerced to Predicate<T> without casting issues.
96 *
97 * @param predicates The predicates to copy
98 * @return The cloned predicates
99 */
100 @SuppressWarnings("unchecked")
101 static <T extends java.util.function.Predicate<?>> T[] copy(final T... predicates) {
102 return clone(predicates);
103 }
104
105 /**
106 * Copy method.
107 *
108 * @param transformers The transformers to copy
109 * @return A clone of the transformers
110 */
111 @SuppressWarnings("unchecked")
112 static <T extends Function<?, ?>> T[] copy(final T... transformers) {
113 return clone(transformers);
114 }
115
116 /**
117 * Validate the predicates to ensure that all is well.
118 *
119 * @param predicates The predicates to validate
120 * @return predicate array
121 */
122 static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) {
123 Objects.requireNonNull(predicates, "predicates");
124 // convert to array like this to guarantee iterator() ordering
125 @SuppressWarnings("unchecked") // OK
126 final Predicate<? super T>[] preds = new Predicate[predicates.size()];
127 int i = 0;
128 for (final java.util.function.Predicate<? super T> predicate : predicates) {
129 preds[i] = (Predicate<? super T>) predicate;
130 if (preds[i] == null) {
131 throw new NullPointerException("predicates[" + i + "]");
132 }
133 i++;
134 }
135 return preds;
136 }
137
138 /**
139 * Validates the consumers to ensure that all is well.
140 *
141 * @param consumers The consumers to validate.
142 */
143 static void validate(final Consumer<?>... consumers) {
144 Objects.requireNonNull(consumers, "consumers");
145 for (int i = 0; i < consumers.length; i++) {
146 if (consumers[i] == null) {
147 throw new NullPointerException("closures[" + i + "]");
148 }
149 }
150 }
151
152 /**
153 * Validate method
154 *
155 * @param functions The transformers to validate
156 */
157 static void validate(final Function<?, ?>... functions) {
158 Objects.requireNonNull(functions, "functions");
159 for (int i = 0; i < functions.length; i++) {
160 if (functions[i] == null) {
161 throw new NullPointerException("functions[" + i + "]");
162 }
163 }
164 }
165
166 /**
167 * Validate the predicates to ensure that all is well.
168 *
169 * @param predicates The predicates to validate
170 */
171 static void validate(final java.util.function.Predicate<?>... predicates) {
172 Objects.requireNonNull(predicates, "predicates");
173 for (int i = 0; i < predicates.length; i++) {
174 if (predicates[i] == null) {
175 throw new NullPointerException("predicates[" + i + "]");
176 }
177 }
178 }
179
180 /**
181 * Restricted constructor.
182 */
183 private FunctorUtils() {
184 }
185
186 }