KickJava   Java API By Example, From Geeks To Geeks.

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


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 acts as an if statement calling one or other closure
25  * based on a predicate.
26  *
27  * @since Commons Collections 3.0
28  * @version $Revision: 1.6 $ $Date: 2004/05/16 11:47:38 $
29  *
30  * @author Stephen Colebourne
31  */

32 public class IfClosure implements Closure, Serializable JavaDoc {
33
34     /** Serial version UID */
35     static final long serialVersionUID = 3518477308466486130L;
36
37     /** The test */
38     private final Predicate iPredicate;
39     /** The closure to use if true */
40     private final Closure iTrueClosure;
41     /** The closure to use if false */
42     private final Closure iFalseClosure;
43
44     /**
45      * Factory method that performs validation.
46      *
47      * @param predicate predicate to switch on
48      * @param trueClosure closure used if true
49      * @param falseClosure closure used if false
50      * @return the <code>if</code> closure
51      * @throws IllegalArgumentException if any argument is null
52      */

53     public static Closure getInstance(Predicate predicate, Closure trueClosure, Closure falseClosure) {
54         if (predicate == null) {
55             throw new IllegalArgumentException JavaDoc("Predicate must not be null");
56         }
57         if (trueClosure == null || falseClosure == null) {
58             throw new IllegalArgumentException JavaDoc("Closures must not be null");
59         }
60         return new IfClosure(predicate, trueClosure, falseClosure);
61     }
62
63     /**
64      * Constructor that performs no validation.
65      * Use <code>getInstance</code> if you want that.
66      *
67      * @param predicate predicate to switch on, not null
68      * @param trueClosure closure used if true, not null
69      * @param falseClosure closure used if false, not null
70      */

71     public IfClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) {
72         super();
73         iPredicate = predicate;
74         iTrueClosure = trueClosure;
75         iFalseClosure = falseClosure;
76     }
77
78     /**
79      * Executes the true or false closure accoring to the result of the predicate.
80      *
81      * @param input the input object
82      */

83     public void execute(Object JavaDoc input) {
84         if (iPredicate.evaluate(input) == true) {
85             iTrueClosure.execute(input);
86         } else {
87             iFalseClosure.execute(input);
88         }
89     }
90
91     /**
92      * Gets the predicate.
93      *
94      * @return the predicate
95      * @since Commons Collections 3.1
96      */

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

107     public Closure getTrueClosure() {
108         return iTrueClosure;
109     }
110
111     /**
112      * Gets the closure called when false.
113      *
114      * @return the closure
115      * @since Commons Collections 3.1
116      */

117     public Closure getFalseClosure() {
118         return iFalseClosure;
119     }
120
121 }
122
Popular Tags