Author: wglass
Date: Fri Sep 23 22:34:39 2005
New Revision: 291252

URL: http://svn.apache.org/viewcvs?rev=291252&view=rev
Log:
equality and not equality now based on toString when classes are different.  
See VELOCITY-350

Modified:
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
    
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
    
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/TemplateTestCase.java
    jakarta/velocity/core/trunk/test/templates/compare/logical.cmp
    jakarta/velocity/core/trunk/test/templates/logical.vm

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java?rev=291252&r1=291251&r2=291252&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTEQNode.java
 Fri Sep 23 22:34:39 2005
@@ -111,27 +111,41 @@
        }
 
 
-        /*
-         *  check to see if they are the same class.  I don't think this is 
slower
-         *  as I don't think that getClass() results in object creation, and 
we can
-         *  extend == to handle all classes
-         */
 
-        if (left.getClass().equals( right.getClass() ) )
+       /** 
+        * assume that if one class is a subclass of the other 
+        * that we should use the equals operator
+        */
+       
+        if (left.getClass().isAssignableFrom(right.getClass()) || 
+                right.getClass().isAssignableFrom(left.getClass()) )
         {
             return left.equals( right );
         }
         else
         {
-            rsvc.error("Error in evaluation of == expression."
-                          + " Both arguments must be of the same Class."
-                          + " Currently left = " + left.getClass() + ", right 
= " 
-                          + right.getClass() + ". "
-                          + context.getCurrentTemplateName() + " [line " + 
getLine() 
-                          + ", column " + getColumn() + "] (ASTEQNode)");
+            /**
+             * Compare the String representations
+             */
+            if ((left.toString() == null) || (right.toString() == null))  
+            {
+                rsvc.error( ( left.toString() == null ? "Left" : "Right" ) + " 
string side "
+                        + "String representation ("
+                        + jjtGetChild( (left == null? 0 : 1) ).literal()
+                        + ") of '!=' operation has null value."
+                        + " Operation not possible. "
+                        + context.getCurrentTemplateName() + " [line " + 
getLine() 
+                        + ", column " + getColumn() + "]");
+
+                return false;
+            }
+            
+            else 
+            {
+                return left.toString().equals(right.toString());
+            }
         }
 
-        return false;    
     }
 
     public Object value(InternalContextAdapter context)

Modified: 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java?rev=291252&r1=291251&r2=291252&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTNENode.java
 Fri Sep 23 22:34:39 2005
@@ -93,27 +93,41 @@
        }
 
 
-        /*
-         *  check to see if they are the same class.  I don't think this is 
slower
-         *  as I don't think that getClass() results in object creation, and 
we can
-         *  extend == to handle all classes
-         */
-
-        if (left.getClass().equals( right.getClass() ) )
+       /** 
+        * assume that if one class is a subclass of the other 
+        * that we should use the equals operator
+        */
+       
+        if (left.getClass().isAssignableFrom(right.getClass()) || 
+                right.getClass().isAssignableFrom(left.getClass()) )
         {
-            return !(left.equals( right ));
+            return !left.equals( right );
         }
         else
         {
-            rsvc.error("Error in evaluation of != expression."
-                          + " Both arguments must be of the same Class."
-                          + " Currently left = " + left.getClass() + ", right 
= " 
-                          + right.getClass() + ". "
-                          + context.getCurrentTemplateName() + " [line " + 
getLine() 
-                          + ", column " + getColumn() + "] (ASTEQNode)");
+            /**
+             * Compare the String representations
+             */
+            if ((left.toString() == null) || (right.toString() == null))  
+            {
+                rsvc.error( ( left.toString() == null ? "Left" : "Right" ) + " 
string side "
+                        + "String representation ("
+                        + jjtGetChild( (left == null? 0 : 1) ).literal()
+                        + ") of '!=' operation has null value."
+                        + " Operation not possible. "
+                        + context.getCurrentTemplateName() + " [line " + 
getLine() 
+                        + ", column " + getColumn() + "]");
+
+                return false;
+            }
+            
+            else 
+            {
+                return !left.toString().equals(right.toString());
+            }
             
-            return false;
         }
+
     }
 
     public Object value(InternalContextAdapter context)

Modified: 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/TemplateTestCase.java
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/TemplateTestCase.java?rev=291252&r1=291251&r2=291252&view=diff
==============================================================================
--- 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/TemplateTestCase.java
 (original)
+++ 
jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/TemplateTestCase.java
 Fri Sep 23 22:34:39 2005
@@ -125,6 +125,8 @@
 
         context.put("provider", provider);
         context1.put("name", "jason");
+        context1.put("name2", new StringBuffer("jason"));
+        context1.put("name3", new StringBuffer("geoge"));
         context2.put("providers", provider.getCustomers2());
         context.put("list", al);
         context1.put("hashtable", h);

Modified: jakarta/velocity/core/trunk/test/templates/compare/logical.cmp
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/test/templates/compare/logical.cmp?rev=291252&r1=291251&r2=291252&view=diff
==============================================================================
--- jakarta/velocity/core/trunk/test/templates/compare/logical.cmp (original)
+++ jakarta/velocity/core/trunk/test/templates/compare/logical.cmp Fri Sep 23 
22:34:39 2005
@@ -164,6 +164,14 @@
 
 Should equal true : true
 
+----------------------
+Compare String and StringBuffer
+----------------------
+This should be true: true
+
+This should be false: false
+
+This should be true: true
 
 
 right

Modified: jakarta/velocity/core/trunk/test/templates/logical.vm
URL: 
http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/test/templates/logical.vm?rev=291252&r1=291251&r2=291252&view=diff
==============================================================================
--- jakarta/velocity/core/trunk/test/templates/logical.vm (original)
+++ jakarta/velocity/core/trunk/test/templates/logical.vm Fri Sep 23 22:34:39 
2005
@@ -390,6 +390,17 @@
 #set($fofo = ( ($t || $f) && $t))
 Should equal true : $fofo
 
+----------------------
+Compare String and StringBuffer
+----------------------
+#set($compClass = ($name == $name2))
+This should be true: $compClass
+
+#set($compClass2 = ($name == $name3))
+This should be false: $compClass2
+
+#set($compClass3 = ($name != $name3))
+This should be true: $compClass3
 
 #set($x = !true)
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to