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;
028:
029: import org.databene.commons.ConfigurationError;
030: import org.databene.commons.BeanUtil;
031: import org.databene.commons.IOUtil;
032: import org.databene.commons.StringUtil;
033:
034: import java.util.Map;
035: import java.util.HashMap;
036: import java.util.Collection;
037: import java.util.Properties;
038: import java.io.IOException;
039:
040: /**
041: * Provides access to specific Sequence number Generators.<br/>
042: * <br/>
043: * Created: 11.09.2006 21:12:57
044: */
045: public class Sequence implements Distribution {
046:
047: private static Map<String, Sequence> map = new HashMap<String, Sequence>();
048:
049: private static final String CONFIG_FILE_NAME = "org/databene/benerator/sequence.properties";
050:
051: static {
052: try {
053: Properties properties = IOUtil
054: .readProperties(CONFIG_FILE_NAME);
055: for (Map.Entry<Object, Object> entry : properties
056: .entrySet()) {
057: String name = (String) entry.getKey();
058: String setup = (String) entry.getValue();
059: String[] generatorClasses = StringUtil.tokenize(setup,
060: ',');
061: Class<AbstractDoubleGenerator> dgClass = BeanUtil
062: .forName(generatorClasses[0]);
063: if (generatorClasses.length == 1) {
064: new Sequence(name, dgClass);
065: } else {
066: Class<AbstractLongGenerator> ngClass = BeanUtil
067: .forName(generatorClasses[1]);
068: new Sequence(name, dgClass, ngClass);
069: }
070: }
071: } catch (IOException e) {
072: throw new ConfigurationError(
073: "Configuration file cannot be read", e);
074: }
075: }
076:
077: // predefined Sequences --------------------------------------------------------------------------------------------
078:
079: public static final Sequence RANDOM = Sequence
080: .getInstance("random");
081: public static final Sequence SHUFFLE = Sequence
082: .getInstance("shuffle");
083: public static final Sequence CUMULATED = Sequence
084: .getInstance("cumulated");
085: public static final Sequence RANDOM_WALK = Sequence
086: .getInstance("randomWalk");
087: public static final Sequence STEP = Sequence.getInstance("step");
088: public static final Sequence WEDGE = Sequence.getInstance("wedge");
089: public static final Sequence BIT_REVERSE = Sequence
090: .getInstance("bitreverse");
091:
092: // attributes ------------------------------------------------------------------------------------------------------
093:
094: private String name;
095: private Class<? extends AbstractLongGenerator> longGeneratorClass;
096: private Class<? extends AbstractDoubleGenerator> doubleGeneratorClass;
097:
098: // factory methods -------------------------------------------------------------------------------------------------
099:
100: public Sequence(String name,
101: Class<? extends NumberGenerator> numberGeneratorClass) {
102: this (
103: name,
104: (AbstractDoubleGenerator.class
105: .isAssignableFrom(numberGeneratorClass) ? (Class<AbstractDoubleGenerator>) numberGeneratorClass
106: : DoubleFromLongGenerator.class),
107: (AbstractLongGenerator.class
108: .isAssignableFrom(numberGeneratorClass) ? (Class<AbstractLongGenerator>) numberGeneratorClass
109: : LongFromDoubleGenerator.class));
110: }
111:
112: public Sequence(
113: String name,
114: Class<? extends AbstractDoubleGenerator> doubleGeneratorClass,
115: Class<? extends AbstractLongGenerator> longGeneratorClass) {
116: this .name = name;
117: this .longGeneratorClass = longGeneratorClass;
118: this .doubleGeneratorClass = doubleGeneratorClass;
119: if (map.get(name) != null)
120: throw new ConfigurationError("Sequence defined twice: "
121: + name);
122: map.put(name, this );
123: }
124:
125: public static Collection<Sequence> getInstances() {
126: return map.values();
127: }
128:
129: public static Sequence getInstance(String name) {
130: Sequence sequence = map.get(name);
131: if (sequence == null)
132: throw new IllegalArgumentException("Sequence undefined: "
133: + name);
134: return sequence;
135: }
136:
137: // interface -------------------------------------------------------------------------------------------------------
138:
139: public String getName() {
140: return name;
141: }
142:
143: public AbstractLongGenerator createLongGenerator() {
144: if (LongFromDoubleGenerator.class.equals(longGeneratorClass))
145: return new LongFromDoubleGenerator(createDoubleGenerator());
146: else
147: return BeanUtil.newInstance(longGeneratorClass);
148: }
149:
150: public AbstractDoubleGenerator createDoubleGenerator() {
151: if (DoubleFromLongGenerator.class.equals(doubleGeneratorClass))
152: return new DoubleFromLongGenerator(createLongGenerator());
153: else
154: return BeanUtil.newInstance(doubleGeneratorClass);
155: }
156:
157: // java.lang.Object overrides --------------------------------------------------------------------------------------
158:
159: public String toString() {
160: return name;
161: }
162: }
|