1. /*
  2. * Copyright 2002,2004 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. package org.apache.commons.jexl.parser;
  17. import org.apache.commons.jexl.JexlContext;
  18. import org.apache.commons.jexl.util.Coercion;
  19. /**
  20. * LT : a < b
  21. *
  22. * Follows A.3.6.1 of the JSTL 1.0 specification
  23. *
  24. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  25. * @author <a href="mailto:proyal@apache.org">Peter Royal</a>
  26. * @version $Id: ASTLTNode.java,v 1.5 2004/02/28 13:45:20 yoavs Exp $
  27. */
  28. public class ASTLTNode extends SimpleNode
  29. {
  30. public ASTLTNode(int id)
  31. {
  32. super(id);
  33. }
  34. public ASTLTNode(Parser p, int id)
  35. {
  36. super(p, id);
  37. }
  38. /** Accept the visitor. **/
  39. public Object jjtAccept(ParserVisitor visitor, Object data)
  40. {
  41. return visitor.visit(this, data);
  42. }
  43. public Object value(JexlContext jc)
  44. throws Exception
  45. {
  46. /*
  47. * now get the values
  48. */
  49. Object left = ( (SimpleNode) jjtGetChild(0)).value(jc);
  50. Object right = ( (SimpleNode) jjtGetChild(1)).value(jc);
  51. if( ( left == right ) || ( left == null ) || ( right == null ) )
  52. {
  53. return Boolean.FALSE;
  54. }
  55. else if( Coercion.isFloatingPoint( left ) || Coercion.isFloatingPoint( right ) )
  56. {
  57. double leftDouble = Coercion.coerceDouble( left ).doubleValue();
  58. double rightDouble = Coercion.coerceDouble( right ).doubleValue();
  59. return leftDouble < rightDouble
  60. ? Boolean.TRUE
  61. : Boolean.FALSE;
  62. }
  63. else if( Coercion.isNumberable( left ) || Coercion.isNumberable( right ) )
  64. {
  65. long leftLong = Coercion.coerceLong( left ).longValue();
  66. long rightLong = Coercion.coerceLong( right ).longValue();
  67. return leftLong < rightLong
  68. ? Boolean.TRUE
  69. : Boolean.FALSE;
  70. }
  71. else if( left instanceof String || right instanceof String )
  72. {
  73. String leftString = left.toString();
  74. String rightString = right.toString();
  75. return leftString.compareTo( rightString ) < 0
  76. ? Boolean.TRUE
  77. : Boolean.FALSE;
  78. }
  79. else if( left instanceof Comparable )
  80. {
  81. return ( (Comparable)left ).compareTo( right ) < 0
  82. ? Boolean.TRUE
  83. : Boolean.FALSE;
  84. }
  85. else if( right instanceof Comparable )
  86. {
  87. return ( (Comparable)right ).compareTo( left ) > 0
  88. ? Boolean.TRUE
  89. : Boolean.FALSE;
  90. }
  91. throw new Exception("Invalid comparison : LT ");
  92. }
  93. }