001: /*
002: * Copyright 2001-2004 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:
022: /**
023: * Closure implementation that calls another closure n times, like a for loop.
024: *
025: * @since Commons Collections 3.0
026: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
027: *
028: * @author Stephen Colebourne
029: */
030: public class ForClosure implements Closure, Serializable {
031:
032: /** Serial version UID */
033: private static final long serialVersionUID = -1190120533393621674L;
034:
035: /** The number of times to loop */
036: private final int iCount;
037: /** The closure to call */
038: private final Closure iClosure;
039:
040: /**
041: * Factory method that performs validation.
042: * <p>
043: * A null closure or zero count returns the <code>NOPClosure</code>.
044: * A count of one returns the specified closure.
045: *
046: * @param count the number of times to execute the closure
047: * @param closure the closure to execute, not null
048: * @return the <code>for</code> closure
049: */
050: public static Closure getInstance(int count, Closure closure) {
051: if (count <= 0 || closure == null) {
052: return NOPClosure.INSTANCE;
053: }
054: if (count == 1) {
055: return closure;
056: }
057: return new ForClosure(count, closure);
058: }
059:
060: /**
061: * Constructor that performs no validation.
062: * Use <code>getInstance</code> if you want that.
063: *
064: * @param count the number of times to execute the closure
065: * @param closure the closure to execute, not null
066: */
067: public ForClosure(int count, Closure closure) {
068: super ();
069: iCount = count;
070: iClosure = closure;
071: }
072:
073: /**
074: * Executes the closure <code>count</code> times.
075: *
076: * @param input the input object
077: */
078: public void execute(Object input) {
079: for (int i = 0; i < iCount; i++) {
080: iClosure.execute(input);
081: }
082: }
083:
084: /**
085: * Gets the closure.
086: *
087: * @return the closure
088: * @since Commons Collections 3.1
089: */
090: public Closure getClosure() {
091: return iClosure;
092: }
093:
094: /**
095: * Gets the count.
096: *
097: * @return the count
098: * @since Commons Collections 3.1
099: */
100: public int getCount() {
101: return iCount;
102: }
103:
104: }
|