1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.apache.commons.jexl.parser;
17  
18  import org.apache.commons.jexl.JexlContext;
19  import org.apache.commons.jexl.util.Coercion;
20  
21  /***
22   * Represents equality between values.
23   * 
24   * If the values are of the same class, .equals() is used.
25   * 
26   * If either value is a {@link Float} or {@link Double} (but both are not the same class),
27   * the values are coerced to {@link Double}s before comparing.
28   * 
29   * If either value is a {@link Number} or {@link Character} (but both are not the same class),
30   * the values are coerced to {@link Long}s before comparing.
31   *
32   * If either value is a {@link Boolean} (but both are not the same class),
33   * the values are coerced to {@link Boolean}s before comparing.
34   * 
35   * If either value is a {@link String} (but both are not the same class),
36   * toString() is called on both before comparing.
37   * 
38   * Otherwise left.equals(right) is returned.
39   * 
40   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
41   * @version $Id: ASTEQNode.java 398190 2006-04-29 16:04:10Z dion $
42   */
43  public class ASTEQNode extends SimpleNode {
44      /***
45       * Create the node given an id.
46       * 
47       * @param id node id.
48       */
49      public ASTEQNode(int id) {
50          super(id);
51      }
52  
53      /***
54       * Create a node with the given parser and id.
55       * 
56       * @param p a parser.
57       * @param id node id.
58       */
59      public ASTEQNode(Parser p, int id) {
60          super(p, id);
61      }
62  
63      /*** {@inheritDoc} */
64      public Object jjtAccept(ParserVisitor visitor, Object data) {
65          return visitor.visit(this, data);
66      }
67  
68      /*** {@inheritDoc} */
69      public Object value(JexlContext pc) throws Exception {
70          Object left = ((SimpleNode) jjtGetChild(0)).value(pc);
71          Object right = ((SimpleNode) jjtGetChild(1)).value(pc);
72  
73          if (left == null && right == null) {
74              
75  
76  
77              return Boolean.TRUE;
78          } else if (left == null || right == null) {
79              
80  
81  
82              return Boolean.FALSE;
83          } else if (left.getClass().equals(right.getClass())) {
84              return left.equals(right) ? Boolean.TRUE : Boolean.FALSE;
85          } else if (left instanceof Float || left instanceof Double
86                  || right instanceof Float || right instanceof Double) {
87              Double l = Coercion.coerceDouble(left);
88              Double r = Coercion.coerceDouble(right);
89  
90              return l.equals(r) ? Boolean.TRUE : Boolean.FALSE;
91          } else if (left instanceof Number || right instanceof Number
92                  || left instanceof Character || right instanceof Character) {
93              return Coercion.coerceLong(left).equals(Coercion.coerceLong(right)) ? Boolean.TRUE
94                      : Boolean.FALSE;
95          } else if (left instanceof Boolean || right instanceof Boolean) {
96              return Coercion.coerceBoolean(left).equals(
97                      Coercion.coerceBoolean(right)) ? Boolean.TRUE
98                      : Boolean.FALSE;
99          } else if (left instanceof java.lang.String || right instanceof String) {
100             return left.toString().equals(right.toString()) ? Boolean.TRUE
101                     : Boolean.FALSE;
102         }
103 
104         return left.equals(right) ? Boolean.TRUE : Boolean.FALSE;
105     }
106 }