34
35 class LiteralFactory extends Factory<Literal> {
36 protected final Type resultType;
37
38 LiteralFactory(Type resultType) {
39 this.resultType = resultType;
40 }
41
42 @Override
43 public Literal produce() throws ProductionFailedException {
44 Literal literal;
45 if (resultType.equals(TypeList.BOOLEAN)) {
46 literal = new Literal(PseudoRandom.randomBoolean(), TypeList.BOOLEAN);
47 } else if (resultType.equals(TypeList.CHAR)) {
48 literal = new Literal((char) ((char) (PseudoRandom.random() * ('z' - 'A')) + 'A'), TypeList.CHAR);
49 } else if (resultType.equals(TypeList.INT)) {
50 literal = new Literal((int) (PseudoRandom.random() * Integer.MAX_VALUE), TypeList.INT);
51 } else if (resultType.equals(TypeList.LONG)) {
52 literal = new Literal((long) (PseudoRandom.random() * Long.MAX_VALUE), TypeList.LONG);
53 } else if (resultType.equals(TypeList.FLOAT)) {
54 literal = new Literal(new Float(String.format(
55 (Locale) null,
56 "%." + ProductionParams.floatingPointPrecision.value() + "EF",
57 (float) PseudoRandom.random() * Float.MAX_VALUE)),
58 TypeList.FLOAT);
59 } else if (resultType.equals(TypeList.DOUBLE)) {
60 literal = new Literal(new Double(String.format(
61 (Locale) null,
62 "%." + 2 * ProductionParams.floatingPointPrecision.value() + "E",
63 PseudoRandom.random() * Double.MAX_VALUE)),
64 TypeList.DOUBLE);
65 } else if (resultType.equals(TypeList.BYTE)) {
66 literal = new Literal((byte)(PseudoRandom.random() * Byte.MAX_VALUE), TypeList.BYTE);
67 } else if (resultType.equals(TypeList.SHORT)) {
68 literal = new Literal((short)(PseudoRandom.random() * Short.MAX_VALUE), TypeList.SHORT);
69 } else if (resultType.equals(TypeList.STRING)) {
70 int size = (int) (PseudoRandom.random() * ProductionParams.stringLiteralSizeLimit.value());
71 byte[] str = new byte[size];
72 for (int i = 0; i < size; i++) {
73 str[i] = (byte) ((int) (('z' - 'a') * PseudoRandom.random()) + 'a');
74 }
75 literal = new Literal(new String(str), TypeList.STRING);
76 } else {
77 throw new ProductionFailedException();
78 }
79 return literal;
80 }
|
34
35 class LiteralFactory extends Factory<Literal> {
36 protected final Type resultType;
37
38 LiteralFactory(Type resultType) {
39 this.resultType = resultType;
40 }
41
42 @Override
43 public Literal produce() throws ProductionFailedException {
44 Literal literal;
45 if (resultType.equals(TypeList.BOOLEAN)) {
46 literal = new Literal(PseudoRandom.randomBoolean(), TypeList.BOOLEAN);
47 } else if (resultType.equals(TypeList.CHAR)) {
48 literal = new Literal((char) ((char) (PseudoRandom.random() * ('z' - 'A')) + 'A'), TypeList.CHAR);
49 } else if (resultType.equals(TypeList.INT)) {
50 literal = new Literal((int) (PseudoRandom.random() * Integer.MAX_VALUE), TypeList.INT);
51 } else if (resultType.equals(TypeList.LONG)) {
52 literal = new Literal((long) (PseudoRandom.random() * Long.MAX_VALUE), TypeList.LONG);
53 } else if (resultType.equals(TypeList.FLOAT)) {
54 literal = new Literal(Float.valueOf(String.format(
55 (Locale) null,
56 "%." + ProductionParams.floatingPointPrecision.value() + "EF",
57 (float) PseudoRandom.random() * Float.MAX_VALUE)),
58 TypeList.FLOAT);
59 } else if (resultType.equals(TypeList.DOUBLE)) {
60 literal = new Literal(Double.valueOf(String.format(
61 (Locale) null,
62 "%." + 2 * ProductionParams.floatingPointPrecision.value() + "E",
63 PseudoRandom.random() * Double.MAX_VALUE)),
64 TypeList.DOUBLE);
65 } else if (resultType.equals(TypeList.BYTE)) {
66 literal = new Literal((byte)(PseudoRandom.random() * Byte.MAX_VALUE), TypeList.BYTE);
67 } else if (resultType.equals(TypeList.SHORT)) {
68 literal = new Literal((short)(PseudoRandom.random() * Short.MAX_VALUE), TypeList.SHORT);
69 } else if (resultType.equals(TypeList.STRING)) {
70 int size = (int) (PseudoRandom.random() * ProductionParams.stringLiteralSizeLimit.value());
71 byte[] str = new byte[size];
72 for (int i = 0; i < size; i++) {
73 str[i] = (byte) ((int) (('z' - 'a') * PseudoRandom.random()) + 'a');
74 }
75 literal = new Literal(new String(str), TypeList.STRING);
76 } else {
77 throw new ProductionFailedException();
78 }
79 return literal;
80 }
|