1 package org.apache.velocity.runtime.parser.node;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.runtime.parser.Parser;
23 import org.apache.velocity.util.DuckType;
24
25 /**
26 * Handles <code>arg1 == arg2</code>
27 *
28 * This operator requires that the LHS and RHS are both of the
29 * same Class, both numbers or both coerce-able to strings.
30 *
31 * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
32 * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
33 * @author Nathan Bubna
34 */
35 public class ASTEQNode extends ASTComparisonNode
36 {
37 public ASTEQNode(int id)
38 {
39 super(id);
40 }
41
42 public ASTEQNode(Parser p, int id)
43 {
44 super(p, id);
45 }
46
47 @Override
48 public boolean compareNull(Object left, Object right)
49 {
50 // at least one is null, see if other is null or acts as a null
51 return left == right || DuckType.asNull(left == null ? right : left);
52 }
53
54 public boolean numberTest(int compareResult)
55 {
56 return compareResult == 0;
57 }
58
59 @Override
60 public boolean compareNonNumber(Object left, Object right)
61 {
62 /**
63 * if both are not null, then assume that if one class
64 * is a subclass of the other that we should use the equals operator
65 */
66 if (left.getClass().isAssignableFrom(right.getClass()) ||
67 right.getClass().isAssignableFrom(left.getClass()))
68 {
69 return left.equals(right);
70 }
71
72 // coerce to string, remember getAsString() methods may return null
73 left = DuckType.asString(left);
74 right = DuckType.asString(right);
75 if (left == right)
76 {
77 return true;
78 }
79 else if (left == null || right == null)
80 {
81 return false;
82 }
83 else
84 {
85 return left.equals(right);
86 }
87 }
88
89 }