KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.commons.collections.Predicate;
22
23 /**
24  * Closure implementation that executes a closure repeatedly until a condition is met,
25  * like a do-while or while loop.
26  *
27  * @since Commons Collections 3.0
28  * @version $Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
29  *
30  * @author Stephen Colebourne
31  */

32 public class WhileClosure implements Closure, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = -3110538116913760108L;
36
37     /** The test condition */
38     private final Predicate iPredicate;
39     /** The closure to call */
40     private final Closure iClosure;
41     /** The flag, true is a do loop, false is a while */
42     private final boolean iDoLoop;
43
44     /**
45      * Factory method that performs validation.
46      *
47      * @param predicate the predicate used to evaluate when the loop terminates, not null
48      * @param closure the closure the execute, not null
49      * @param doLoop true to act as a do-while loop, always executing the closure once
50      * @return the <code>while</code> closure
51      * @throws IllegalArgumentException if the predicate or closure is null
52      */

53     public static Closure getInstance(Predicate predicate, Closure closure, boolean doLoop) {
54         if (predicate == null) {
55             throw new IllegalArgumentException JavaDoc("Predicate must not be null");
56         }
57         if (closure == null) {
58             throw new IllegalArgumentException JavaDoc("Closure must not be null");
59         }
60         return new WhileClosure(predicate, closure, doLoop);
61     }
62
63     /**
64      * Constructor that performs no validation.
65      * Use <code>getInstance</code> if you want that.
66      *
67      * @param predicate the predicate used to evaluate when the loop terminates, not null
68      * @param closure the closure the execute, not null
69      * @param doLoop true to act as a do-while loop, always executing the closure once
70      */

71     public WhileClosure(Predicate predicate, Closure closure, boolean doLoop) {
72         super();
73         iPredicate = predicate;
74         iClosure = closure;
75         iDoLoop = doLoop;
76     }
77
78     /**
79      * Executes the closure until the predicate is false.
80      *
81      * @param input the input object
82      */

83     public void execute(Object JavaDoc input) {
84         if (iDoLoop) {
85             iClosure.execute(input);
86         }
87         while (iPredicate.evaluate(input)) {
88             iClosure.execute(input);
89         }
90     }
91
92     /**
93      * Gets the predicate in use.
94      *
95      * @return the predicate
96      * @since Commons Collections 3.1
97      */

98     public Predicate getPredicate() {
99         return iPredicate;
100     }
101
102     /**
103      * Gets the closure.
104      *
105      * @return the closure
106      * @since Commons Collections 3.1
107      */

108     public Closure getClosure() {
109         return iClosure;
110     }
111
112     /**
113      * Is the loop a do-while loop.
114      *
115      * @return true is do-while, false if while
116      * @since Commons Collections 3.1
117      */

118     public boolean isDoLoop() {
119         return iDoLoop;
120     }
121
122 }
123
Popular Tags