001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.wrapper;
028:
029: import org.databene.benerator.Generator;
030: import org.databene.benerator.InvalidGeneratorSetupException;
031: import org.databene.commons.HeavyweightIterator;
032: import org.databene.commons.TypedIterable;
033:
034: import java.util.Iterator;
035:
036: /**
037: * Iterates over Iterators that are provided by an Iterable.<br/>
038: * <br/>
039: * Created: 16.08.2007 07:09:57
040: */
041: public class IteratingGenerator<E> implements Generator<E> {
042:
043: private TypedIterable<E> iterable;
044:
045: private Iterator<E> iterator;
046: private boolean dirty;
047:
048: public IteratingGenerator() {
049: this (null);
050: }
051:
052: public IteratingGenerator(TypedIterable<E> iterable) {
053: this .iterable = iterable;
054: this .dirty = true;
055: }
056:
057: public TypedIterable<E> getIterable() {
058: return iterable;
059: }
060:
061: public void setIterable(TypedIterable<E> iterable) {
062: this .iterable = iterable;
063: this .dirty = true;
064: }
065:
066: // Generator interface ---------------------------------------------------------------------------------------------
067:
068: public void validate() {
069: if (dirty) {
070: if (iterable == null)
071: throw new InvalidGeneratorSetupException("iterable",
072: "is null");
073: close();
074: this .iterator = iterable.iterator();
075: dirty = false;
076: }
077: }
078:
079: public Class<E> getGeneratedType() {
080: if (dirty)
081: validate();
082: return iterable.getType();
083: }
084:
085: public E generate() {
086: if (dirty)
087: validate();
088: return iterator.next();
089: }
090:
091: public void reset() {
092: close();
093: iterator = iterable.iterator();
094: }
095:
096: public void close() {
097: if (iterator != null) {
098: if (iterator instanceof HeavyweightIterator)
099: ((HeavyweightIterator) iterator).close();
100: iterator = null;
101: }
102: }
103:
104: public boolean available() {
105: if (dirty)
106: validate();
107: return (iterator != null && iterator.hasNext());
108: }
109:
110: // java.lang.Object overrides --------------------------------------------------------------------------------------
111:
112: public String toString() {
113: return "IteratingGenerator[" + iterable + ']';
114: }
115: }
|