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