commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser ASTAddNode.java ASTMulNode.java ASTSubtractNode.java
Date Fri, 17 May 2002 12:13:22 GMT
geirm       02/05/17 05:13:22

  Modified:    jexl/src/java/org/apache/commons/jexl/parser ASTAddNode.java
                        ASTMulNode.java ASTSubtractNode.java
  Log:
  Make +, - and * 'more conformant' to the spec.  We lost string concat for
  the moment ( "foo" + "bar" == "foobar") but we might just revisit this with
  a new operator.
  
  Revision  Changes    Path
  1.2       +36 -9     jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
  
  Index: ASTAddNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTAddNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ASTAddNode.java	26 Apr 2002 04:23:14 -0000	1.1
  +++ ASTAddNode.java	17 May 2002 12:13:22 -0000	1.2
  @@ -55,12 +55,13 @@
   package org.apache.commons.jexl.parser;
   
   import org.apache.commons.jexl.JexlContext;
  +import org.apache.commons.jexl.util.Coercion;
   
   /**
    *  Addition : either integer addition or string concatenation
    *
    *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  - *  @version $Id: ASTAddNode.java,v 1.1 2002/04/26 04:23:14 geirm Exp $
  + *  @version $Id: ASTAddNode.java,v 1.2 2002/05/17 12:13:22 geirm Exp $
    */
   public class ASTAddNode extends SimpleNode
   {
  @@ -87,17 +88,43 @@
           Object left = ((SimpleNode) jjtGetChild(0)).value(context);
           Object right = ((SimpleNode) jjtGetChild(1)).value(context);
   
  -        if (left instanceof String || right instanceof String)
  +        /*
  +         *  the spec says 'and', I think 'or'
  +         */
  +        if (left == null && right == null)
  +            return new Integer(0);
  +
  +        /*
  +         *  if anything is float, double or string with ( "." | "E" | "e")
  +         *  coerce all to doubles and do it
  +         */
  +        if ( left instanceof Float || left instanceof Double
  +            || right instanceof Float || right instanceof Double
  +            || (  left instanceof String
  +                  && (  ((String) left).indexOf(".") != -1 ||
  +                        ((String) left).indexOf("e") != -1 ||
  +                        ((String) left).indexOf("E") != -1 )
  +               )
  +            || (  right instanceof String
  +                  && (  ((String) right).indexOf(".") != -1 ||
  +                        ((String) right).indexOf("e") != -1 ||
  +                        ((String) right).indexOf("E") != -1 )
  +               )
  +            )
           {
  -            return left.toString() + right.toString();
  -        }
  -        else if (left instanceof Integer && right instanceof Integer)
  -        {
  -            int res = ((Integer) left).intValue() + ((Integer) right).intValue();
  +            Double l = Coercion.coerceDouble(left);
  +            Double r = Coercion.coerceDouble(right);
   
  -            return new Integer(res);
  +            return new Double( l.doubleValue() + r.doubleValue() );
           }
   
  -        return null;
  +        /*
  +         * otherwise to longs with thee!
  +         */
  +
  +        Long l = Coercion.coerceLong(left);
  +        Long r = Coercion.coerceLong(right);
  +
  +        return new Long(l.longValue() + r.longValue());
       }
   }
  
  
  
  1.2       +126 -13   jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
  
  Index: ASTMulNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTMulNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ASTMulNode.java	26 Apr 2002 04:23:14 -0000	1.1
  +++ ASTMulNode.java	17 May 2002 12:13:22 -0000	1.2
  @@ -1,19 +1,132 @@
  -/* Generated By:JJTree: Do not edit this line. ASTMulNode.java */
  +/*
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 2002 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution, if
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowlegement may appear in the software itself,
  + *    if and wherever such third-party acknowlegements normally appear.
  + *
  + * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
  + *    Foundation" must not be used to endorse or promote products derived
  + *    from this software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache"
  + *    nor may "Apache" appear in their names without prior written
  + *    permission of the Apache Group.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + */
   
   package org.apache.commons.jexl.parser;
   
  -public class ASTMulNode extends SimpleNode {
  -  public ASTMulNode(int id) {
  -    super(id);
  -  }
  -
  -  public ASTMulNode(Parser p, int id) {
  -    super(p, id);
  -  }
  +import org.apache.commons.jexl.JexlContext;
  +import org.apache.commons.jexl.util.Coercion;
   
  +/**
  + *  Multiplication
  + *
  + *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  + *  @version $Id: ASTMulNode.java,v 1.2 2002/05/17 12:13:22 geirm Exp $
  + */
  +public class ASTMulNode extends SimpleNode
  +{
  +    public ASTMulNode(int id)
  +    {
  +        super(id);
  +    }
  +
  +    public ASTMulNode(Parser p, int id)
  +    {
  +        super(p, id);
  +    }
  +
  +
  +    /** Accept the visitor. **/
  +    public Object jjtAccept(ParserVisitor visitor, Object data)
  +    {
  +        return visitor.visit(this, data);
  +    }
  +
  +    public Object value(JexlContext context)
  +        throws Exception
  +    {
  +        Object left = ((SimpleNode) jjtGetChild(0)).value(context);
  +        Object right = ((SimpleNode) jjtGetChild(1)).value(context);
  +
  +        /*
  +         *  the spec says 'and', I think 'or'
  +         */
  +        if (left == null && right == null)
  +            return new Integer(0);
  +
  +        /*
  +         *  if anything is float, double or string with ( "." | "E" | "e")
  +         *  coerce all to doubles and do it
  +         */
  +        if ( left instanceof Float || left instanceof Double
  +            || right instanceof Float || right instanceof Double
  +            || (  left instanceof String
  +                  && (  ((String) left).indexOf(".") != -1 ||
  +                        ((String) left).indexOf("e") != -1 ||
  +                        ((String) left).indexOf("E") != -1 )
  +               )
  +            || (  right instanceof String
  +                  && (  ((String) right).indexOf(".") != -1 ||
  +                        ((String) right).indexOf("e") != -1 ||
  +                        ((String) right).indexOf("E") != -1 )
  +               )
  +            )
  +        {
  +            Double l = Coercion.coerceDouble(left);
  +            Double r = Coercion.coerceDouble(right);
  +
  +            return new Double( l.doubleValue() * r.doubleValue() );
  +        }
  +
  +        /*
  +         * otherwise to longs with thee!
  +         */
  +
  +        Long l = Coercion.coerceLong(left);
  +        Long r = Coercion.coerceLong(right);
  +
  +        return new Long(l.longValue() * r.longValue());
  +    }
   
  -  /** Accept the visitor. **/
  -  public Object jjtAccept(ParserVisitor visitor, Object data) {
  -    return visitor.visit(this, data);
  -  }
   }
  +
  
  
  
  1.2       +123 -13   jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
  
  Index: ASTSubtractNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jexl/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ASTSubtractNode.java	26 Apr 2002 04:23:14 -0000	1.1
  +++ ASTSubtractNode.java	17 May 2002 12:13:22 -0000	1.2
  @@ -1,19 +1,129 @@
  -/* Generated By:JJTree: Do not edit this line. ASTSubtractNode.java */
  +/*
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 2002 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution, if
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowlegement may appear in the software itself,
  + *    if and wherever such third-party acknowlegements normally appear.
  + *
  + * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
  + *    Foundation" must not be used to endorse or promote products derived
  + *    from this software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache"
  + *    nor may "Apache" appear in their names without prior written
  + *    permission of the Apache Group.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + */
   
   package org.apache.commons.jexl.parser;
   
  -public class ASTSubtractNode extends SimpleNode {
  -  public ASTSubtractNode(int id) {
  -    super(id);
  -  }
  -
  -  public ASTSubtractNode(Parser p, int id) {
  -    super(p, id);
  -  }
  +import org.apache.commons.jexl.util.Coercion;
  +import org.apache.commons.jexl.JexlContext;
   
  +/**
  + *  Subtraction
  + *
  + *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  + *  @version $Id: ASTSubtractNode.java,v 1.2 2002/05/17 12:13:22 geirm Exp $
  + */
  +public class ASTSubtractNode extends SimpleNode
  +{
  +    public ASTSubtractNode(int id)
  +    {
  +        super(id);
  +    }
   
  -  /** Accept the visitor. **/
  -  public Object jjtAccept(ParserVisitor visitor, Object data) {
  -    return visitor.visit(this, data);
  -  }
  +    public ASTSubtractNode(Parser p, int id)
  +    {
  +        super(p, id);
  +    }
  +
  +    public Object value(JexlContext context)
  +        throws Exception
  +    {
  +        Object left = ((SimpleNode) jjtGetChild(0)).value(context);
  +        Object right = ((SimpleNode) jjtGetChild(1)).value(context);
  +
  +        /*
  +         *  the spec says 'and', I think 'or'
  +         */
  +        if (left == null && right == null)
  +            return new Integer(0);
  +
  +        /*
  +         *  if anything is float, double or string with ( "." | "E" | "e")
  +         *  coerce all to doubles and do it
  +         */
  +        if ( left instanceof Float || left instanceof Double
  +            || right instanceof Float || right instanceof Double
  +            || (  left instanceof String
  +                 && (  ((String) left).indexOf(".") != -1 ||
  +                    ((String) left).indexOf("e") != -1 ||
  +                    ((String) left).indexOf("E") != -1 )
  +            )
  +            || (  right instanceof String
  +                && (  ((String) right).indexOf(".") != -1 ||
  +                ((String) right).indexOf("e") != -1 ||
  +                ((String) right).indexOf("E") != -1 )
  +               )
  +        )
  +        {
  +            Double l = Coercion.coerceDouble(left);
  +            Double r = Coercion.coerceDouble(right);
  +
  +            return new Double(l.doubleValue() - r.doubleValue());
  +        }
  +
  +        /*
  +         * otherwise to longs with thee!
  +         */
  +
  +        Long l = Coercion.coerceLong(left);
  +        Long r = Coercion.coerceLong(right);
  +
  +        return new Long(l.longValue() - r.longValue());
  +
  +    }
  +    /** Accept the visitor. **/
  +    public Object jjtAccept(ParserVisitor visitor, Object data)
  +    {
  +        return visitor.visit(this, data);
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message