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.io.Serializable;
20  import java.util.Collection;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.Closure;
24  
25  /**
26   * Closure implementation that chains the specified closures together.
27   *
28   * @since 3.0
29   */
30  public class ChainedClosure<E> implements Closure<E>, Serializable {
31  
32      /** Serial version UID */
33      private static final long serialVersionUID = -3520677225766901240L;
34  
35      /**
36       * Factory method that performs validation and copies the parameter array.
37       *
38       * @param <E> the type that the closure acts on
39       * @param closures  the closures to chain, copied, no nulls
40       * @return the {@code chained} closure
41       * @throws NullPointerException if the closures array is null
42       * @throws NullPointerException if any closure in the array is null
43       */
44      public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) {
45          FunctorUtils.validate(closures);
46          if (closures.length == 0) {
47              return NOPClosure.<E>nopClosure();
48          }
49          return new ChainedClosure<>(closures);
50      }
51  
52      /**
53       * Create a new Closure that calls each closure in turn, passing the
54       * result into the next closure. The ordering is that of the iterator()
55       * method on the collection.
56       *
57       * @param <E> the type that the closure acts on
58       * @param closures  a collection of closures to chain
59       * @return the {@code chained} closure
60       * @throws NullPointerException if the closures collection is null
61       * @throws NullPointerException if any closure in the collection is null
62       */
63      @SuppressWarnings("unchecked")
64      public static <E> Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures) {
65          Objects.requireNonNull(closures, "closures");
66          if (closures.isEmpty()) {
67              return NOPClosure.<E>nopClosure();
68          }
69          // convert to array like this to guarantee iterator() ordering
70          final Closure<? super E>[] cmds = new Closure[closures.size()];
71          int i = 0;
72          for (final Closure<? super E> closure : closures) {
73              cmds[i++] = closure;
74          }
75          FunctorUtils.validate(cmds);
76          return new ChainedClosure<>(false, cmds);
77      }
78  
79      /** The closures to call in turn */
80      private final Closure<? super E>[] iClosures;
81  
82      /**
83       * Hidden constructor for the use by the static factory methods.
84       *
85       * @param clone  if {@code true} the input argument will be cloned
86       * @param closures  the closures to chain, no nulls
87       */
88      private ChainedClosure(final boolean clone, final Closure<? super E>... closures) {
89          iClosures = clone ? FunctorUtils.copy(closures) : closures;
90      }
91  
92      /**
93       * Constructor that performs no validation.
94       * Use {@code chainedClosure} if you want that.
95       *
96       * @param closures  the closures to chain, copied, no nulls
97       */
98      public ChainedClosure(final Closure<? super E>... closures) {
99          this(true, closures);
100     }
101 
102     /**
103      * Execute a list of closures.
104      *
105      * @param input  the input object passed to each closure
106      */
107     @Override
108     public void execute(final E input) {
109         for (final Closure<? super E> iClosure : iClosures) {
110             iClosure.execute(input);
111         }
112     }
113 
114     /**
115      * Gets the closures.
116      *
117      * @return a copy of the closures
118      * @since 3.1
119      */
120     public Closure<? super E>[] getClosures() {
121         return FunctorUtils.<E>copy(iClosures);
122     }
123 
124 }