[ 
https://issues.apache.org/jira/browse/THRIFT-1386?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13128068#comment-13128068
 ] 

Verdoïa Laurent commented on THRIFT-1386:
-----------------------------------------

I understand for optional nullable fields; because of that, the comma cannot be 
computed at compile time :(
So I not care anymore about: {{if (!first) sb.append(", ");}}


But for the handling of null value can still be improved because the append 
member {{StringBuilder.append(...)}} always use the string {{"null"}} for 
{{String}} and {{Object}}

||Last version from subversion||Improved version||
|{code:title=OptionalNullable.java|borderStyle=solid}
if (isSetTheField()) {
    sb.append("theField:");
    if (this.theField == null) {
        sb.append("null");
    } else {
        sb.append(this.theField);
    }
    first = false;
}
{code}|{code:title=ImprovedOptionalNullable.java|borderStyle=solid}
if (isSetTheField()) {
    sb.append("theField:");
    sb.append(this.theField);
    first = false;
}
{code}|
|{code:title=RequiredNullable.java|borderStyle=solid}
sb.append("theField:");
if (this.theField == null) {
    sb.append("null");
} else {
    sb.append(this.theField);
}
first = false;
{code}|{code:title=RequiredOptionalNullable.java|borderStyle=solid}
sb.append("theField:");
sb.append(this.theField);
first = false;
{code}|


But in the uppercase source, i didn't talk about the thruft {{binary}} type 
which is convert into {{Bytebuffer}} and use the static member 
{{org.apache.thrift.TBaseHelper.toString}} for be converter to String.
This member don't handle null value.
That why for binary only whe shouldn't modify generated code an still have the 
nullity test:
{code:title=StringBufferHandler__reminder.java|borderStyle=solid}
if (this.theField == null) { // Cannot be improved
    sb.append("null");
} else {
    org.apache.thrift.TBaseHelper.toString(this.theField, sb);
}
{code}

If you want a patch just ask me...
                
> The toString function can consume less CPU and tests
> ----------------------------------------------------
>
>                 Key: THRIFT-1386
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1386
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Java - Compiler
>    Affects Versions: 0.8
>            Reporter: Verdoïa Laurent
>            Priority: Minor
>             Fix For: 0.8
>
>         Attachments: toString.patch
>
>
> The toString member of generates classes don't need:
> 1. to test the isFirst value.
> The compiler allready know it and can handle the last comma himself
> 2. to handle special case for nullable object.
> StrinBuilder is used for create the resulting string.
> This append method always append the string "null" for null objects.
> But the result of append(char[] data) with null is not specified; who care. 
> We don't use it!

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply via email to