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