ChainedClosure.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.io.Serializable;
  19. import java.util.Collection;
  20. import java.util.Objects;

  21. import org.apache.commons.collections4.Closure;

  22. /**
  23.  * Closure implementation that chains the specified closures together.
  24.  *
  25.  * @param <T> the type of the input to the operation.
  26.  * @since 3.0
  27.  */
  28. public class ChainedClosure<T> implements Closure<T>, Serializable {

  29.     /** Serial version UID */
  30.     private static final long serialVersionUID = -3520677225766901240L;

  31.     /**
  32.      * Factory method that performs validation and copies the parameter array.
  33.      *
  34.      * @param <E> the type that the closure acts on
  35.      * @param closures  the closures to chain, copied, no nulls
  36.      * @return the {@code chained} closure
  37.      * @throws NullPointerException if the closures array is null
  38.      * @throws NullPointerException if any closure in the array is null
  39.      */
  40.     public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) {
  41.         FunctorUtils.validate(closures);
  42.         if (closures.length == 0) {
  43.             return NOPClosure.<E>nopClosure();
  44.         }
  45.         return new ChainedClosure<>(closures);
  46.     }

  47.     /**
  48.      * Create a new Closure that calls each closure in turn, passing the
  49.      * result into the next closure. The ordering is that of the iterator()
  50.      * method on the collection.
  51.      *
  52.      * @param <E> the type that the closure acts on
  53.      * @param closures  a collection of closures to chain
  54.      * @return the {@code chained} closure
  55.      * @throws NullPointerException if the closures collection is null
  56.      * @throws NullPointerException if any closure in the collection is null
  57.      */
  58.     @SuppressWarnings("unchecked")
  59.     public static <E> Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures) {
  60.         Objects.requireNonNull(closures, "closures");
  61.         if (closures.isEmpty()) {
  62.             return NOPClosure.<E>nopClosure();
  63.         }
  64.         // convert to array like this to guarantee iterator() ordering
  65.         final Closure<? super E>[] cmds = new Closure[closures.size()];
  66.         int i = 0;
  67.         for (final Closure<? super E> closure : closures) {
  68.             cmds[i++] = closure;
  69.         }
  70.         FunctorUtils.validate(cmds);
  71.         return new ChainedClosure<>(false, cmds);
  72.     }

  73.     /** The closures to call in turn */
  74.     private final Closure<? super T>[] iClosures;

  75.     /**
  76.      * Hidden constructor for the use by the static factory methods.
  77.      *
  78.      * @param clone  if {@code true} the input argument will be cloned
  79.      * @param closures  the closures to chain, no nulls
  80.      */
  81.     private ChainedClosure(final boolean clone, final Closure<? super T>... closures) {
  82.         iClosures = clone ? FunctorUtils.copy(closures) : closures;
  83.     }

  84.     /**
  85.      * Constructor that performs no validation.
  86.      * Use {@code chainedClosure} if you want that.
  87.      *
  88.      * @param closures  the closures to chain, copied, no nulls
  89.      */
  90.     public ChainedClosure(final Closure<? super T>... closures) {
  91.         this(true, closures);
  92.     }

  93.     /**
  94.      * Execute a list of closures.
  95.      *
  96.      * @param input  the input object passed to each closure
  97.      */
  98.     @Override
  99.     public void execute(final T input) {
  100.         for (final Closure<? super T> iClosure : iClosures) {
  101.             iClosure.accept(input);
  102.         }
  103.     }

  104.     /**
  105.      * Gets the closures.
  106.      *
  107.      * @return a copy of the closures
  108.      * @since 3.1
  109.      */
  110.     public Closure<? super T>[] getClosures() {
  111.         return FunctorUtils.copy(iClosures);
  112.     }

  113. }