KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import org.apache.commons.collections.Closure;
21
22 /**
23  * Closure implementation that calls another closure n times, like a for loop.
24  *
25  * @since Commons Collections 3.0
26  * @version $Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
27  *
28  * @author Stephen Colebourne
29  */

30 public class ForClosure implements Closure, Serializable JavaDoc {
31
32     /** Serial version UID */
33     static final long serialVersionUID = -1190120533393621674L;
34
35     /** The number of times to loop */
36     private final int iCount;
37     /** The closure to call */
38     private final Closure iClosure;
39
40     /**
41      * Factory method that performs validation.
42      * <p>
43      * A null closure or zero count returns the <code>NOPClosure</code>.
44      * A count of one returns the specified closure.
45      *
46      * @param count the number of times to execute the closure
47      * @param closure the closure to execute, not null
48      * @return the <code>for</code> closure
49      */

50     public static Closure getInstance(int count, Closure closure) {
51         if (count <= 0 || closure == null) {
52             return NOPClosure.INSTANCE;
53         }
54         if (count == 1) {
55             return closure;
56         }
57         return new ForClosure(count, closure);
58     }
59
60     /**
61      * Constructor that performs no validation.
62      * Use <code>getInstance</code> if you want that.
63      *
64      * @param count the number of times to execute the closure
65      * @param closure the closure to execute, not null
66      */

67     public ForClosure(int count, Closure closure) {
68         super();
69         iCount = count;
70         iClosure = closure;
71     }
72
73     /**
74      * Executes the closure <code>count</code> times.
75      *
76      * @param input the input object
77      */

78     public void execute(Object JavaDoc input) {
79         for (int i = 0; i < iCount; i++) {
80             iClosure.execute(input);
81         }
82     }
83
84     /**
85      * Gets the closure.
86      *
87      * @return the closure
88      * @since Commons Collections 3.1
89      */

90     public Closure getClosure() {
91         return iClosure;
92     }
93
94     /**
95      * Gets the count.
96      *
97      * @return the count
98      * @since Commons Collections 3.1
99      */

100     public int getCount() {
101         return iCount;
102     }
103
104 }
105
Popular Tags