View Javadoc
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  
22  import org.apache.commons.collections4.Closure;
23  import org.apache.commons.collections4.Predicate;
24  import org.apache.commons.collections4.Transformer;
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 Closure<? super T> to Closure<T>.
35       * <p>This method exists
36       * simply as centralized documentation and atomic unchecked warning
37       * suppression.
38       *
39       * @param <T> the type of object the returned closure should "accept"
40       * @param closure the closure to coerce.
41       * @return the coerced closure.
42       */
43      @SuppressWarnings("unchecked")
44      static <T> Closure<T> coerce(final Closure<? super T> closure) {
45          return (Closure<T>) closure;
46      }
47  
48      /**
49       * A very simple method that coerces Predicate<? super T> to Predicate<T>.
50       * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
51       * able to be coerced to Predicate<T> without casting issues.
52       * <p>This method exists
53       * simply as centralised documentation and atomic unchecked warning
54       * suppression.
55       *
56       * @param <T> the type of object the returned predicate should "accept"
57       * @param predicate the predicate to coerce.
58       * @return the coerced predicate.
59       */
60      @SuppressWarnings("unchecked")
61      static <T> Predicate<T> coerce(final Predicate<? super T> predicate) {
62          return (Predicate<T>) predicate;
63      }
64  
65      /**
66       * A very simple method that coerces Transformer<? super I, ? extends O> to Transformer<I, O>.
67       * <p>This method exists
68       * simply as centralised documentation and atomic unchecked warning
69       * suppression.
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 <I, O> Transformer<I, O> coerce(final Transformer<? super I, ? extends O> transformer) {
78          return (Transformer<I, O>) transformer;
79      }
80  
81      /**
82       * Clone the closures to ensure that the internal reference can't be messed with.
83       *
84       * @param closures  the closures to copy
85       * @return the cloned closures
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       * Clone the predicates to ensure that the internal reference can't be messed with.
97       * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
98       * able to be coerced to Predicate<T> without casting issues.
99       *
100      * @param predicates  the predicates to copy
101      * @return the cloned predicates
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      * Copy method
113      *
114      * @param transformers  the transformers to copy
115      * @return a clone of the transformers
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      * Validate the closures to ensure that all is well.
127      *
128      * @param closures  the closures to validate
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      * Validate the predicates to ensure that all is well.
141      *
142      * @param predicates  the predicates to validate
143      * @return predicate array
144      */
145     static <T> Predicate<? super T>[] validate(final Collection<? extends Predicate<? super T>> predicates) {
146         Objects.requireNonNull(predicates, "predicates");
147         // convert to array like this to guarantee iterator() ordering
148         @SuppressWarnings("unchecked") // OK
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      * Validate the predicates to ensure that all is well.
163      *
164      * @param predicates  the predicates to validate
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      * Validate method
177      *
178      * @param transformers  the transformers to validate
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      * Restricted constructor.
191      */
192     private FunctorUtils() {
193     }
194 
195 }