[ http://issues.apache.org/jira/browse/VELOCITY-316?page=all ]

Will Glass-Husain updated VELOCITY-316:
---------------------------------------

    Bugzilla Id:   (was: 32179)
    Fix Version: 2.0
    Description: 
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.

  was:
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.

    Environment: 
Operating System: All
Platform: All

  was:
Operating System: All
Platform: All

      Assign To:     (was: Velocity-Dev List)

Thanks for contributing this.  Looks useful.  Unfortunately, we're aiming for a 
JDK 1.3 compatibility, so can't include this.  I'll leave the bug open and mark 
it as a "2.0" feature which our catch-all for interesting but not quite 
compatible changes.

> Workaround the lack of equality in CharSequence.
> ------------------------------------------------
>
>          Key: VELOCITY-316
>          URL: http://issues.apache.org/jira/browse/VELOCITY-316
>      Project: Velocity
>         Type: Improvement
>   Components: Source
>     Versions: 1.4
>  Environment: Operating System: All
> Platform: All
>     Reporter: ArtemGr
>     Priority: Minor
>      Fix For: 2.0

>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


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

Reply via email to