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.Generator;
030: import org.databene.benerator.MultiGeneratorWrapper;
031: import org.databene.benerator.Distribution;
032: import org.databene.benerator.primitive.number.adapter.IntegerGenerator;
033:
034: /**
035: * On each call to generate(), it chooses a generator from a collection,
036: * calls its generate() method and returns the product.<br/>
037: * <br/>
038: * Created: 30.08.2006 21:56:59
039: */
040: public class AlternativeGenerator<E> extends
041: MultiGeneratorWrapper<E, E> {
042:
043: private IntegerGenerator indexGenerator;
044: private Class<E> targetType;
045:
046: // constructors ----------------------------------------------------------------------------------------------------
047:
048: public AlternativeGenerator() {
049: this (null);
050: }
051:
052: /** Initializes the generator to a collection of source generators */
053: public AlternativeGenerator(Class<E> targetType,
054: Generator<E>... sources) {
055: super (sources);
056: this .targetType = targetType;
057: this .indexGenerator = new IntegerGenerator(0,
058: sources.length - 1);
059: }
060:
061: // config properties -----------------------------------------------------------------------------------------------
062:
063: public Integer getVariation1() {
064: return indexGenerator.getVariation1();
065: }
066:
067: public void setVariation1(Integer varation1) {
068: indexGenerator.setVariation1(varation1);
069: }
070:
071: public Integer getVariation2() {
072: return indexGenerator.getVariation2();
073: }
074:
075: public void setVariation2(Integer variation2) {
076: indexGenerator.setVariation2(variation2);
077: }
078:
079: public Distribution getDistribution() {
080: return indexGenerator.getDistribution();
081: }
082:
083: public void setDistribution(Distribution distribution) {
084: indexGenerator.setDistribution(distribution);
085: }
086:
087: // Generator implementation ----------------------------------------------------------------------------------------
088:
089: public Class<E> getGeneratedType() {
090: return targetType;
091: }
092:
093: public void validate() {
094: super .validate();
095: indexGenerator.validate();
096: }
097:
098: /** @see org.databene.benerator.Generator#generate() */
099: public E generate() {
100: Generator<E> generator = getSource(indexGenerator.generate());
101: return generator.generate();
102: }
103:
104: }
|