01: package org.obe.sql;
02:
03: import java.io.IOException;
04: import java.io.Writer;
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: /**
09: * @author Adrian Price
10: */
11: public class SQLProductExpr extends SQLArithmeticExpr {
12: private static final int MULTIPLY = 0;
13: private static final int DIVIDE = 1;
14: private static final String[] _operators = { "*", "/" };
15: private static final Map operatorMap = new HashMap();
16:
17: static {
18: operatorMap.put("*", new Integer(MULTIPLY));
19: operatorMap.put("/", new Integer(DIVIDE));
20: }
21:
22: public SQLProductExpr(int id) {
23: super (id);
24: }
25:
26: public void addOperator(String op) {
27: operators.add(operatorMap.get(op));
28: }
29:
30: public void write(Writer out) throws IOException {
31: for (int i = 0; i < children.length; i++) {
32: children[i].write(out);
33: if (i > 0) {
34: int opCode = ((Integer) operators.get(i - 1))
35: .intValue();
36: out.write(' ');
37: out.write(_operators[opCode]);
38: out.write(' ');
39: }
40: }
41: }
42:
43: public Object execute(Object context) {
44: Object[] args = { children[0].execute(context), null };
45: for (int i = 1; i < children.length; i++) {
46: args[1] = children[i].execute(context);
47: switch (((Integer) operators.get(i - 1)).intValue()) {
48: case MULTIPLY:
49: args[0] = multiply(args);
50: break;
51: case DIVIDE:
52: args[0] = divide(args);
53: break;
54: }
55: }
56: return args[0];
57: }
58: }
|