01: package com.bostechcorp.cbesb.custom;
02:
03: import java.text.DecimalFormat;
04: import java.util.Map;
05: import java.util.Random;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09:
10: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;
11:
12: public class RandomFloat implements ITransformationOperation {
13:
14: protected static transient Log logger = LogFactory
15: .getLog(RandomFloat.class);
16:
17: public void addProperty(String name, String value) {
18:
19: }
20:
21: public void cleanup(Map<String, Object> transformerContext)
22: throws Exception {
23:
24: }
25:
26: public void initialize(Map<String, Object> transformerContext)
27: throws Exception {
28:
29: }
30:
31: public boolean process(String[] source, String[] target)
32: throws Exception {
33: if (source.length == 3) {
34: float minValue = Float.parseFloat(source[0]);
35: float maxValue = Float.parseFloat(source[1]);
36: int decPlaces = Integer.parseInt(source[2]);
37:
38: Random random = new Random();
39: float multiplier = random.nextFloat();
40: float diff = maxValue - minValue;
41: float randomFloat = minValue + (diff * multiplier);
42:
43: DecimalFormat decFormat = new DecimalFormat();
44: decFormat.setMaximumFractionDigits(decPlaces);
45: decFormat.setMinimumFractionDigits(decPlaces);
46: target[0] = decFormat.format(randomFloat);
47: return true;
48: }
49:
50: return false;
51: }
52:
53: // Main method for testing
54: public static void main(String[] args) {
55: if (args.length != 3) {
56: System.out
57: .println("Usage: RandomFloat minValue maxValue decimalPlaces\n");
58: System.exit(0);
59: }
60:
61: String[] target = new String[1];
62: RandomFloat instance = new RandomFloat();
63: try {
64: boolean status = instance.process(args, target);
65: if (status) {
66: System.out.println("Random number = " + target[0]);
67: } else {
68: System.out.println("process returned false");
69: }
70: } catch (Exception e) {
71: logger.error("Exception in main(): " + e.getMessage());
72: if (logger.isDebugEnabled()) {
73: logger.debug("Exception in main():", e);
74: }
75: }
76:
77: }
78:
79: }
|