KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > ChainedClosure


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.functors;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.commons.collections.Closure;
23
24 /**
25  * Closure implementation that chains the specified closures together.
26  *
27  * @since Commons Collections 3.0
28  * @version $Revision: 1.5 $ $Date: 2004/03/13 17:17:03 $
29  *
30  * @author Stephen Colebourne
31  */

32 public class ChainedClosure implements Closure, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = -3520677225766901240L;
36
37     /** The closures to call in turn */
38     private final Closure[] iClosures;
39
40     /**
41      * Factory method that performs validation and copies the parameter array.
42      *
43      * @param closures the closures to chain, copied, no nulls
44      * @return the <code>chained</code> closure
45      * @throws IllegalArgumentException if the closures array is null
46      * @throws IllegalArgumentException if any closure in the array is null
47      */

48     public static Closure getInstance(Closure[] closures) {
49         FunctorUtils.validate(closures);
50         if (closures.length == 0) {
51             return NOPClosure.INSTANCE;
52         }
53         closures = FunctorUtils.copy(closures);
54         return new ChainedClosure(closures);
55     }
56     
57     /**
58      * Create a new Closure that calls each closure in turn, passing the
59      * result into the next closure. The ordering is that of the iterator()
60      * method on the collection.
61      *
62      * @param closures a collection of closures to chain
63      * @return the <code>chained</code> closure
64      * @throws IllegalArgumentException if the closures collection is null
65      * @throws IllegalArgumentException if any closure in the collection is null
66      */

67     public static Closure getInstance(Collection JavaDoc closures) {
68         if (closures == null) {
69             throw new IllegalArgumentException JavaDoc("Closure collection must not be null");
70         }
71         if (closures.size() == 0) {
72             return NOPClosure.INSTANCE;
73         }
74         // convert to array like this to guarantee iterator() ordering
75
Closure[] cmds = new Closure[closures.size()];
76         int i = 0;
77         for (Iterator JavaDoc it = closures.iterator(); it.hasNext();) {
78             cmds[i++] = (Closure) it.next();
79         }
80         FunctorUtils.validate(cmds);
81         return new ChainedClosure(cmds);
82     }
83
84     /**
85      * Factory method that performs validation.
86      *
87      * @param closure1 the first closure, not null
88      * @param closure2 the second closure, not null
89      * @return the <code>chained</code> closure
90      * @throws IllegalArgumentException if either closure is null
91      */

92     public static Closure getInstance(Closure closure1, Closure closure2) {
93         if (closure1 == null || closure2 == null) {
94             throw new IllegalArgumentException JavaDoc("Closures must not be null");
95         }
96         Closure[] closures = new Closure[] { closure1, closure2 };
97         return new ChainedClosure(closures);
98     }
99
100     /**
101      * Constructor that performs no validation.
102      * Use <code>getInstance</code> if you want that.
103      *
104      * @param closures the closures to chain, not copied, no nulls
105      */

106     public ChainedClosure(Closure[] closures) {
107         super();
108         iClosures = closures;
109     }
110
111     /**
112      * Execute a list of closures.
113      *
114      * @param input the input object passed to each closure
115      */

116     public void execute(Object JavaDoc input) {
117         for (int i = 0; i < iClosures.length; i++) {
118             iClosures[i].execute(input);
119         }
120     }
121
122     /**
123      * Gets the closures, do not modify the array.
124      * @return the closures
125      * @since Commons Collections 3.1
126      */

127     public Closure[] getClosures() {
128         return iClosures;
129     }
130
131 }
132
Popular Tags