View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * GT : 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: ASTGTNode.java 480412 2006-11-29 05:11:23Z bayard $
30   */
31  public class ASTGTNode extends SimpleNode {
32      /**
33       * Create the node given an id.
34       * 
35       * @param id node id.
36       */
37      public ASTGTNode(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 ASTGTNode(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).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 || right instanceof String) {
79              String leftString = left.toString();
80              String rightString = right.toString();
81  
82              return leftString.compareTo(rightString) > 0 ? Boolean.TRUE
83                      : Boolean.FALSE;
84          } else if (left instanceof Comparable) {
85              return ((Comparable) left).compareTo(right) > 0 ? Boolean.TRUE
86                      : Boolean.FALSE;
87          } else if (right instanceof Comparable) {
88              return ((Comparable) right).compareTo(left) < 0 ? Boolean.TRUE
89                      : Boolean.FALSE;
90          }
91  
92          throw new Exception("Invalid comparison : GT ");
93      }
94  }