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.commons.BeanUtil;
030: import org.databene.benerator.primitive.number.adapter.IntegerGenerator;
031: import org.databene.benerator.*;
032:
033: import java.util.*;
034:
035: /**
036: * Combines a a random number a source generator's products into a collection.<br/>
037: * <br/>
038: * Created: 07.07.2006 19:13:22
039: */
040: public class CollectionGenerator<C extends Collection, I> extends
041: GeneratorWrapper<I, C> {
042:
043: /** The collection type to create */
044: private Class<C> collectionType;
045:
046: /** Generator that determines the collection size on generation */
047: private IntegerGenerator sizeGenerator;
048:
049: // constructors ----------------------------------------------------------------------------------------------------
050:
051: public CollectionGenerator() {
052: this ((Class<C>) List.class, null);
053: }
054:
055: public CollectionGenerator(Class<C> collectionType) {
056: this (collectionType, null);
057: }
058:
059: public CollectionGenerator(Class<C> collectionType,
060: Generator<I> source) {
061: this (collectionType, source, 0, 30, Sequence.RANDOM);
062: }
063:
064: public CollectionGenerator(Class<C> collectionType,
065: Generator<I> source, int minLength, int maxLength) {
066: this (collectionType, source, minLength, maxLength,
067: Sequence.RANDOM);
068: }
069:
070: public CollectionGenerator(Class<C> collectionType,
071: Generator<I> source, int minLength, int maxLength,
072: Distribution lengthDistribution) {
073: super (source);
074: this .collectionType = mapCollectionType(collectionType);
075: sizeGenerator = new IntegerGenerator(minLength, maxLength, 1,
076: lengthDistribution);
077: }
078:
079: // configuration properties ----------------------------------------------------------------------------------------
080:
081: public Class<C> getCollectionType() {
082: return collectionType;
083: }
084:
085: public void setCollectionType(Class<C> collectionType) {
086: this .collectionType = collectionType;
087: }
088:
089: public Generator<I> getSource() {
090: return source;
091: }
092:
093: public void setSource(Generator<I> source) {
094: this .source = source;
095: }
096:
097: public int getMinSize() {
098: return sizeGenerator.getMin();
099: }
100:
101: public void setMinSize(int minCardinality) {
102: sizeGenerator.setMin(minCardinality);
103: }
104:
105: public int getMaxSize() {
106: return sizeGenerator.getMin();
107: }
108:
109: public void setMaxSize(int maxCardinality) {
110: sizeGenerator.setMax(maxCardinality);
111: }
112:
113: public Distribution getSizeDistribution() {
114: return sizeGenerator.getDistribution();
115: }
116:
117: public void setSizeDistribution(Distribution distribution) {
118: sizeGenerator.setDistribution(distribution);
119: }
120:
121: public Integer getSizeVariation1() {
122: return sizeGenerator.getVariation1();
123: }
124:
125: public void setSizeVariation1(Integer varation1) {
126: sizeGenerator.setVariation1(varation1);
127: }
128:
129: public Integer getSizeVariation2() {
130: return sizeGenerator.getVariation2();
131: }
132:
133: public void setSizeVariation2(Integer variation2) {
134: sizeGenerator.setVariation2(variation2);
135: }
136:
137: // Generator interface ---------------------------------------------------------------------------------------------
138:
139: /** ensures consistency of the state */
140: public void validate() {
141: if (collectionType == null)
142: throw new InvalidGeneratorSetupException("collectionType",
143: "undefined");
144: sizeGenerator.validate();
145: super .validate();
146: }
147:
148: public Class<C> getGeneratedType() {
149: return collectionType;
150: }
151:
152: /** @see org.databene.benerator.Generator#generate() */
153: public C generate() {
154: if (dirty)
155: validate();
156: C collection = BeanUtil.newInstance(collectionType);
157: int size = sizeGenerator.generate();
158: for (int i = 0; i < size; i++)
159: collection.add(source.generate());
160: return collection;
161: }
162:
163: // implementation --------------------------------------------------------------------------------------------------
164:
165: /** maps abstract collection types to concrete ones */
166: private static <C extends Collection> Class<C> mapCollectionType(
167: Class<C> collectionType) {
168: if (List.class.equals(collectionType))
169: return (Class<C>) ArrayList.class;
170: else if (Set.class.equals(collectionType))
171: return (Class<C>) HashSet.class;
172: else
173: return collectionType;
174: }
175:
176: }
|