Skip to content

Commit d77b731

Browse files
committed
[Java] Further reduction in copying in JavaGenerator.
1 parent bf648c0 commit d77b731

File tree

6 files changed

+94
-106
lines changed

6 files changed

+94
-106
lines changed

sbe-samples/src/main/resources/common-types.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<types>
3-
<composite name="messageHeader" description="Message identifiers and length of message root">
3+
<composite name="messageHeader" description="Message identifiers and length of message root.">
44
<type name="blockLength" primitiveType="uint16"/>
55
<type name="templateId" primitiveType="uint16"/>
66
<type name="schemaId" primitiveType="uint16"/>
77
<type name="version" primitiveType="uint16"/>
88
</composite>
9-
<composite name="groupSizeEncoding" description="Repeating group dimensions">
9+
<composite name="groupSizeEncoding" description="Repeating group dimensions.">
1010
<type name="blockLength" primitiveType="uint16"/>
1111
<type name="numInGroup" primitiveType="uint16"/>
1212
</composite>
13-
<composite name="varStringEncoding">
13+
<composite name="varStringEncoding" description="Variable length UTF-8 String.">
1414
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
1515
<type name="varData" primitiveType="uint8" length="0" characterEncoding="UTF-8"/>
1616
</composite>
17-
<composite name="varAsciiEncoding">
17+
<composite name="varAsciiEncoding" description="Variable length ASCII String.">
1818
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
1919
<type name="varData" primitiveType="uint8" length="0" characterEncoding="ASCII"/>
2020
</composite>
21-
<composite name="varDataEncoding">
21+
<composite name="varDataEncoding" description="Variable length binary blob.">
2222
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
2323
<type name="varData" primitiveType="uint8" length="0"/>
2424
</composite>

sbe-samples/src/main/resources/example-extension-schema.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
<ref name="boosterEnabled" type="BooleanType"/>
3434
<ref name="booster" type="Booster"/>
3535
</composite>
36-
<enum name="BooleanType" encodingType="uint8">
37-
<validValue name="F">0</validValue>
38-
<validValue name="T">1</validValue>
36+
<enum name="BooleanType" encodingType="uint8" description="Boolean Type.">
37+
<validValue name="F" description="False value representation.">0</validValue>
38+
<validValue name="T" description="True value representation.">1</validValue>
3939
</enum>
4040
<enum name="Model" encodingType="char">
4141
<validValue name="A">A</validValue>

sbe-samples/src/main/resources/example-schema.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
<ref name="boosterEnabled" type="BooleanType"/>
3434
<ref name="booster" type="Booster"/>
3535
</composite>
36-
<enum name="BooleanType" encodingType="uint8">
37-
<validValue name="F">0</validValue>
38-
<validValue name="T">1</validValue>
36+
<enum name="BooleanType" encodingType="uint8" description="Boolean Type.">
37+
<validValue name="F" description="False value representation.">0</validValue>
38+
<validValue name="T" description="True value representation.">1</validValue>
3939
</enum>
4040
<enum name="Model" encodingType="char">
4141
<validValue name="A">A</validValue>

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

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,14 @@ private static void generateGroupDecoderProperty(
681681
indent + " return " + propertyName + ";\n" +
682682
indent + " }\n\n";
683683

684+
generateFlyweightPropertyJavadoc(sb, indent + INDENT, token, className);
684685
sb.append(String.format("\n" +
685-
"%1$s" +
686-
indent + " public %2$s %3$s()\n" +
686+
indent + " public %1$s %2$s()\n" +
687687
indent + " {\n" +
688-
"%4$s" +
689-
indent + " %3$s.wrap(buffer);\n" +
690-
indent + " return %3$s;\n" +
688+
"%3$s" +
689+
indent + " %2$s.wrap(buffer);\n" +
690+
indent + " return %2$s;\n" +
691691
indent + " }\n",
692-
generateFlyweightPropertyJavadoc(indent + INDENT, token, className),
693692
className,
694693
propertyName,
695694
actingVersionGuard));
@@ -722,14 +721,13 @@ private void generateGroupEncoderProperty(
722721
formatPropertyName(groupName),
723722
token.id()));
724723

724+
generateGroupEncodePropertyJavadoc(sb, indent + INDENT, token, className);
725725
sb.append(String.format("\n" +
726-
"%1$s" +
727-
indent + " public %2$s %3$sCount(final int count)\n" +
726+
indent + " public %1$s %2$sCount(final int count)\n" +
728727
indent + " {\n" +
729-
indent + " %3$s.wrap(buffer, count);\n" +
730-
indent + " return %3$s;\n" +
728+
indent + " %2$s.wrap(buffer, count);\n" +
729+
indent + " return %2$s;\n" +
731730
indent + " }\n",
732-
generateGroupEncodePropertyJavadoc(indent + INDENT, token, className),
733731
className,
734732
propertyName));
735733
}
@@ -1347,10 +1345,11 @@ private CharSequence generateChoiceClear(final String bitSetClassName, final Tok
13471345

13481346
private CharSequence generateChoiceDecoders(final List<Token> tokens)
13491347
{
1350-
return concatTokens(
1351-
tokens,
1352-
Signal.CHOICE,
1353-
(token) ->
1348+
final StringBuilder sb = new StringBuilder();
1349+
1350+
for (final Token token : tokens)
1351+
{
1352+
if (token.signal() == Signal.CHOICE)
13541353
{
13551354
final String choiceName = formatPropertyName(token.name());
13561355
final Encoding encoding = token.encoding();
@@ -1359,30 +1358,33 @@ private CharSequence generateChoiceDecoders(final List<Token> tokens)
13591358
final PrimitiveType primitiveType = encoding.primitiveType();
13601359
final String argType = bitsetArgType(primitiveType);
13611360

1362-
return String.format("\n" +
1363-
"%1$s" +
1364-
" public boolean %2$s()\n" +
1361+
generateOptionDecodeJavadoc(sb, INDENT, token);
1362+
sb.append(String.format("\n" +
1363+
" public boolean %1$s()\n" +
13651364
" {\n" +
1366-
" return %3$s;\n" +
1365+
" return %2$s;\n" +
13671366
" }\n\n" +
1368-
" public static boolean %2$s(final %4$s value)\n" +
1367+
" public static boolean %1$s(final %3$s value)\n" +
13691368
" {\n" +
1370-
" return %5$s;\n" +
1369+
" return %4$s;\n" +
13711370
" }\n",
1372-
generateOptionDecodeJavadoc(INDENT, token),
13731371
choiceName,
13741372
generateChoiceGet(primitiveType, choiceBitIndex, byteOrderStr),
13751373
argType,
1376-
generateStaticChoiceGet(primitiveType, choiceBitIndex));
1377-
});
1374+
generateStaticChoiceGet(primitiveType, choiceBitIndex)));
1375+
}
1376+
}
1377+
1378+
return sb;
13781379
}
13791380

13801381
private CharSequence generateChoiceEncoders(final String bitSetClassName, final List<Token> tokens)
13811382
{
1382-
return concatTokens(
1383-
tokens,
1384-
Signal.CHOICE,
1385-
(token) ->
1383+
final StringBuilder sb = new StringBuilder();
1384+
1385+
for (final Token token : tokens)
1386+
{
1387+
if (token.signal() == Signal.CHOICE)
13861388
{
13871389
final String choiceName = formatPropertyName(token.name());
13881390
final Encoding encoding = token.encoding();
@@ -1391,24 +1393,26 @@ private CharSequence generateChoiceEncoders(final String bitSetClassName, final
13911393
final PrimitiveType primitiveType = encoding.primitiveType();
13921394
final String argType = bitsetArgType(primitiveType);
13931395

1394-
return String.format("\n" +
1395-
"%1$s" +
1396-
" public %2$s %3$s(final boolean value)\n" +
1396+
generateOptionEncodeJavadoc(sb, INDENT, token);
1397+
sb.append(String.format("\n" +
1398+
" public %1$s %2$s(final boolean value)\n" +
13971399
" {\n" +
1398-
"%4$s\n" +
1400+
"%3$s\n" +
13991401
" return this;\n" +
14001402
" }\n\n" +
1401-
" public static %5$s %3$s(final %5$s bits, final boolean value)\n" +
1403+
" public static %4$s %2$s(final %4$s bits, final boolean value)\n" +
14021404
" {\n" +
1403-
"%6$s" +
1405+
"%5$s" +
14041406
" }\n",
1405-
generateOptionEncodeJavadoc(INDENT, token),
14061407
bitSetClassName,
14071408
choiceName,
14081409
generateChoicePut(encoding.primitiveType(), choiceBitIndex, byteOrderStr),
14091410
argType,
1410-
generateStaticChoicePut(encoding.primitiveType(), choiceBitIndex));
1411-
});
1411+
generateStaticChoicePut(encoding.primitiveType(), choiceBitIndex)));
1412+
}
1413+
}
1414+
1415+
return sb;
14121416
}
14131417

14141418
private String bitsetArgType(final PrimitiveType primitiveType)
@@ -1638,7 +1642,7 @@ private static CharSequence generateDeclaration(
16381642
.append("public class ").append(className).append(implementsString).append('\n')
16391643
.append("{\n");
16401644

1641-
return sb.toString();
1645+
return sb;
16421646
}
16431647

16441648
private void generatePackageInfo() throws IOException
@@ -1695,7 +1699,7 @@ private static CharSequence generateEnumDeclaration(final String name, final Tok
16951699
generateTypeJavadoc(sb, BASE_INDENT, typeToken);
16961700
sb.append("public enum ").append(name).append("\n{\n");
16971701

1698-
return sb.toString();
1702+
return sb;
16991703
}
17001704

17011705
private CharSequence generatePrimitiveDecoder(
@@ -2843,15 +2847,14 @@ private CharSequence generateBitSetProperty(
28432847
propertyName,
28442848
bitSetName));
28452849

2850+
generateFlyweightPropertyJavadoc(sb, indent + INDENT, propertyToken, bitSetName);
28462851
sb.append(String.format("\n" +
2847-
"%s" +
28482852
indent + " public %s %s()\n" +
28492853
indent + " {\n" +
28502854
"%s" +
28512855
indent + " %s.wrap(buffer, offset + %d);\n" +
28522856
indent + " return %s;\n" +
28532857
indent + " }\n",
2854-
generateFlyweightPropertyJavadoc(indent + INDENT, propertyToken, bitSetName),
28552858
bitSetName,
28562859
propertyName,
28572860
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken, null, indent),
@@ -2879,15 +2882,14 @@ private CharSequence generateCompositeProperty(
28792882
propertyName,
28802883
compositeName));
28812884

2885+
generateFlyweightPropertyJavadoc(sb, indent + INDENT, propertyToken, compositeName);
28822886
sb.append(String.format("\n" +
2883-
"%s" +
28842887
indent + " public %s %s()\n" +
28852888
indent + " {\n" +
28862889
"%s" +
28872890
indent + " %s.wrap(buffer, offset + %d);\n" +
28882891
indent + " return %s;\n" +
28892892
indent + " }\n",
2890-
generateFlyweightPropertyJavadoc(indent + INDENT, propertyToken, compositeName),
28912893
compositeName,
28922894
propertyName,
28932895
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken, null, indent),

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

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -273,97 +273,96 @@ public static void generateTypeJavadoc(
273273
/**
274274
* Generate the Javadoc comment header for a bitset choice option decode method.
275275
*
276+
* @param sb to append to.
276277
* @param indent level for the comment.
277278
* @param optionToken for the type.
278-
* @return a string representation of the Javadoc comment.
279279
*/
280-
public static String generateOptionDecodeJavadoc(final String indent, final Token optionToken)
280+
public static void generateOptionDecodeJavadoc(
281+
final StringBuilder sb, final String indent, final Token optionToken)
281282
{
282283
final String description = optionToken.description();
283284
if (null == description || description.isEmpty())
284285
{
285-
return "";
286+
return;
286287
}
287288

288-
return
289-
indent + "/**\n" +
290-
indent + " * " + description + '\n' +
291-
indent + " *\n" +
292-
indent + " * @return true if " + optionToken.name() + " is set or false if not\n" +
293-
indent + " */\n";
289+
sb.append(indent).append("/**\n")
290+
.append(indent).append(" * ").append(description).append('\n')
291+
.append(indent).append(" *\n")
292+
.append(indent).append(" * @return true if ").append(optionToken.name()).append(" is set or false if not\n")
293+
.append(indent).append(" */\n");
294294
}
295295

296296
/**
297297
* Generate the Javadoc comment header for a bitset choice option encode method.
298298
*
299+
* @param sb to append to.
299300
* @param indent level for the comment.
300301
* @param optionToken for the type.
301-
* @return a string representation of the Javadoc comment.
302302
*/
303-
public static String generateOptionEncodeJavadoc(final String indent, final Token optionToken)
303+
public static void generateOptionEncodeJavadoc(
304+
final StringBuilder sb, final String indent, final Token optionToken)
304305
{
305306
final String description = optionToken.description();
306307
if (null == description || description.isEmpty())
307308
{
308-
return "";
309+
return;
309310
}
310311

311-
return
312-
indent + "/**\n" +
313-
indent + " * " + description + '\n' +
314-
indent + " *\n" +
315-
indent + " * @param value true if " + optionToken.name() + " is set or false if not\n" +
316-
indent + " */\n";
312+
final String name = optionToken.name();
313+
sb.append(indent).append("/**\n")
314+
.append(indent).append(" * ").append(description).append('\n')
315+
.append(indent).append(" *\n")
316+
.append(indent).append(" * @param value true if ").append(name).append(" is set or false if not.\n")
317+
.append(indent).append(" */\n");
317318
}
318319

319320
/**
320321
* Generate the Javadoc comment header for flyweight property.
321322
*
323+
* @param sb to append to.
322324
* @param indent level for the comment.
323325
* @param propertyToken for the property name.
324326
* @param typeName for the property type.
325-
* @return a string representation of the Javadoc comment.
326327
*/
327-
public static String generateFlyweightPropertyJavadoc(
328-
final String indent, final Token propertyToken, final String typeName)
328+
public static void generateFlyweightPropertyJavadoc(
329+
final StringBuilder sb, final String indent, final Token propertyToken, final String typeName)
329330
{
330331
final String description = propertyToken.description();
331332
if (null == description || description.isEmpty())
332333
{
333-
return "";
334+
return;
334335
}
335336

336-
return
337-
indent + "/**\n" +
338-
indent + " * " + description + '\n' +
339-
indent + " *\n" +
340-
indent + " * @return " + typeName + " : " + description + "\n" +
341-
indent + " */\n";
337+
sb.append(indent).append("/**\n")
338+
.append(indent).append(" * ").append(description).append('\n')
339+
.append(indent).append(" *\n")
340+
.append(indent).append(" * @return ").append(typeName).append(" : ").append(description).append("\n")
341+
.append(indent).append(" */\n");
342342
}
343343

344344
/**
345345
* Generate the Javadoc comment header for group encode property.
346346
*
347+
* @param sb to append to.
347348
* @param indent level for the comment.
348349
* @param propertyToken for the property name.
349350
* @param typeName for the property type.
350-
* @return a string representation of the Javadoc comment.
351351
*/
352-
public static String generateGroupEncodePropertyJavadoc(
353-
final String indent, final Token propertyToken, final String typeName)
352+
public static void generateGroupEncodePropertyJavadoc(
353+
final StringBuilder sb, final String indent, final Token propertyToken, final String typeName)
354354
{
355355
final String description = propertyToken.description();
356356
if (null == description || description.isEmpty())
357357
{
358-
return "";
358+
return;
359359
}
360360

361-
return
362-
indent + "/**\n" +
363-
indent + " * " + description + "\n" +
364-
indent + " *\n" +
365-
indent + " * @param count of times the group will be encoded\n" +
366-
indent + " * @return " + typeName + " : encoder for the group\n" +
367-
indent + " */\n";
361+
sb.append(indent).append("/**\n")
362+
.append(indent).append(" * ").append(description).append("\n")
363+
.append(indent).append(" *\n")
364+
.append(indent).append(" * @param count of times the group will be encoded\n")
365+
.append(indent).append(" * @return ").append(typeName).append(" : encoder for the group\n")
366+
.append(indent).append(" */\n");
368367
}
369368
}

0 commit comments

Comments
 (0)