KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jexl > parser > ASTEQNode


1 /*
2  * Copyright 2002-2006 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data) {
65         return visitor.visit(this, data);
66     }
67
68     /** {@inheritDoc} */
69     public Object JavaDoc value(JexlContext pc) throws Exception JavaDoc {
70         Object JavaDoc left = ((SimpleNode) jjtGetChild(0)).value(pc);
71         Object JavaDoc right = ((SimpleNode) jjtGetChild(1)).value(pc);
72
73         if (left == null && right == null) {
74             /*
75              * if both are null L == R
76              */

77             return Boolean.TRUE;
78         } else if (left == null || right == null) {
79             /*
80              * we know both aren't null, therefore L != R
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 JavaDoc || left instanceof Double JavaDoc
86                 || right instanceof Float JavaDoc || right instanceof Double JavaDoc) {
87             Double JavaDoc l = Coercion.coerceDouble(left);
88             Double JavaDoc r = Coercion.coerceDouble(right);
89
90             return l.equals(r) ? Boolean.TRUE : Boolean.FALSE;
91         } else if (left instanceof Number JavaDoc || right instanceof Number JavaDoc
92                 || left instanceof Character JavaDoc || right instanceof Character JavaDoc) {
93             return Coercion.coerceLong(left).equals(Coercion.coerceLong(right)) ? Boolean.TRUE
94                     : Boolean.FALSE;
95         } else if (left instanceof Boolean JavaDoc || right instanceof Boolean JavaDoc) {
96             return Coercion.coerceBoolean(left).equals(
97                     Coercion.coerceBoolean(right)) ? Boolean.TRUE
98                     : Boolean.FALSE;
99         } else if (left instanceof java.lang.String JavaDoc || right instanceof String JavaDoc) {
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 }
107
Popular Tags