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.collections.functors;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.collections.Closure;
22  import org.apache.commons.collections.Predicate;
23  import org.apache.commons.collections.Transformer;
24  
25  /**
26   * Internal utilities for functors.
27   *
28   * @since 3.0
29   * @version $Id: FunctorUtils.java 1435965 2013-01-20 21:13:18Z tn $
30   */
31  class FunctorUtils {
32  
33      /**
34       * Restricted constructor.
35       */
36      private FunctorUtils() {
37          super();
38      }
39  
40      /**
41       * Clone the predicates to ensure that the internal reference can't be messed with.
42       * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
43       * able to be coerced to Predicate<T> without casting issues.
44       *
45       * @param predicates  the predicates to copy
46       * @return the cloned predicates
47       */
48      @SuppressWarnings("unchecked")
49      static <T> Predicate<T>[] copy(final Predicate<? super T>[] predicates) {
50          if (predicates == null) {
51              return null;
52          }
53          return (Predicate<T>[]) predicates.clone();
54      }
55  
56      /**
57       * A very simple method that coerces Predicate<? super T> to Predicate<T>.
58       * Due to the {@link Predicate#evaluate(T)} method, Predicate<? super T> is
59       * able to be coerced to Predicate<T> without casting issues.
60       * <p>This method exists
61       * simply as centralised documentation and atomic unchecked warning
62       * suppression.
63       *
64       * @param <T> the type of object the returned predicate should "accept"
65       * @param predicate the predicate to coerce.
66       * @return the coerced predicate.
67       */
68      @SuppressWarnings("unchecked")
69      static <T> Predicate<T> coerce(final Predicate<? super T> predicate){
70          return (Predicate<T>) predicate;
71      }
72  
73      /**
74       * Validate the predicates to ensure that all is well.
75       *
76       * @param predicates  the predicates to validate
77       */
78      static void validate(final Predicate<?>[] predicates) {
79          if (predicates == null) {
80              throw new IllegalArgumentException("The predicate array must not be null");
81          }
82          for (int i = 0; i < predicates.length; i++) {
83              if (predicates[i] == null) {
84                  throw new IllegalArgumentException(
85                          "The predicate array must not contain a null predicate, index " + i + " was null");
86              }
87          }
88      }
89  
90      /**
91       * Validate the predicates to ensure that all is well.
92       *
93       * @param predicates  the predicates to validate
94       * @return predicate array
95       */
96      static <T> Predicate<T>[] validate(final Collection<? extends Predicate<T>> predicates) {
97          if (predicates == null) {
98              throw new IllegalArgumentException("The predicate collection must not be null");
99          }
100         // convert to array like this to guarantee iterator() ordering
101         @SuppressWarnings("unchecked") // OK
102         final
103         Predicate<T>[] preds = new Predicate[predicates.size()];
104         int i = 0;
105         for (final Predicate<T> predicate : predicates) {
106             preds[i] = predicate;
107             if (preds[i] == null) {
108                 throw new IllegalArgumentException(
109                         "The predicate collection must not contain a null predicate, index " + i + " was null");
110             }
111             i++;
112         }
113         return preds;
114     }
115 
116     /**
117      * Clone the closures to ensure that the internal reference can't be messed with.
118      *
119      * @param closures  the closures to copy
120      * @return the cloned closures
121      */
122     @SuppressWarnings("unchecked")
123     static <E> Closure<E>[] copy(final Closure<? super E>[] closures) {
124         if (closures == null) {
125             return null;
126         }
127         return (Closure<E>[]) closures.clone();
128     }
129 
130     /**
131      * Validate the closures to ensure that all is well.
132      *
133      * @param closures  the closures to validate
134      */
135     static void validate(final Closure<?>[] closures) {
136         if (closures == null) {
137             throw new IllegalArgumentException("The closure array must not be null");
138         }
139         for (int i = 0; i < closures.length; i++) {
140             if (closures[i] == null) {
141                 throw new IllegalArgumentException(
142                         "The closure array must not contain a null closure, index " + i + " was null");
143             }
144         }
145     }
146 
147     /**
148      * A very simple method that coerces Closure<? super T> to Closure<T>.
149      * <p>This method exists
150      * simply as centralised documentation and atomic unchecked warning
151      * suppression.
152      *
153      * @param <T> the type of object the returned closure should "accept"
154      * @param closure the closure to coerce.
155      * @return the coerced closure.
156      */
157     @SuppressWarnings("unchecked")
158     static <T> Closure<T> coerce(final Closure<? super T> closure){
159         return (Closure<T>) closure;
160     }
161 
162     /**
163      * Copy method
164      *
165      * @param transformers  the transformers to copy
166      * @return a clone of the transformers
167      */
168     @SuppressWarnings("unchecked")
169     static <I, O> Transformer<I, O>[] copy(final Transformer<? super I, ? extends O>[] transformers) {
170         if (transformers == null) {
171             return null;
172         }
173         return (Transformer<I, O>[]) transformers.clone();
174     }
175 
176     /**
177      * Validate method
178      *
179      * @param transformers  the transformers to validate
180      */
181     static void validate(final Transformer<?, ?>[] transformers) {
182         if (transformers == null) {
183             throw new IllegalArgumentException("The transformer array must not be null");
184         }
185         for (int i = 0; i < transformers.length; i++) {
186             if (transformers[i] == null) {
187                 throw new IllegalArgumentException(
188                     "The transformer array must not contain a null transformer, index " + i + " was null");
189             }
190         }
191     }
192 
193     /**
194      * A very simple method that coerces Transformer<? super I, ? extends O> to Transformer<I, O>.
195      * <p>This method exists
196      * simply as centralised documentation and atomic unchecked warning
197      * suppression.
198      *
199      * @param <T> the type of object the returned transformer should "accept"
200      * @param transformer the transformer to coerce.
201      * @return the coerced transformer.
202      */
203     @SuppressWarnings("unchecked")
204     static <I, O> Transformer<I, O> coerce(final Transformer<? super I, ? extends O> transformer) {
205         return (Transformer<I, O>) transformer;
206     }
207 
208 }