01: /*
02: * Copyright 2002-2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 jjtAccept(ParserVisitor visitor, Object data) {
53: return visitor.visit(this , data);
54: }
55:
56: /** {@inheritDoc} */
57: public Object value(JexlContext jc) throws Exception {
58: /*
59: * now get the values
60: */
61:
62: Object left = ((SimpleNode) jjtGetChild(0)).value(jc);
63: Object 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)
70: .doubleValue();
71: double rightDouble = Coercion.coerceDouble(right)
72: .doubleValue();
73:
74: return leftDouble < rightDouble ? Boolean.TRUE
75: : Boolean.FALSE;
76: } else if (Coercion.isNumberable(left)
77: || Coercion.isNumberable(right)) {
78: long leftLong = Coercion.coerceLong(left).longValue();
79: long rightLong = Coercion.coerceLong(right).longValue();
80:
81: return leftLong < rightLong ? Boolean.TRUE : Boolean.FALSE;
82: } else if (left instanceof String || right instanceof String) {
83: String leftString = left.toString();
84: String rightString = right.toString();
85:
86: return leftString.compareTo(rightString) < 0 ? Boolean.TRUE
87: : Boolean.FALSE;
88: } else if (left instanceof Comparable) {
89: return ((Comparable) left).compareTo(right) < 0 ? Boolean.TRUE
90: : Boolean.FALSE;
91: } else if (right instanceof Comparable) {
92: return ((Comparable) right).compareTo(left) > 0 ? Boolean.TRUE
93: : Boolean.FALSE;
94: }
95:
96: throw new Exception("Invalid comparison : LT ");
97: }
98: }
|