FunctorUtils.java

  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. import java.util.Collection;
  19. import java.util.Objects;
  20. import java.util.function.Consumer;
  21. import java.util.function.Function;

  22. import org.apache.commons.collections4.Predicate;

  23. /**
  24.  * Internal utilities for functors.
  25.  *
  26.  * @since 3.0
  27.  */
  28. final class FunctorUtils {

  29.     /**
  30.      * A very simple method that coerces Predicate<? super T> to Predicate<T>.
  31.      * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
  32.      * able to be coerced to Predicate<T> without casting issues.
  33.      * <p>
  34.      * This method exists
  35.      * simply as centralized documentation and atomic unchecked warning
  36.      * suppression.
  37.      * </p>
  38.      *
  39.      * @param <T> the type of object the returned predicate should "accept"
  40.      * @param predicate the predicate to coerce.
  41.      * @return the coerced predicate.
  42.      */
  43.     @SuppressWarnings("unchecked")
  44.     static <R extends java.util.function.Predicate<T>, P extends java.util.function.Predicate<? super T>, T> R coerce(final P predicate) {
  45.         return (R) predicate;
  46.     }

  47.     /**
  48.      * A very simple method that coerces Transformer<? super I, ? extends O> to Transformer<I, O>.
  49.      * <p>
  50.      * This method exists
  51.      * simply as centralized documentation and atomic unchecked warning
  52.      * suppression.
  53.      * </p>
  54.      *
  55.      * @param <I> the type of object the returned transformer should "accept"
  56.      * @param <O> the type of object the returned transformer should "produce"
  57.      * @param transformer the transformer to coerce.
  58.      * @return the coerced transformer.
  59.      */
  60.     @SuppressWarnings("unchecked")
  61.     static <R extends Function<I, O>, P extends Function<? super I, ? extends O>, I, O> R coerce(final P transformer) {
  62.         return (R) transformer;
  63.     }

  64.     /**
  65.      * Clones the consumers to ensure that the internal references can't be updated.
  66.      *
  67.      * @param consumers  the consumers to copy.
  68.      * @return the cloned consumers.
  69.      */
  70.     @SuppressWarnings("unchecked")
  71.     static <T extends Consumer<?>> T[] copy(final T... consumers) {
  72.         if (consumers == null) {
  73.             return null;
  74.         }
  75.         return consumers.clone();
  76.     }

  77.     /**
  78.      * Clone the predicates to ensure that the internal reference can't be messed with.
  79.      * Due to the {@link Predicate#test(T)} method, Predicate<? super T> is
  80.      * able to be coerced to Predicate<T> without casting issues.
  81.      *
  82.      * @param predicates  the predicates to copy
  83.      * @return the cloned predicates
  84.      */
  85.     @SuppressWarnings("unchecked")
  86.     static <T extends java.util.function.Predicate<?>> T[] copy(final T... predicates) {
  87.         if (predicates == null) {
  88.             return null;
  89.         }
  90.         return predicates.clone();
  91.     }

  92.     /**
  93.      * Copy method.
  94.      *
  95.      * @param transformers  the transformers to copy
  96.      * @return a clone of the transformers
  97.      */
  98.     @SuppressWarnings("unchecked")
  99.     static <T extends Function<?, ?>> T[] copy(final T... transformers) {
  100.         if (transformers == null) {
  101.             return null;
  102.         }
  103.         return transformers.clone();
  104.     }

  105.     /**
  106.      * Validate the predicates to ensure that all is well.
  107.      *
  108.      * @param predicates  the predicates to validate
  109.      * @return predicate array
  110.      */
  111.     static <T> Predicate<? super T>[] validate(final Collection<? extends java.util.function.Predicate<? super T>> predicates) {
  112.         Objects.requireNonNull(predicates, "predicates");
  113.         // convert to array like this to guarantee iterator() ordering
  114.         @SuppressWarnings("unchecked") // OK
  115.         final Predicate<? super T>[] preds = new Predicate[predicates.size()];
  116.         int i = 0;
  117.         for (final java.util.function.Predicate<? super T> predicate : predicates) {
  118.             preds[i] = (Predicate<? super T>) predicate;
  119.             if (preds[i] == null) {
  120.                 throw new NullPointerException("predicates[" + i + "]");
  121.             }
  122.             i++;
  123.         }
  124.         return preds;
  125.     }

  126.     /**
  127.      * Validates the consumers to ensure that all is well.
  128.      *
  129.      * @param consumers  the consumers to validate.
  130.      */
  131.     static void validate(final Consumer<?>... consumers) {
  132.         Objects.requireNonNull(consumers, "closures");
  133.         for (int i = 0; i < consumers.length; i++) {
  134.             if (consumers[i] == null) {
  135.                 throw new NullPointerException("closures[" + i + "]");
  136.             }
  137.         }
  138.     }

  139.     /**
  140.      * Validate method
  141.      *
  142.      * @param functions  the transformers to validate
  143.      */
  144.     static void validate(final Function<?, ?>... functions) {
  145.         Objects.requireNonNull(functions, "functions");
  146.         for (int i = 0; i < functions.length; i++) {
  147.             if (functions[i] == null) {
  148.                 throw new NullPointerException("functions[" + i + "]");
  149.             }
  150.         }
  151.     }

  152.     /**
  153.      * Validate the predicates to ensure that all is well.
  154.      *
  155.      * @param predicates  the predicates to validate
  156.      */
  157.     static void validate(final java.util.function.Predicate<?>... predicates) {
  158.         Objects.requireNonNull(predicates, "predicates");
  159.         for (int i = 0; i < predicates.length; i++) {
  160.             if (predicates[i] == null) {
  161.                 throw new NullPointerException("predicates[" + i + "]");
  162.             }
  163.         }
  164.     }

  165.     /**
  166.      * Restricted constructor.
  167.      */
  168.     private FunctorUtils() {
  169.     }

  170. }