You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by bu...@apache.org on 2004/11/11 12:58:01 UTC

DO NOT REPLY [Bug 32179] New: - Workaround the lack of equality in CharSequence.

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=32179>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=32179

Workaround the lack of equality in CharSequence.

           Summary: Workaround the lack of equality in CharSequence.
           Product: Velocity
           Version: 1.4
          Platform: All
               URL: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSe
                    quence.html
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Source
        AssignedTo: velocity-dev@jakarta.apache.org
        ReportedBy: artem@bizlink.ru


CharSequence is a nice addition to the Java API,
helping to encapsulate implementation details
and allowing programmers to use StringBuffer,
mutable strings (such as
http://mg4j.dsi.unimi.it/docs/it/unimi/dsi/mg4j/util/MutableString.html )
or even their custom classes directly
instead of costly process of converting everything into a String.

One of the remaining drawbacks of the CharSequence
is the lack of support for the Object#equals method.
Fortunately, in Velocity it can be easily fixed
in the ASTEQNode and ASTNENode classes.
Example of the fix is provided in the following patches.

--- org/apache/velocity/runtime/parser/node/original/ASTEQNode.java	Wed Apr 
14 09:26:42 2004
+++ org/apache/velocity/runtime/parser/node/ASTEQNode.java	Thu Nov 11 14:21:
03 2004
@@ -101,6 +101,20 @@
         }
         else
         {
+          
+          /*
+           * Comparison of CharSequence objects.
+           */
+          if (left instanceof CharSequence && right instanceof CharSequence)
+          {
+            final CharSequence a = (CharSequence) left;
+            final CharSequence b = (CharSequence) right;
+            final int aLen = a.length(); final int bLen = b.length();
+            if( aLen != bLen ) return false;
+            int n = aLen; while( 0 != n-- ) if( a.charAt( n ) != b.charAt( n ) 
) return false;
+            return true;
+          }
+
             rsvc.error("Error in evaluation of == expression."
                           + " Both arguments must be of the same Class."
                           + " Currently left = " + left.getClass() + ", right = 
" 


--- org/apache/velocity/runtime/parser/node/original/ASTNENode.java	Wed Apr 
14 09:26:42 2004
+++ org/apache/velocity/runtime/parser/node/ASTNENode.java	Thu Nov 11 14:22:
04 2004
@@ -74,6 +74,20 @@
         }
         else
         {
+          
+          /*
+           * Comparison of CharSequence objects.
+           */
+          if (left instanceof CharSequence && right instanceof CharSequence)
+          {
+            final CharSequence a = (CharSequence) left;
+            final CharSequence b = (CharSequence) right;
+            final int aLen = a.length(); final int bLen = b.length();
+            if( aLen != bLen ) return true;
+            int n = aLen; while( 0 != n-- ) if( a.charAt( n ) != b.charAt( n ) 
) return true;
+            return false;
+          }
+
             rsvc.error("Error in evaluation of != expression."
                           + " Both arguments must be of the same Class."
                           + " Currently left = " + left.getClass() + ", right = 
" 

The additional check should not have any performance impact,
since it is performed where the 'Both arguments must be of the same Class'
error was.

I really hope Velocity will incorporate at least that minimal
support for CharSequence, making Java environment
to be a lot friendlier to some of us.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org