private static void bodyWithBuilderAndSeparator ( SourceBuilder code , Datatype datatype , Map < Property , PropertyCodeGenerator > generatorsByProperty , String typename ) { Variable result = new Variable ( "result" ) ; Variable separator = new Variable ( "separator" ) ; code . addLine ( " %1$s %2$s = new %1$s(\"%3$s{\");" , StringBuilder . class , result , typename ) ; if ( generatorsByProperty . size ( ) > 1 ) { // If there's a single property, we don't actually use the separator variable code . addLine ( " %s %s = \"\";" , String . class , separator ) ; } Property first = generatorsByProperty . keySet ( ) . iterator ( ) . next ( ) ; Property last = getLast ( generatorsByProperty . keySet ( ) ) ; for ( Property property : generatorsByProperty . keySet ( ) ) { PropertyCodeGenerator generator = generatorsByProperty . get ( property ) ; switch ( generator . initialState ( ) ) { case HAS_DEFAULT : throw new RuntimeException ( "Internal error: unexpected default field" ) ; case OPTIONAL : code . addLine ( " if (%s) {" , ( Excerpt ) generator :: addToStringCondition ) ; break ; case REQUIRED : code . addLine ( " if (!%s.contains(%s.%s)) {" , UNSET_PROPERTIES , datatype . getPropertyEnum ( ) , property . getAllCapsName ( ) ) ; break ; } code . add ( " " ) . add ( result ) ; if ( property != first ) { code . add ( ".append(%s)" , separator ) ; } code . add ( ".append(\"%s=\").append(%s)" , property . getName ( ) , ( Excerpt ) generator :: addToStringValue ) ; if ( property != last ) { code . add ( ";%n %s = \", \"" , separator ) ; } code . add ( ";%n }%n" ) ; } code . addLine ( " return %s.append(\"}\").toString();" , result ) ; }
Generates the body of a toString method that uses a StringBuilder and a separator variable .