Skip to content

Commit 177e21d

Browse files
committed
[Java] Short circuit toString and appendTo when buffer is null to be a no op.
1 parent ce34259 commit 177e21d

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,12 @@ private void generateEncoderDisplay(final StringBuilder sb, final String decoder
30903090
sb.append('\n');
30913091
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
30923092
append(sb, indent, "{");
3093-
append(sb, indent, INDENT + decoderName + " writer = new " + decoderName + "();");
3093+
append(sb, indent, " if (null == buffer)");
3094+
append(sb, indent, " {");
3095+
append(sb, indent, " return builder;");
3096+
append(sb, indent, " }");
3097+
sb.append('\n');
3098+
append(sb, indent, " final " + decoderName + " writer = new " + decoderName + "();");
30943099
append(sb, indent, " writer.wrap(buffer, initialOffset, BLOCK_LENGTH, SCHEMA_VERSION);");
30953100
sb.append('\n');
30963101
append(sb, indent, " return writer.appendTo(builder);");
@@ -3105,7 +3110,12 @@ private CharSequence generateCompositeEncoderDisplay(final String decoderName)
31053110
sb.append('\n');
31063111
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
31073112
append(sb, indent, "{");
3108-
append(sb, indent, INDENT + decoderName + " writer = new " + decoderName + "();");
3113+
append(sb, indent, " if (null == buffer)");
3114+
append(sb, indent, " {");
3115+
append(sb, indent, " return builder;");
3116+
append(sb, indent, " }");
3117+
sb.append('\n');
3118+
append(sb, indent, " final " + decoderName + " writer = new " + decoderName + "();");
31093119
append(sb, indent, " writer.wrap(buffer, offset);");
31103120
sb.append('\n');
31113121
append(sb, indent, " return writer.appendTo(builder);");
@@ -3123,6 +3133,11 @@ private CharSequence generateCompositeDecoderDisplay(final List<Token> tokens)
31233133
sb.append('\n');
31243134
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
31253135
append(sb, indent, "{");
3136+
append(sb, indent, " if (null == buffer)");
3137+
append(sb, indent, " {");
3138+
append(sb, indent, " return builder;");
3139+
append(sb, indent, " }");
3140+
sb.append('\n');
31263141
Separators.BEGIN_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
31273142

31283143
int lengthBeforeLastGeneratedSeparator = -1;
@@ -3199,6 +3214,11 @@ private void generateDecoderDisplay(
31993214
sb.append('\n');
32003215
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
32013216
append(sb, indent, "{");
3217+
append(sb, indent, " if (null == buffer)");
3218+
append(sb, indent, " {");
3219+
append(sb, indent, " return builder;");
3220+
append(sb, indent, " }");
3221+
sb.append('\n');
32023222
append(sb, indent, " final int originalLimit = limit();");
32033223
append(sb, indent, " limit(initialOffset + actingBlockLength);");
32043224
append(sb, indent, " builder.append(\"[" + name + "](sbeTemplateId=\");");
@@ -3240,6 +3260,11 @@ private void appendGroupInstanceDecoderDisplay(
32403260
sb.append('\n');
32413261
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
32423262
append(sb, indent, "{");
3263+
append(sb, indent, " if (null == buffer)");
3264+
append(sb, indent, " {");
3265+
append(sb, indent, " return builder;");
3266+
append(sb, indent, " }");
3267+
sb.append('\n');
32433268
Separators.BEGIN_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
32443269
appendDecoderDisplay(sb, fields, groups, varData, indent + INDENT);
32453270
Separators.END_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
@@ -3427,6 +3452,11 @@ private void appendToString(final StringBuilder sb, final String indent)
34273452
sb.append('\n');
34283453
append(sb, indent, "public String toString()");
34293454
append(sb, indent, "{");
3455+
append(sb, indent, " if (null == buffer)");
3456+
append(sb, indent, " {");
3457+
append(sb, indent, " return \"\";");
3458+
append(sb, indent, " }");
3459+
sb.append('\n');
34303460
append(sb, indent, " return appendTo(new StringBuilder()).toString();");
34313461
append(sb, indent, "}");
34323462
}
@@ -3436,7 +3466,12 @@ private void appendMessageToString(final StringBuilder sb, final String indent,
34363466
sb.append('\n');
34373467
append(sb, indent, "public String toString()");
34383468
append(sb, indent, "{");
3439-
append(sb, indent, " " + decoderName + " decoder = new " + decoderName + "();");
3469+
append(sb, indent, " if (null == buffer)");
3470+
append(sb, indent, " {");
3471+
append(sb, indent, " return \"\";");
3472+
append(sb, indent, " }");
3473+
sb.append('\n');
3474+
append(sb, indent, " final " + decoderName + " decoder = new " + decoderName + "();");
34403475
append(sb, indent, " decoder.wrap(buffer, initialOffset, BLOCK_LENGTH, SCHEMA_VERSION);");
34413476
sb.append('\n');
34423477
append(sb, indent, " return decoder.appendTo(new StringBuilder()).toString();");

0 commit comments

Comments
 (0)