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.util.Coercion;
  18. import org.apache.commons.jexl.JexlContext;
  19. /**
  20. * Subtraction
  21. *
  22. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  23. * @author <a href="mailto:mhw@kremvax.net">Mark H. Wilkinson</a>
  24. * @version $Id: ASTSubtractNode.java,v 1.5 2004/02/28 13:45:20 yoavs Exp $
  25. */
  26. public class ASTSubtractNode extends SimpleNode
  27. {
  28. public ASTSubtractNode(int id)
  29. {
  30. super(id);
  31. }
  32. public ASTSubtractNode(Parser p, int id)
  33. {
  34. super(p, id);
  35. }
  36. public Object value(JexlContext context)
  37. throws Exception
  38. {
  39. Object left = ((SimpleNode) jjtGetChild(0)).value(context);
  40. Object right = ((SimpleNode) jjtGetChild(1)).value(context);
  41. /*
  42. * the spec says 'and', I think 'or'
  43. */
  44. if (left == null && right == null)
  45. return new Integer(0);
  46. /*
  47. * if anything is float, double or string with ( "." | "E" | "e")
  48. * coerce all to doubles and do it
  49. */
  50. if ( left instanceof Float || left instanceof Double
  51. || right instanceof Float || right instanceof Double
  52. || ( left instanceof String
  53. && ( ((String) left).indexOf(".") != -1 ||
  54. ((String) left).indexOf("e") != -1 ||
  55. ((String) left).indexOf("E") != -1 )
  56. )
  57. || ( right instanceof String
  58. && ( ((String) right).indexOf(".") != -1 ||
  59. ((String) right).indexOf("e") != -1 ||
  60. ((String) right).indexOf("E") != -1 )
  61. )
  62. )
  63. {
  64. Double l = Coercion.coerceDouble(left);
  65. Double r = Coercion.coerceDouble(right);
  66. return new Double(l.doubleValue() - r.doubleValue());
  67. }
  68. /*
  69. * otherwise to longs with thee!
  70. */
  71. Long l = Coercion.coerceLong(left);
  72. Long r = Coercion.coerceLong(right);
  73. /*
  74. * TODO - this is actually wrong - JSTL says to return a Long
  75. * but we have problems where you have something like
  76. *
  77. * foo.bar( a - b )
  78. *
  79. * where bar wants an int...
  80. *
  81. */
  82. long v = l.longValue() - r.longValue();
  83. if ( left instanceof Integer && right instanceof Integer )
  84. {
  85. return new Integer((int) v);
  86. }
  87. else
  88. {
  89. return new Long(v);
  90. }
  91. }
  92. /** Accept the visitor. **/
  93. public Object jjtAccept(ParserVisitor visitor, Object data)
  94. {
  95. return visitor.visit(this, data);
  96. }
  97. }