1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 }