001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy 006 * 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, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations 014 * under the License. 015 */ 016 package org.springframework.rules.closure.support; 017 018 import java.util.Collection; 019 import java.util.Iterator; 020 021 /** 022 * Simple process template that iterates over elements. 023 * 024 * @author Keith Donald 025 */ 026 public class IteratorTemplate extends AbstractElementGeneratorWorkflow { 027 028 /** Collection of objects to iterate over. */ 029 private Collection collection; 030 031 /** Iterator on the collection. */ 032 private Iterator it; 033 034 /** 035 * Constructor. 036 * 037 * @param collection the elements to iterate over. 038 */ 039 public IteratorTemplate(Collection collection) { 040 this.collection = collection; 041 } 042 043 /** 044 * Constructor. When passing an Iterator, the Template will be a run-once 045 * instance. 046 * 047 * @param it Iterator over the elements. 048 */ 049 public IteratorTemplate(Iterator it) { 050 super(true); 051 this.it = it; 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 protected void doSetup() { 058 if (this.collection != null) { 059 this.it = this.collection.iterator(); 060 } 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 protected boolean hasMoreWork() { 067 return it.hasNext(); 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 protected Object doWork() { 074 return it.next(); 075 } 076 }