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.sample;
028:
029: import org.databene.benerator.primitive.number.adapter.IntegerGenerator;
030: import org.databene.benerator.*;
031:
032: import java.util.List;
033: import java.util.ArrayList;
034: import java.util.Collection;
035:
036: /**
037: * Generates values from an unweighted list of samples.<br/>
038: * <br/>
039: * Created: 07.06.2006 19:04:08
040: */
041: public class SequencedSampleGenerator<E> extends
042: LightweightGenerator<E> {
043:
044: private Class<E> targetType;
045:
046: /** Keeps the Sample information */
047: private List<E> samples = new ArrayList<E>();
048:
049: /** Generator for choosing a List index of the sample list */
050: private IntegerGenerator indexGenerator = new IntegerGenerator(0,
051: 0, 1, Sequence.RANDOM);
052:
053: /** Flag that indicates if the generator needs to be initialized */
054: private boolean dirty = true;
055:
056: // constructors ----------------------------------------------------------------------------------------------------
057:
058: public SequencedSampleGenerator() {
059: this (null);
060: }
061:
062: /** Initializes the generator to an empty sample list */
063: public SequencedSampleGenerator(Class<E> targetType) {
064: this (targetType, new ArrayList<E>());
065: }
066:
067: /** Initializes the generator to a sample list */
068: public SequencedSampleGenerator(Class<E> targetType, E... values) {
069: this .targetType = targetType;
070: setValues(values);
071: }
072:
073: /** Initializes the generator to a sample list */
074: public SequencedSampleGenerator(Class<E> targetType,
075: Sequence distribution, E... values) {
076: this .targetType = targetType;
077: setDistribution(distribution);
078: setValues(values);
079: }
080:
081: /** Initializes the generator to a sample list */
082: public SequencedSampleGenerator(Class<E> targetType,
083: Collection<E> values) {
084: this .targetType = targetType;
085: setValues(values);
086: }
087:
088: /** Initializes the generator to a sample list */
089: public SequencedSampleGenerator(Class<E> targetType,
090: Sequence distribution, Collection<E> values) {
091: this .targetType = targetType;
092: setDistribution(distribution);
093: setValues(values);
094: }
095:
096: // config properties -----------------------------------------------------------------------------------------------
097:
098: public Sequence getDistribution() {
099: return (Sequence) indexGenerator.getDistribution();
100: }
101:
102: public void setDistribution(Sequence distribution) {
103: indexGenerator.setDistribution(distribution);
104: }
105:
106: public Integer getVariation1() {
107: return indexGenerator.getVariation1();
108: }
109:
110: public void setVariation1(Integer varation1) {
111: indexGenerator.setVariation1(varation1);
112: }
113:
114: public Integer getVariation2() {
115: return indexGenerator.getVariation2();
116: }
117:
118: public void setVariation2(Integer variation2) {
119: indexGenerator.setVariation2(variation2);
120: }
121:
122: // values property -------------------------------------------------------------------------------------------------
123:
124: /** Adds values to the sample list */
125: public void setValues(Collection<E> values) {
126: this .samples.clear();
127: if (values != null)
128: for (E value : values)
129: addValue(value);
130: }
131:
132: /** Sets the sample list to the specified values */
133: public void setValues(E... values) {
134: this .samples.clear();
135: if (values != null)
136: for (E value : values)
137: addValue(value);
138: }
139:
140: /** Adds values to the sample list */
141: public void addValues(E... values) {
142: if (values != null)
143: for (E value : values)
144: addValue(value);
145: }
146:
147: /** Adds values to the sample list */
148: public void addValues(Collection<E> values) {
149: if (values != null)
150: for (E value : values)
151: addValue(value);
152: }
153:
154: /** Adds a value to the sample list */
155: public void addValue(E value) {
156: samples.add(value);
157: this .dirty = true;
158: }
159:
160: // Generator implementation ----------------------------------------------------------------------------------------
161:
162: /** Initializes all attributes */
163: public void validate() {
164: if (dirty) {
165: if (samples.size() > 0) {
166: indexGenerator.setMax(samples.size() - 1);
167: indexGenerator.validate();
168: }
169: this .dirty = false;
170: }
171: }
172:
173: public Class<E> getGeneratedType() {
174: return targetType;
175: }
176:
177: /** @see org.databene.benerator.Generator#generate() */
178: public E generate() {
179: if (dirty)
180: validate();
181: if (samples.size() == 0)
182: return null;
183: int index = indexGenerator.generate();
184: return samples.get(index);
185: }
186:
187: // static interface ------------------------------------------------------------------------------------------------
188:
189: /** Convenience utility method that chooses one sample out of a list with uniform random distribution */
190: public static <T> T generate(T... samples) {
191: return samples[SimpleRandom.randomInt(0, samples.length - 1)];
192: }
193:
194: /** Convenience utility method that chooses one sample out of a list with uniform random distribution */
195: public static <T> T generate(List<T> samples) {
196: return samples.get(SimpleRandom
197: .randomInt(0, samples.size() - 1));
198: }
199:
200: // java.lang.Object overrides --------------------------------------------------------------------------------------
201:
202: public String toString() {
203: return getClass().getSimpleName() + '['
204: + indexGenerator.getDistribution() + ']';
205: }
206: }
|