View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.generator;
48  
49  import java.math.BigDecimal;
50  import java.util.Random;
51  
52  import pl.kernelpanic.dbmonster.DBMonster;
53  import pl.kernelpanic.dbmonster.DBMonsterContext;
54  
55  /***
56   * The generator which only produces nulls.
57   *
58   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
59   *
60   * @version $Id: NumberGenerator.html,v 1.1 2007/06/21 08:38:13 sbahloul Exp $
61   */
62  public class NumberGenerator extends BasicDataGenerator implements Initializable {
63  
64      /***
65       * Result should be short.
66       */
67      public static final int SHORT = 0;
68  
69      /***
70       * Result should be integer.
71       */
72      public static final int INTEGER = 1;
73  
74      /***
75       * Result should be long.
76       */
77      public static final int LONG = 2;
78  
79      /***
80       * Result should be float.
81       */
82      public static final int FLOAT = 3;
83  
84      /***
85       * Result should be double.
86       */
87      public static final int DOUBLE = 4;
88  
89      /***
90       * The result should be numeric.
91       */
92      public static final int NUMERIC = 5;
93  
94      /***
95       * Minimal value.
96       */
97      private BigDecimal minValue = new BigDecimal("0");
98  
99      /***
100      * Maximal value.
101      */
102     private BigDecimal maxValue = new BigDecimal("127");
103 
104     /***
105      * Scale.
106      */
107     private int scale = 0;
108 
109     /***
110      * Returned type.
111      */
112     private int returnedType = SHORT;
113 
114     /***
115      * Random number generator.
116      */
117     private Random random = null;
118 
119     /***
120      * Initializes the generator.
121      *
122      * @param ctx context
123      *
124      * @throws Exception if generator contains errors.
125      *
126      * @todo dopisac validacje zakresow!!!
127      */
128     public final void initialize(final DBMonsterContext ctx) throws Exception {
129         random = (Random) ctx.getProperty(DBMonster.RANDOM_KEY);
130         if (minValue.compareTo(maxValue) > 0) {
131             throw new Exception("MinValue < maxValue");
132         }
133     }
134 
135     /***
136      * Generates random number.
137      *
138      * @return number
139      */
140     public final Object generate() {
141 
142         if (nulls != 0 && random.nextInt(101) <= nulls) {
143             return null;
144         }
145 
146         BigDecimal retValue = null;
147         BigDecimal length = maxValue.subtract(minValue);
148         BigDecimal factor = new BigDecimal(random.nextDouble());
149         retValue = length.multiply(factor).add(minValue);
150         if (returnedType == SHORT) {
151             return new Short((short) retValue.toBigInteger().intValue());
152         }
153         if (returnedType == INTEGER) {
154            return new Integer(retValue.toBigInteger().intValue());
155         }
156         if (returnedType == LONG) {
157             return new Long(retValue.toBigInteger().longValue());
158         }
159         if (returnedType == FLOAT) {
160             return new Float(retValue.floatValue());
161         }
162         if (returnedType == DOUBLE) {
163             return new Double(retValue.doubleValue());
164         }
165         retValue = retValue.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
166         return retValue;
167     }
168 
169     /***
170      * Returns minimal value.
171      *
172      * @return minimal value
173      */
174     public final String getMinValue() {
175         return minValue.toString();
176     }
177 
178     /***
179      * Minimal value.
180      *
181      * @param minVal minimal value
182      */
183     public final void setMinValue(final String minVal) {
184         minValue = new BigDecimal(minVal);
185     }
186 
187     /***
188      * Returns maximal value.
189      *
190      * @return maximal value
191      */
192     public final String getMaxValue() {
193         return maxValue.toString();
194     }
195 
196     /***
197      * Maximum value.
198      *
199      * @param maxVal maximum value
200      */
201     public final void setMaxValue(final String maxVal) {
202         maxValue = new BigDecimal(maxVal);
203     }
204 
205     /***
206      * Returns scale.
207      *
208      * @return number of fraction digits
209      */
210     public final int getScale() {
211         return scale;
212     }
213 
214     /***
215      * Sets the scale.
216      *
217      * @param s number of fraction digits
218      */
219     public final void setScale(final int s) {
220         scale = s;
221     }
222 
223     /***
224      * Returns type.
225      *
226      * @return string representation of returned time
227      */
228     public final String getReturnedType() {
229         if (returnedType == INTEGER) {
230             return "integer";
231         }
232         if (returnedType == LONG) {
233             return "long";
234         }
235         if (returnedType == FLOAT) {
236             return "float";
237         }
238         if (returnedType == DOUBLE) {
239             return "double";
240         }
241         if (returnedType == NUMERIC) {
242             return "numeric";
243         }
244         return "short";
245     }
246 
247     /***
248      * Sets the returned type.
249      *
250      * @param type type
251      */
252     public final void setReturnedType(final String type) {
253         if ("integer".equals(type)) {
254             returnedType = INTEGER;
255         } else if ("long".equals(type)) {
256             returnedType = LONG;
257         } else if ("float".equals(type)) {
258             returnedType = FLOAT;
259         } else if ("double".equals(type)) {
260             returnedType = DOUBLE;
261         } else if ("numeric".equals(type)) {
262             returnedType = NUMERIC;
263         } else {
264             returnedType = SHORT;
265         }
266     }
267 
268     /***
269      * @see pl.kernelpanic.dbmonster.generator.DataGenerator#reset()
270      */
271     public void reset() {
272     }
273 }