001: /*
002: * (c) Copyright 2006 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.*;
030: import org.databene.benerator.primitive.number.adapter.LongGenerator;
031:
032: /**
033: * This forwards a source generator's products.
034: * Iterates through the products of another generator with a variable step width.
035: * This is intended mainly for use with importing generators that provide data
036: * volumes too big to keep in RAM.<br/>
037: * <br/>
038: * Created: 26.08.2006 16:16:04
039: */
040: public class SkipGeneratorProxy<E> extends GeneratorProxy<E> {
041:
042: /** The increment generator, which creates an individual increment on each generation */
043: private LongGenerator incrementGenerator;
044:
045: // constructors ----------------------------------------------------------------------------------------------------
046:
047: public SkipGeneratorProxy() {
048: this (null);
049: }
050:
051: /** Initializes the generator to iterate with increment 1 */
052: public SkipGeneratorProxy(Generator<E> source) {
053: this (source, 1L, 1L);
054: }
055:
056: /** Initializes the generator to use a random increment of uniform distribution */
057: public SkipGeneratorProxy(Generator<E> source, Long minIncrement,
058: Long maxIncrement) {
059: this (source, minIncrement, maxIncrement, Sequence.RANDOM);
060: }
061:
062: /** Initializes the generator */
063: public SkipGeneratorProxy(Generator<E> source, Long minIncrement,
064: Long maxIncrement, Distribution incrementDistribution) {
065: super (source);
066: this .incrementGenerator = new LongGenerator(minIncrement,
067: maxIncrement, 1L, incrementDistribution);
068: }
069:
070: // config properties -----------------------------------------------------------------------------------------------
071:
072: public long getMinIncrement() {
073: return incrementGenerator.getMin();
074: }
075:
076: public void setMinIncrement(long minIncrement) {
077: incrementGenerator.setMin(minIncrement);
078: }
079:
080: public long getMaxIncrement() {
081: return incrementGenerator.getMax();
082: }
083:
084: public void setMaxIncrement(long maxIncrement) {
085: incrementGenerator.setMax(maxIncrement);
086: }
087:
088: public Distribution getIncrementDistribution() {
089: return incrementGenerator.getDistribution();
090: }
091:
092: public void setIncrementDistribution(Distribution distribution) {
093: incrementGenerator.setDistribution(distribution);
094: }
095:
096: public Long getIncrementVariation1() {
097: return incrementGenerator.getVariation1();
098: }
099:
100: public void setIncrementVariation1(Long varation1) {
101: incrementGenerator.setVariation1(varation1);
102: }
103:
104: public Long getIncrementVariation2() {
105: return incrementGenerator.getVariation2();
106: }
107:
108: public void setIncrementVariation2(Long variation2) {
109: incrementGenerator.setVariation2(variation2);
110: }
111:
112: // Generator implementation ----------------------------------------------------------------------------------------
113:
114: public void validate() {
115: if (incrementGenerator.getMin() < 0)
116: throw new InvalidGeneratorSetupException("minIncrement",
117: "less than 0");
118: incrementGenerator.validate();
119: super .validate();
120: }
121:
122: /** @see org.databene.benerator.Generator#reset() */
123: public E generate() {
124: if (!source.available())
125: throw new IllegalGeneratorStateException(
126: "source is not available");
127: long increment = incrementGenerator.generate();
128: for (long i = 0; i < increment - 1; i++)
129: source.generate();
130: return source.generate();
131: }
132:
133: public void close() {
134: super .close();
135: incrementGenerator.close();
136: }
137:
138: public void reset() {
139: super.reset();
140: incrementGenerator.reset();
141: }
142: }
|