KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > core > nodes > OperatorNode


1 package com.genimen.djeneric.repository.oql.core.nodes;
2
3 import java.math.BigDecimal JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import com.genimen.djeneric.repository.oql.core.DjOqlParserEngine;
7 import com.genimen.djeneric.repository.oql.core.MatchException;
8 import com.genimen.djeneric.repository.oql.core.ParseException;
9 import com.genimen.djeneric.repository.oql.core.SimpleNode;
10
11 public class OperatorNode extends SimpleNode implements ValueExpression
12 {
13   private String JavaDoc operator;
14
15   public OperatorNode(int i)
16   {
17     super(i);
18   }
19
20   public OperatorNode(DjOqlParserEngine p, int i)
21   {
22     super(p, i);
23   }
24
25   public String JavaDoc getName()
26   {
27     return toString();
28   }
29
30   public String JavaDoc toString()
31   {
32     return getOperator() + " (op)";
33   }
34
35   public void setOperator(String JavaDoc operator)
36   {
37     this.operator = operator;
38   }
39
40   public String JavaDoc getOperator()
41   {
42     return operator;
43   }
44
45   public void translate(StringBuffer JavaDoc result, HashMap JavaDoc path2AliasMapping) throws ParseException
46   {
47     appendOpenBrackets(result);
48     getChild(0).translate(result, path2AliasMapping);
49     result.append(operator);
50     getChild(1).translate(result, path2AliasMapping);
51     appendCloseBrackets(result);
52   }
53
54   public Object JavaDoc getValue(MatchingContext context) throws MatchException
55   {
56     ValueExpression left = (ValueExpression) getChild(0);
57     ValueExpression right = (ValueExpression) getChild(1);
58
59     // Concatenation of two functions?
60
if (getOperator().equals("."))
61     {
62       Object JavaDoc leftVal = left.getValue(context);
63       if (!(right instanceof PropertyOrFunctionNode))
64       {
65         throw new MatchException("Can only apply . to a right side function", beginLine, beginColumn);
66       }
67       PropertyOrFunctionNode pof = (PropertyOrFunctionNode) right;
68       try
69       {
70         return pof.evalFunction(leftVal, context);
71       }
72       catch (Exception JavaDoc x)
73       {
74         throw new MatchException(x.getMessage(), beginLine, beginColumn);
75       }
76     }
77
78     Object JavaDoc leftVal = left.getValue(context);
79     Object JavaDoc rightVal = right.getValue(context);
80
81     // First handle null stuff
82
if (leftVal == null || rightVal == null)
83     {
84       return null;
85     }
86
87     // String operation?
88
if ((leftVal instanceof String JavaDoc) || (rightVal instanceof String JavaDoc))
89     {
90       if (getOperator().equals("+")) return leftVal.toString() + rightVal.toString();
91       throw new MatchException("Can not apply " + getOperator() + " to strings", beginLine, beginColumn);
92     }
93
94     // Float op?
95
if ((leftVal instanceof BigDecimal JavaDoc) || (rightVal instanceof BigDecimal JavaDoc))
96     {
97       float l = toBigDecimal(leftVal).floatValue();
98       float r = toBigDecimal(rightVal).floatValue();
99
100       if (getOperator().equals("+")) return new Float JavaDoc(l + r);
101       if (getOperator().equals("-")) return new Float JavaDoc(l - r);
102       if (getOperator().equals("*")) return new Float JavaDoc(l * r);
103       if (getOperator().equals("/")) return new Float JavaDoc(l / r);
104       throw new MatchException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn);
105     }
106     // Float op?
107
if ((leftVal instanceof Float JavaDoc) || (rightVal instanceof Float JavaDoc))
108     {
109       float l = toBigDecimal(leftVal).floatValue();
110       float r = toBigDecimal(rightVal).floatValue();
111
112       if (getOperator().equals("+")) return new Float JavaDoc(l + r);
113       if (getOperator().equals("-")) return new Float JavaDoc(l - r);
114       if (getOperator().equals("*")) return new Float JavaDoc(l * r);
115       if (getOperator().equals("/")) return new Float JavaDoc(l / r);
116       throw new MatchException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn);
117     }
118     // Integer op?
119
if ((leftVal instanceof Integer JavaDoc) || (rightVal instanceof Integer JavaDoc))
120     {
121       int l = toLong(leftVal).intValue();
122       int r = toLong(rightVal).intValue();
123
124       if (getOperator().equals("+")) return new Integer JavaDoc(l + r);
125       if (getOperator().equals("-")) return new Integer JavaDoc(l - r);
126       if (getOperator().equals("*")) return new Integer JavaDoc(l * r);
127       if (getOperator().equals("/")) return new Integer JavaDoc(l / r);
128       if (getOperator().equals("%")) return new Integer JavaDoc(l % r);
129       throw new MatchException("Can not apply " + getOperator() + " to integers", beginLine, beginColumn);
130     }
131     throw new MatchException("Can not apply " + classOnly(leftVal) + getOperator() + classOnly(rightVal), beginLine,
132         beginColumn);
133   }
134
135 }
Popular Tags