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