001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jexl.parser;
017:
018: import org.apache.commons.jexl.JexlContext;
019: import org.apache.commons.jexl.util.Coercion;
020:
021: /**
022: * Represents equality between values.
023: *
024: * If the values are of the same class, .equals() is used.
025: *
026: * If either value is a {@link Float} or {@link Double} (but both are not the same class),
027: * the values are coerced to {@link Double}s before comparing.
028: *
029: * If either value is a {@link Number} or {@link Character} (but both are not the same class),
030: * the values are coerced to {@link Long}s before comparing.
031: *
032: * If either value is a {@link Boolean} (but both are not the same class),
033: * the values are coerced to {@link Boolean}s before comparing.
034: *
035: * If either value is a {@link String} (but both are not the same class),
036: * toString() is called on both before comparing.
037: *
038: * Otherwise left.equals(right) is returned.
039: *
040: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
041: * @version $Id: ASTEQNode.java 398190 2006-04-29 16:04:10Z dion $
042: */
043: public class ASTEQNode extends SimpleNode {
044: /**
045: * Create the node given an id.
046: *
047: * @param id node id.
048: */
049: public ASTEQNode(int id) {
050: super (id);
051: }
052:
053: /**
054: * Create a node with the given parser and id.
055: *
056: * @param p a parser.
057: * @param id node id.
058: */
059: public ASTEQNode(Parser p, int id) {
060: super (p, id);
061: }
062:
063: /** {@inheritDoc} */
064: public Object jjtAccept(ParserVisitor visitor, Object data) {
065: return visitor.visit(this , data);
066: }
067:
068: /** {@inheritDoc} */
069: public Object value(JexlContext pc) throws Exception {
070: Object left = ((SimpleNode) jjtGetChild(0)).value(pc);
071: Object right = ((SimpleNode) jjtGetChild(1)).value(pc);
072:
073: if (left == null && right == null) {
074: /*
075: * if both are null L == R
076: */
077: return Boolean.TRUE;
078: } else if (left == null || right == null) {
079: /*
080: * we know both aren't null, therefore L != R
081: */
082: return Boolean.FALSE;
083: } else if (left.getClass().equals(right.getClass())) {
084: return left.equals(right) ? Boolean.TRUE : Boolean.FALSE;
085: } else if (left instanceof Float || left instanceof Double
086: || right instanceof Float || right instanceof Double) {
087: Double l = Coercion.coerceDouble(left);
088: Double r = Coercion.coerceDouble(right);
089:
090: return l.equals(r) ? Boolean.TRUE : Boolean.FALSE;
091: } else if (left instanceof Number || right instanceof Number
092: || left instanceof Character
093: || right instanceof Character) {
094: return Coercion.coerceLong(left).equals(
095: Coercion.coerceLong(right)) ? Boolean.TRUE
096: : Boolean.FALSE;
097: } else if (left instanceof Boolean || right instanceof Boolean) {
098: return Coercion.coerceBoolean(left).equals(
099: Coercion.coerceBoolean(right)) ? Boolean.TRUE
100: : Boolean.FALSE;
101: } else if (left instanceof java.lang.String
102: || right instanceof String) {
103: return left.toString().equals(right.toString()) ? Boolean.TRUE
104: : Boolean.FALSE;
105: }
106:
107: return left.equals(right) ? Boolean.TRUE : Boolean.FALSE;
108: }
109: }
|