KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.commons.jexl.parser;
18
19 import org.apache.commons.jexl.JexlContext;
20 import org.apache.commons.jexl.util.Coercion;
21
22 /**
23  * LT : a < b.
24  *
25  * Follows A.3.6.1 of the JSTL 1.0 specification
26  *
27  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
28  * @author <a HREF="mailto:proyal@apache.org">Peter Royal</a>
29  * @version $Id: ASTLTNode.java 398203 2006-04-29 16:44:55Z dion $
30  */

31 public class ASTLTNode extends SimpleNode {
32     /**
33      * Create the node given an id.
34      *
35      * @param id node id.
36      */

37     public ASTLTNode(int id) {
38         super(id);
39     }
40
41     /**
42      * Create a node with the given parser and id.
43      *
44      * @param p a parser.
45      * @param id node id.
46      */

47     public ASTLTNode(Parser p, int id) {
48         super(p, id);
49     }
50
51     /** {@inheritDoc} */
52     public Object JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data) {
53         return visitor.visit(this, data);
54     }
55
56     /** {@inheritDoc} */
57     public Object JavaDoc value(JexlContext jc) throws Exception JavaDoc {
58         /*
59          * now get the values
60          */

61
62         Object JavaDoc left = ((SimpleNode) jjtGetChild(0)).value(jc);
63         Object JavaDoc right = ((SimpleNode) jjtGetChild(1)).value(jc);
64
65         if ((left == right) || (left == null) || (right == null)) {
66             return Boolean.FALSE;
67         } else if (Coercion.isFloatingPoint(left)
68                 || Coercion.isFloatingPoint(right)) {
69             double leftDouble = Coercion.coerceDouble(left).doubleValue();
70             double rightDouble = Coercion.coerceDouble(right).doubleValue();
71
72             return leftDouble < rightDouble ? Boolean.TRUE : Boolean.FALSE;
73         } else if (Coercion.isNumberable(left) || Coercion.isNumberable(right)) {
74             long leftLong = Coercion.coerceLong(left).longValue();
75             long rightLong = Coercion.coerceLong(right).longValue();
76
77             return leftLong < rightLong ? Boolean.TRUE : Boolean.FALSE;
78         } else if (left instanceof String JavaDoc || right instanceof String JavaDoc) {
79             String JavaDoc leftString = left.toString();
80             String JavaDoc rightString = right.toString();
81
82             return leftString.compareTo(rightString) < 0 ? Boolean.TRUE
83                     : Boolean.FALSE;
84         } else if (left instanceof Comparable JavaDoc) {
85             return ((Comparable JavaDoc) left).compareTo(right) < 0 ? Boolean.TRUE
86                     : Boolean.FALSE;
87         } else if (right instanceof Comparable JavaDoc) {
88             return ((Comparable JavaDoc) right).compareTo(left) > 0 ? Boolean.TRUE
89                     : Boolean.FALSE;
90         }
91
92         throw new Exception JavaDoc("Invalid comparison : LT ");
93     }
94 }
95
Popular Tags