private static void bodyWithBuilder ( SourceBuilder code , Datatype datatype , Map < Property , PropertyCodeGenerator > generatorsByProperty , String typename , Predicate < PropertyCodeGenerator > isOptional ) { Variable result = new Variable ( "result" ) ; code . add ( " %1$s %2$s = new %1$s(\"%3$s{" , StringBuilder . class , result , typename ) ; boolean midStringLiteral = true ; boolean midAppends = true ; boolean prependCommas = false ; PropertyCodeGenerator lastOptionalGenerator = generatorsByProperty . values ( ) . stream ( ) . filter ( isOptional ) . reduce ( ( first , second ) -> second ) . get ( ) ; for ( Property property : generatorsByProperty . keySet ( ) ) { PropertyCodeGenerator generator = generatorsByProperty . get ( property ) ; if ( isOptional . test ( generator ) ) { if ( midStringLiteral ) { code . add ( "\")" ) ; } if ( midAppends ) { code . add ( ";%n " ) ; } code . add ( "if (" ) ; if ( generator . initialState ( ) == Initially . OPTIONAL ) { generator . addToStringCondition ( code ) ; } else { code . add ( "!%s.contains(%s.%s)" , UNSET_PROPERTIES , datatype . getPropertyEnum ( ) , property . getAllCapsName ( ) ) ; } code . add ( ") {%n %s.append(\"" , result ) ; if ( prependCommas ) { code . add ( ", " ) ; } code . add ( "%s=\").append(%s)" , property . getName ( ) , property . getField ( ) ) ; if ( ! prependCommas ) { code . add ( ".append(\", \")" ) ; } code . add ( ";%n }%n " ) ; if ( generator . equals ( lastOptionalGenerator ) ) { code . add ( "return %s.append(\"" , result ) ; midStringLiteral = true ; midAppends = true ; } else { midStringLiteral = false ; midAppends = false ; } } else { if ( ! midAppends ) { code . add ( "%s" , result ) ; } if ( ! midStringLiteral ) { code . add ( ".append(\"" ) ; } if ( prependCommas ) { code . add ( ", " ) ; } code . add ( "%s=\").append(%s)" , property . getName ( ) , ( Excerpt ) generator :: addToStringValue ) ; midStringLiteral = false ; midAppends = true ; prependCommas = true ; } } checkState ( prependCommas , "Unexpected state at end of toString method" ) ; checkState ( midAppends , "Unexpected state at end of toString method" ) ; if ( ! midStringLiteral ) { code . add ( ".append(\"" ) ; } code . add ( "}\").toString();%n" , result ) ; }
Generates the body of a toString method that uses a StringBuilder .