You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Henning P. Schmiedehausen" <ma...@hometree.net> on 2001/06/19 18:27:28 UTC

[PATCH] Better == and != behaviour with various int types

Hi,

ok, I admit, I'm fed up with having to jump through loops if I want to
check the values of various object methods that do not return Integer
but Byte or Short values. This especially a problem with the Torque
generated Peer classes from Turbine, which e.g. generate methods that
return Byte objects for SQL columns that contain SMALLINTs.

I also consider (though I know that this is not "pure Java" but more a
script language like approach like perl) the ability to use an Integer
or Integer-like value in an #if() statement a huge simplification for
writing macros and small scripts. Velocity is IMHO more a perl like
language than a Java-like language (flame me, if you want. ;-) )

So the following two patch-sets against velocity-1.1. The first adds a
small static helper to get integer values; didn't find a place where
it really fits so I just put it into its own class and two small
patches against ASTEQNode and ASTNENode where the comparisation of the
values is extended so that you can easily compare Byte to Integer
values (that's where I need it).

The second one extends the if(<variable>) expression check in
ASTReference so that a value, if it is not a boolean is checked,
whether it is an Integer-like value and if yes, then it is checked,
whether it is == 0 or not. If it is == 0, then I return false, else
true. If it is not an integer-like value, I return true (current
behaviour).

Please check; if you like it, please apply to the velocity source. I
consider both patches hugely useful.

Caveat: There may be some problems applying the patches, because the
source of Velocity uses DOS-line endings (^M) and I converted my
source tree of Velocity 1.1 to Unix line endings first (no ^M). If you
have problems, please contact me via E-Mail and I will prepare a
DOS-compatible patch.

	Regards
		Henning


first patch-set:

diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/IntegerValue.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/IntegerValue.java
--- velocity-1.1/src/java/org/apache/velocity/runtime/parser/IntegerValue.java	Thu Jan  1 01:00:00 1970
+++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/IntegerValue.java	Tue Jun 19 18:14:12 2001
@@ -0,0 +1,85 @@
+package org.apache.velocity.runtime;
+
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000-2001 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", "Velocity", 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/>.
+ */
+
+import java.lang.Integer;
+import java.lang.reflect.Method;
+
+/**
+ * This is a small helper, that tries to find a intValue() method 
+ * inside an object, and if it exists, returns this method. Didn't find
+ * a better place for this, if you know one, move it there. 
+ *
+ * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+
+public class IntegerValue
+{
+  /**
+   * Checks whether an object contains an intValue method and
+   * returns this value if it exists.
+   *
+   * @param o   An Object
+   * @return    The Integer value
+   *
+   */
+    static public Integer getInteger(Object o)
+        throws Exception
+    {
+        Method m = o.getClass().getMethod("intValue", null);
+        return (Integer)m.invoke(o, null);
+    }
+}
diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
--- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java	Sat Jun 16 19:50:27 2001
+++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java	Tue Jun 19 17:46:11 2001
@@ -57,6 +57,7 @@
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.runtime.parser.Parser;
 import org.apache.velocity.runtime.Runtime;
+import org.apache.velocity.runtime.IntegerValue;
 
 import org.apache.velocity.exception.MethodInvocationException;
 
@@ -138,6 +139,17 @@
         }
         else
         {
+            try 
+            {
+                Integer leftInt  = IntegerValue.getInteger(left);
+                Integer rightInt = IntegerValue.getInteger(right);
+                
+                return leftInt.equals(rightInt);
+            } 
+            catch(Exception e)
+            {
+            }
+
             Runtime.error("Error in evaluation of == expression."
                           + " Both arguments must be of the same Class."
                           + " Currently left = " + left.getClass() + ", right = " 
diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
--- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java	Sat Jun 16 19:50:27 2001
+++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java	Tue Jun 19 17:46:36 2001
@@ -57,6 +57,7 @@
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.runtime.parser.Parser;
 import org.apache.velocity.runtime.Runtime;
+import org.apache.velocity.runtime.IntegerValue;
 
 import org.apache.velocity.exception.MethodInvocationException;
 
@@ -111,6 +112,17 @@
         }
         else
         {
+            try 
+            {
+                Integer leftInt  = IntegerValue.getInteger(left);
+                Integer rightInt = IntegerValue.getInteger(right);
+                
+                return !(leftInt.equals(rightInt));
+            } 
+            catch(Exception e)
+            {
+            }
+
             Runtime.error("Error in evaluation of != expression."
                           + " Both arguments must be of the same Class."
                           + " Currently left = " + left.getClass() + ", right = " 


second patch set:

diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
--- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java	Sat Jun 16 19:50:27 2001
+++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java	Tue Jun 19 18:12:48 2001
@@ -62,6 +62,7 @@
 
 import org.apache.velocity.context.Context;
 import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.runtime.IntegerValue;
 import org.apache.velocity.runtime.Runtime;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.exception.ReferenceException;
@@ -83,6 +84,7 @@
  * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
  * @author <a href="mailto:kjohnson@transparent.com>Kent Johnson</a>
+ * @author <a href="mailto:hps@intermeta.de>Henning P. Schmiedehausen</a>
  * @version $Id: ASTReference.java,v 1.33 2001/05/20 19:50:06 geirm Exp $ 
 */
 public class ASTReference extends SimpleNode
@@ -328,7 +330,20 @@
                 return false;
         }
         else
+        {
+          try
+          {
+            Integer val = IntegerValue.getInteger(value);
+            if(val.intValue() > 0)
+              return true;
+            else
+              return false;
+          }
+          catch (Exception e)
+          {
+          }
             return true;
+        }
     }
 
     public Object value( InternalContextAdapter context)




-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

Re: [PATCH] Better == and != behaviour with various int types

Posted by "Henning P. Schmiedehausen" <ma...@hometree.net>.
"Geir Magnusson Jr." <ge...@optonline.net> writes:

>First, it looks like true is if >0, false otherwise.

>Second, this will actually change the semantics of #if( $foo ) in a big
>way - if $foo is valid and is possibly integer valued, it can return
>false, which is not what anyone expects.

Thats a good argument. But how about the other change? != and == ?

This is IMHO a big win, because you can easily do

#if($data.Item > 0) 

where getItem() returns e.g. a Byte. At the moment you get a run time
error in the log.

>p.s.  Came kinda close with that Perl comment  :->

=:-)

	Regards
		Henning


-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
D-91054 Buckenhof     Fax.: 09131 / 50654-20   

Re: [PATCH] Better == and != behaviour with various int types

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
First, it looks like true is if >0, false otherwise.

Second, this will actually change the semantics of #if( $foo ) in a big
way - if $foo is valid and is possibly integer valued, it can return
false, which is not what anyone expects.

Why not have us work in a more normal #if( $foo > 0 ) type of thing...?

We should clarify and summarize the changes this will make.

(I am damn sensitive to #if() issues due to recent VM work.. :)

geir

p.s.  Came kinda close with that Perl comment  :->



"Henning P. Schmiedehausen" wrote:
> 
> Hi,
> 
> ok, I admit, I'm fed up with having to jump through loops if I want to
> check the values of various object methods that do not return Integer
> but Byte or Short values. This especially a problem with the Torque
> generated Peer classes from Turbine, which e.g. generate methods that
> return Byte objects for SQL columns that contain SMALLINTs.
> 
> I also consider (though I know that this is not "pure Java" but more a
> script language like approach like perl) the ability to use an Integer
> or Integer-like value in an #if() statement a huge simplification for
> writing macros and small scripts. Velocity is IMHO more a perl like
> language than a Java-like language (flame me, if you want. ;-) )
> 
> So the following two patch-sets against velocity-1.1. The first adds a
> small static helper to get integer values; didn't find a place where
> it really fits so I just put it into its own class and two small
> patches against ASTEQNode and ASTNENode where the comparisation of the
> values is extended so that you can easily compare Byte to Integer
> values (that's where I need it).
> 
> The second one extends the if(<variable>) expression check in
> ASTReference so that a value, if it is not a boolean is checked,
> whether it is an Integer-like value and if yes, then it is checked,
> whether it is == 0 or not. If it is == 0, then I return false, else
> true. If it is not an integer-like value, I return true (current
> behaviour).
> 
> Please check; if you like it, please apply to the velocity source. I
> consider both patches hugely useful.
> 
> Caveat: There may be some problems applying the patches, because the
> source of Velocity uses DOS-line endings (^M) and I converted my
> source tree of Velocity 1.1 to Unix line endings first (no ^M). If you
> have problems, please contact me via E-Mail and I will prepare a
> DOS-compatible patch.
> 
>         Regards
>                 Henning
> 
> first patch-set:
> 
> diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/IntegerValue.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/IntegerValue.java
> --- velocity-1.1/src/java/org/apache/velocity/runtime/parser/IntegerValue.java  Thu Jan  1 01:00:00 1970
> +++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/IntegerValue.java        Tue Jun 19 18:14:12 2001
> @@ -0,0 +1,85 @@
> +package org.apache.velocity.runtime;
> +
> +/*
> + * The Apache Software License, Version 1.1
> + *
> + * Copyright (c) 2000-2001 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", "Velocity", 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/>.
> + */
> +
> +import java.lang.Integer;
> +import java.lang.reflect.Method;
> +
> +/**
> + * This is a small helper, that tries to find a intValue() method
> + * inside an object, and if it exists, returns this method. Didn't find
> + * a better place for this, if you know one, move it there.
> + *
> + * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
> + * @version $Id$
> + */
> +
> +public class IntegerValue
> +{
> +  /**
> +   * Checks whether an object contains an intValue method and
> +   * returns this value if it exists.
> +   *
> +   * @param o   An Object
> +   * @return    The Integer value
> +   *
> +   */
> +    static public Integer getInteger(Object o)
> +        throws Exception
> +    {
> +        Method m = o.getClass().getMethod("intValue", null);
> +        return (Integer)m.invoke(o, null);
> +    }
> +}
> diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
> --- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java        Sat Jun 16 19:50:27 2001
> +++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java      Tue Jun 19 17:46:11 2001
> @@ -57,6 +57,7 @@
>  import org.apache.velocity.context.InternalContextAdapter;
>  import org.apache.velocity.runtime.parser.Parser;
>  import org.apache.velocity.runtime.Runtime;
> +import org.apache.velocity.runtime.IntegerValue;
> 
>  import org.apache.velocity.exception.MethodInvocationException;
> 
> @@ -138,6 +139,17 @@
>          }
>          else
>          {
> +            try
> +            {
> +                Integer leftInt  = IntegerValue.getInteger(left);
> +                Integer rightInt = IntegerValue.getInteger(right);
> +
> +                return leftInt.equals(rightInt);
> +            }
> +            catch(Exception e)
> +            {
> +            }
> +
>              Runtime.error("Error in evaluation of == expression."
>                            + " Both arguments must be of the same Class."
>                            + " Currently left = " + left.getClass() + ", right = "
> diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
> --- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java        Sat Jun 16 19:50:27 2001
> +++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java      Tue Jun 19 17:46:36 2001
> @@ -57,6 +57,7 @@
>  import org.apache.velocity.context.InternalContextAdapter;
>  import org.apache.velocity.runtime.parser.Parser;
>  import org.apache.velocity.runtime.Runtime;
> +import org.apache.velocity.runtime.IntegerValue;
> 
>  import org.apache.velocity.exception.MethodInvocationException;
> 
> @@ -111,6 +112,17 @@
>          }
>          else
>          {
> +            try
> +            {
> +                Integer leftInt  = IntegerValue.getInteger(left);
> +                Integer rightInt = IntegerValue.getInteger(right);
> +
> +                return !(leftInt.equals(rightInt));
> +            }
> +            catch(Exception e)
> +            {
> +            }
> +
>              Runtime.error("Error in evaluation of != expression."
>                            + " Both arguments must be of the same Class."
>                            + " Currently left = " + left.getClass() + ", right = "
> 
> second patch set:
> 
> diff -Nurb velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
> --- velocity-1.1/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java     Sat Jun 16 19:50:27 2001
> +++ velocity-1.1.p/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java   Tue Jun 19 18:12:48 2001
> @@ -62,6 +62,7 @@
> 
>  import org.apache.velocity.context.Context;
>  import org.apache.velocity.context.InternalContextAdapter;
> +import org.apache.velocity.runtime.IntegerValue;
>  import org.apache.velocity.runtime.Runtime;
>  import org.apache.velocity.runtime.RuntimeConstants;
>  import org.apache.velocity.runtime.exception.ReferenceException;
> @@ -83,6 +84,7 @@
>   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
>   * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
>   * @author <a href="mailto:kjohnson@transparent.com>Kent Johnson</a>
> + * @author <a href="mailto:hps@intermeta.de>Henning P. Schmiedehausen</a>
>   * @version $Id: ASTReference.java,v 1.33 2001/05/20 19:50:06 geirm Exp $
>  */
>  public class ASTReference extends SimpleNode
> @@ -328,7 +330,20 @@
>                  return false;
>          }
>          else
> +        {
> +          try
> +          {
> +            Integer val = IntegerValue.getInteger(value);
> +            if(val.intValue() > 0)
> +              return true;
> +            else
> +              return false;
> +          }
> +          catch (Exception e)
> +          {
> +          }
>              return true;
> +        }
>      }
> 
>      public Object value( InternalContextAdapter context)
> 
> --
> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
> INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de
> 
> Am Schwabachgrund 22  Fon.: 09131 / 50654-0   info@intermeta.de
> D-91054 Buckenhof     Fax.: 09131 / 50654-20

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!