1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl.parser;
18
19 import org.apache.commons.jexl.util.Coercion;
20 import org.apache.commons.jexl.JexlContext;
21
22 /***
23 * Subtraction
24 *
25 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
26 * @author <a href="mailto:mhw@kremvax.net">Mark H. Wilkinson</a>
27 * @version $Id: ASTSubtractNode.java,v 1.5 2004/02/28 13:45:20 yoavs Exp $
28 */
29 public class ASTSubtractNode extends SimpleNode
30 {
31 public ASTSubtractNode(int id)
32 {
33 super(id);
34 }
35
36 public ASTSubtractNode(Parser p, int id)
37 {
38 super(p, id);
39 }
40
41 public Object value(JexlContext context)
42 throws Exception
43 {
44 Object left = ((SimpleNode) jjtGetChild(0)).value(context);
45 Object right = ((SimpleNode) jjtGetChild(1)).value(context);
46
47
48
49
50 if (left == null && right == null)
51 return new Integer(0);
52
53
54
55
56
57 if ( left instanceof Float || left instanceof Double
58 || right instanceof Float || right instanceof Double
59 || ( left instanceof String
60 && ( ((String) left).indexOf(".") != -1 ||
61 ((String) left).indexOf("e") != -1 ||
62 ((String) left).indexOf("E") != -1 )
63 )
64 || ( right instanceof String
65 && ( ((String) right).indexOf(".") != -1 ||
66 ((String) right).indexOf("e") != -1 ||
67 ((String) right).indexOf("E") != -1 )
68 )
69 )
70 {
71 Double l = Coercion.coerceDouble(left);
72 Double r = Coercion.coerceDouble(right);
73
74 return new Double(l.doubleValue() - r.doubleValue());
75 }
76
77
78
79
80
81 Long l = Coercion.coerceLong(left);
82 Long r = Coercion.coerceLong(right);
83
84
85
86
87
88
89
90
91
92
93 long v = l.longValue() - r.longValue();
94
95 if ( left instanceof Integer && right instanceof Integer )
96 {
97 return new Integer((int) v);
98 }
99 else
100 {
101 return new Long(v);
102 }
103 }
104 /*** Accept the visitor. **/
105 public Object jjtAccept(ParserVisitor visitor, Object data)
106 {
107 return visitor.visit(this, data);
108 }
109 }