View Javadoc

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