KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > ASTEqual


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.exp.parser;
21
22 import java.math.BigDecimal JavaDoc;
23
24 import org.apache.cayenne.exp.Expression;
25
26 /**
27  * "Equal To" expression.
28  *
29  * @since 1.1
30  * @author Andrus Adamchik
31  */

32 public class ASTEqual extends ConditionNode {
33     /**
34      * Constructor used by expression parser. Do not invoke directly.
35      */

36     ASTEqual(int id) {
37         super(id);
38     }
39
40     public ASTEqual() {
41         super(ExpressionParserTreeConstants.JJTEQUAL);
42     }
43
44     /**
45      * Creates "Equal To" expression.
46      */

47     public ASTEqual(ASTPath path, Object JavaDoc value) {
48         super(ExpressionParserTreeConstants.JJTEQUAL);
49         jjtAddChild(path, 0);
50         jjtAddChild(new ASTScalar(value), 1);
51     }
52
53     protected Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc {
54         int len = jjtGetNumChildren();
55         if (len != 2) {
56             return Boolean.FALSE;
57         }
58
59         Object JavaDoc o1 = evaluateChild(0, o);
60         Object JavaDoc o2 = evaluateChild(1, o);
61
62         // TODO: maybe we need a comparison "strategy" here, instead of
63
// a switch of all possible cases? ... there were other requests for
64
// more relaxed type-unsafe comparison (e.g. numbers to strings)
65

66         if (o1 == null && o2 == null) {
67             return Boolean.TRUE;
68         }
69         else if (o1 != null) {
70             // BigDecimals must be compared using compareTo (
71
// see CAY-280 and BigDecimal.equals JavaDoc)
72
if (o1 instanceof BigDecimal JavaDoc) {
73                 if (o2 instanceof BigDecimal JavaDoc) {
74                     return ((BigDecimal JavaDoc) o1).compareTo((BigDecimal JavaDoc) o2) == 0
75                             ? Boolean.TRUE
76                             : Boolean.FALSE;
77                 }
78
79                 return Boolean.FALSE;
80             }
81
82             return o1.equals(o2) ? Boolean.TRUE : Boolean.FALSE;
83         }
84         else {
85             return Boolean.FALSE;
86         }
87     }
88
89     /**
90      * Creates a copy of this expression node, without copying children.
91      */

92     public Expression shallowCopy() {
93         return new ASTEqual(id);
94     }
95
96     protected String JavaDoc getExpressionOperator(int index) {
97         return "=";
98     }
99
100     public int getType() {
101         return Expression.EQUAL_TO;
102     }
103 }
104
Popular Tags