001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.functors;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.collections.Closure;
021: import org.apache.commons.collections.Predicate;
022:
023: /**
024: * Closure implementation acts as an if statement calling one or other closure
025: * based on a predicate.
026: *
027: * @since Commons Collections 3.0
028: * @version $Revision: 375766 $ $Date: 2006-02-07 23:10:36 +0000 (Tue, 07 Feb 2006) $
029: *
030: * @author Stephen Colebourne
031: * @author Matt Benson
032: */
033: public class IfClosure implements Closure, Serializable {
034:
035: /** Serial version UID */
036: private static final long serialVersionUID = 3518477308466486130L;
037:
038: /** The test */
039: private final Predicate iPredicate;
040: /** The closure to use if true */
041: private final Closure iTrueClosure;
042: /** The closure to use if false */
043: private final Closure iFalseClosure;
044:
045: /**
046: * Factory method that performs validation.
047: * <p>
048: * This factory creates a closure that performs no action when
049: * the predicate is false.
050: *
051: * @param predicate predicate to switch on
052: * @param trueClosure closure used if true
053: * @return the <code>if</code> closure
054: * @throws IllegalArgumentException if either argument is null
055: * @since Commons Collections 3.2
056: */
057: public static Closure getInstance(Predicate predicate,
058: Closure trueClosure) {
059: return getInstance(predicate, trueClosure, NOPClosure.INSTANCE);
060: }
061:
062: /**
063: * Factory method that performs validation.
064: *
065: * @param predicate predicate to switch on
066: * @param trueClosure closure used if true
067: * @param falseClosure closure used if false
068: * @return the <code>if</code> closure
069: * @throws IllegalArgumentException if any argument is null
070: */
071: public static Closure getInstance(Predicate predicate,
072: Closure trueClosure, Closure falseClosure) {
073: if (predicate == null) {
074: throw new IllegalArgumentException(
075: "Predicate must not be null");
076: }
077: if (trueClosure == null || falseClosure == null) {
078: throw new IllegalArgumentException(
079: "Closures must not be null");
080: }
081: return new IfClosure(predicate, trueClosure, falseClosure);
082: }
083:
084: /**
085: * Constructor that performs no validation.
086: * Use <code>getInstance</code> if you want that.
087: * <p>
088: * This constructor creates a closure that performs no action when
089: * the predicate is false.
090: *
091: * @param predicate predicate to switch on, not null
092: * @param trueClosure closure used if true, not null
093: * @since Commons Collections 3.2
094: */
095: public IfClosure(Predicate predicate, Closure trueClosure) {
096: this (predicate, trueClosure, NOPClosure.INSTANCE);
097: }
098:
099: /**
100: * Constructor that performs no validation.
101: * Use <code>getInstance</code> if you want that.
102: *
103: * @param predicate predicate to switch on, not null
104: * @param trueClosure closure used if true, not null
105: * @param falseClosure closure used if false, not null
106: */
107: public IfClosure(Predicate predicate, Closure trueClosure,
108: Closure falseClosure) {
109: super ();
110: iPredicate = predicate;
111: iTrueClosure = trueClosure;
112: iFalseClosure = falseClosure;
113: }
114:
115: /**
116: * Executes the true or false closure accoring to the result of the predicate.
117: *
118: * @param input the input object
119: */
120: public void execute(Object input) {
121: if (iPredicate.evaluate(input) == true) {
122: iTrueClosure.execute(input);
123: } else {
124: iFalseClosure.execute(input);
125: }
126: }
127:
128: /**
129: * Gets the predicate.
130: *
131: * @return the predicate
132: * @since Commons Collections 3.1
133: */
134: public Predicate getPredicate() {
135: return iPredicate;
136: }
137:
138: /**
139: * Gets the closure called when true.
140: *
141: * @return the closure
142: * @since Commons Collections 3.1
143: */
144: public Closure getTrueClosure() {
145: return iTrueClosure;
146: }
147:
148: /**
149: * Gets the closure called when false.
150: *
151: * @return the closure
152: * @since Commons Collections 3.1
153: */
154: public Closure getFalseClosure() {
155: return iFalseClosure;
156: }
157:
158: }
|