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.GeneratorProxy;
030: import org.databene.benerator.Generator;
031: import org.databene.benerator.SimpleRandom;
032: import org.databene.benerator.IllegalGeneratorStateException;
033:
034: /**
035: * A generator proxy that forwards the output of another generator with a random number of repetitions.<br/>
036: * <br/>
037: * Created: 18.08.2007 17:08:10
038: */
039: public class RepeatGeneratorProxy<E> extends GeneratorProxy<E> {
040:
041: private long minRepetitions;
042: private long maxRepetitions;
043: private long repCount;
044: private long totalReps;
045: private E next;
046:
047: public RepeatGeneratorProxy() {
048: this (null, 0L, 3L);
049: }
050:
051: public RepeatGeneratorProxy(Generator<E> source,
052: Long minRepetitions, Long maxRepetitions) {
053: super (source);
054: repCount = -1;
055: setMinRepetitions(minRepetitions);
056: setMaxRepetitions(maxRepetitions);
057: totalReps = SimpleRandom.randomLong(minRepetitions,
058: maxRepetitions);
059: }
060:
061: public long getMinRepetitions() {
062: return minRepetitions;
063: }
064:
065: public void setMinRepetitions(long minRepetitions) {
066: if (minRepetitions < 0)
067: throw new IllegalArgumentException(
068: "minRepetitions must be >= 0, was: "
069: + minRepetitions);
070: this .minRepetitions = minRepetitions;
071: }
072:
073: public long getMaxRepetitions() {
074: return maxRepetitions;
075: }
076:
077: public void setMaxRepetitions(long maxRepetitions) {
078: if (maxRepetitions < minRepetitions)
079: throw new IllegalArgumentException(
080: "maxRepetitions must be >= minRepetitions");
081: this .maxRepetitions = maxRepetitions;
082: }
083:
084: public void validate() {
085: if (dirty) {
086: super .validate();
087: next = source.generate();
088: }
089: }
090:
091: public boolean available() {
092: if (dirty)
093: validate();
094: return repCount < totalReps;
095: }
096:
097: public E generate() {
098: if (dirty)
099: validate();
100: if (next == null)
101: throw new IllegalGeneratorStateException(
102: "Generator is no more available");
103: E result = next;
104: repCount++;
105: if (repCount >= totalReps) {
106: if (source.available()) {
107: next = source.generate();
108: totalReps = SimpleRandom.randomLong(minRepetitions,
109: maxRepetitions);
110: repCount = -1;
111: } else {
112: next = null;
113: }
114: }
115: return result;
116: }
117: }
|