public static void addToString ( SourceBuilder code , Datatype datatype , Map < Property , PropertyCodeGenerator > generatorsByProperty , boolean forPartial ) { String typename = ( forPartial ? "partial " : "" ) + datatype . getType ( ) . getSimpleName ( ) ; Predicate < PropertyCodeGenerator > isOptional = generator -> { Initially initially = generator . initialState ( ) ; return ( initially == Initially . OPTIONAL || ( initially == Initially . REQUIRED && forPartial ) ) ; } ; boolean anyOptional = generatorsByProperty . values ( ) . stream ( ) . anyMatch ( isOptional ) ; boolean allOptional = generatorsByProperty . values ( ) . stream ( ) . allMatch ( isOptional ) && ! generatorsByProperty . isEmpty ( ) ; code . addLine ( "" ) . addLine ( "@%s" , Override . class ) . addLine ( "public %s toString() {" , String . class ) ; if ( allOptional ) { bodyWithBuilderAndSeparator ( code , datatype , generatorsByProperty , typename ) ; } else if ( anyOptional ) { bodyWithBuilder ( code , datatype , generatorsByProperty , typename , isOptional ) ; } else { bodyWithConcatenation ( code , generatorsByProperty , typename ) ; } code . addLine ( "}" ) ; }
Generates a toString method using concatenation or a StringBuilder .