velocity-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna (JIRA)" <...@velocity.apache.org>
Subject [jira] Resolved: (VELOCITY-316) Workaround the lack of equality in CharSequence.
Date Wed, 01 Oct 2008 22:38:44 GMT

     [ https://issues.apache.org/jira/browse/VELOCITY-316?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Nathan Bubna resolved VELOCITY-316.
-----------------------------------

       Resolution: Fixed
    Fix Version/s:     (was: 2.0)
                   1.5

This is actually fixed already, since in 1.5 we started falling back to comparing the results
of toString() when objects are not of the same type.

> Workaround the lack of equality in CharSequence.
> ------------------------------------------------
>
>                 Key: VELOCITY-316
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-316
>             Project: Velocity
>          Issue Type: Improvement
>          Components: Engine
>    Affects Versions: 1.4
>         Environment: Operating System: All
> Platform: All
>            Reporter: ArtemGr
>            Priority: Minor
>             Fix For: 1.5
>
>
> 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.
-
You can reply to this email to add a comment to the issue online.


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


Mime
View raw message