Skip to content

Commit 5dd77ad

Browse files
authored
Merge pull request #360 from xiangh-fr/String3
[Java] Rationalize display format
2 parents d7150b4 + 526e4d2 commit 5dd77ad

File tree

5 files changed

+202
-115
lines changed

5 files changed

+202
-115
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,8 +2379,7 @@ private CharSequence generateCompositeDecoderDisplay(final List<Token> tokens, f
23792379
sb.append('\n');
23802380
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
23812381
append(sb, indent, "{");
2382-
append(sb, indent, " builder.append(\"[\");");
2383-
2382+
Separators.BEGIN_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
23842383
for (int i = 1, end = tokens.size() - 1; i < end; )
23852384
{
23862385
final Token encodingToken = tokens.get(i);
@@ -2389,8 +2388,7 @@ private CharSequence generateCompositeDecoderDisplay(final List<Token> tokens, f
23892388
i += encodingToken.componentTokenCount();
23902389
}
23912390

2392-
append(sb, indent, " builder.setLength(builder.length() - 1);");
2393-
append(sb, indent, " builder.append(\"]\");");
2391+
Separators.END_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
23942392
sb.append('\n');
23952393
append(sb, indent, " return builder;");
23962394
append(sb, indent, "}");
@@ -2407,7 +2405,7 @@ private CharSequence generateChoiceDisplay(final List<Token> tokens)
24072405
sb.append('\n');
24082406
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
24092407
append(sb, indent, "{");
2410-
append(sb, indent, " builder.append('{');");
2408+
Separators.BEGIN_SET.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
24112409
append(sb, indent, " boolean atLeastOne = false;");
24122410

24132411
tokens
@@ -2421,15 +2419,15 @@ private CharSequence generateChoiceDisplay(final List<Token> tokens)
24212419
append(sb, indent, " {");
24222420
append(sb, indent, " if (atLeastOne)");
24232421
append(sb, indent, " {");
2424-
append(sb, indent, " builder.append(',');");
2422+
Separators.ENTRY.appendToGeneratedBuilder(sb, indent + INDENT + INDENT + INDENT, "builder");
24252423
append(sb, indent, " }");
24262424
append(sb, indent, " builder.append(\"" + choiceName + "\");");
24272425
append(sb, indent, " atLeastOne = true;");
24282426
append(sb, indent, " }");
24292427
}
24302428
);
24312429

2432-
append(sb, indent, " builder.append('}');");
2430+
Separators.END_SET.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
24332431
sb.append('\n');
24342432
append(sb, indent, " return builder;");
24352433
append(sb, indent, "}");
@@ -2497,8 +2495,9 @@ private StringBuilder appendGroupInstanceDecoderDisplay(
24972495
sb.append('\n');
24982496
append(sb, indent, "public StringBuilder appendTo(final StringBuilder builder)");
24992497
append(sb, indent, "{");
2498+
Separators.BEGIN_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
25002499
appendDecoderDisplay(sb, fields, groups, varData, indent + INDENT);
2501-
sb.append('\n');
2500+
Separators.END_COMPOSITE.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
25022501
append(sb, indent, " return builder;");
25032502
append(sb, indent, "}");
25042503

@@ -2534,16 +2533,15 @@ private StringBuilder appendDecoderDisplay(
25342533
final String groupName = formatPropertyName(groupToken.name());
25352534
final String groupDecoderName = decoderName(formatClassName(groupToken.name()));
25362535

2537-
append(sb, indent, "builder.append(\"" + groupName + "={\");");
2536+
append(sb, indent, "builder.append(\"" + groupName + Separators.KEY_VALUE + Separators.BEGIN_GROUP + "\");");
25382537
append(sb, indent, groupDecoderName + " " + groupName + " = " + groupName + "();");
25392538
append(sb, indent, "while (" + groupName + ".hasNext())");
25402539
append(sb, indent, "{");
2541-
append(sb, indent, " builder.append('[');");
25422540
append(sb, indent, " " + groupName + ".next().appendTo(builder);");
2543-
append(sb, indent, " builder.setLength(builder.length() - 1);");
2544-
append(sb, indent, " builder.append(']');");
2541+
Separators.ENTRY.appendToGeneratedBuilder(sb, indent + INDENT, "builder");
25452542
append(sb, indent, "}");
2546-
append(sb, indent, "builder.append(\"}|\");");
2543+
append(sb, indent, "builder.setLength(builder.length() - 1);");
2544+
append(sb, indent, "builder.append(\"" + Separators.END_GROUP + Separators.FIELD + "\");");
25472545

25482546
i = findEndSignal(groups, i, Signal.END_GROUP, groupToken.name());
25492547
}
@@ -2559,9 +2557,9 @@ private StringBuilder appendDecoderDisplay(
25592557
append(sb, indent, "//" + varDataToken);
25602558

25612559
final String varDataName = formatPropertyName(varDataToken.name());
2562-
append(sb, indent, "builder.append(\"" + varDataName + "=\");");
2560+
append(sb, indent, "builder.append(\"" + varDataName + Separators.KEY_VALUE + "\");");
25632561
append(sb, indent, "builder.append(" + varDataName + "());");
2564-
append(sb, indent, "builder.append('|');");
2562+
Separators.FIELD.appendToGeneratedBuilder(sb, indent, "builder");
25652563

25662564
i += varDataToken.componentTokenCount();
25672565
}
@@ -2582,7 +2580,7 @@ private void writeTokenDisplay(
25822580
return;
25832581
}
25842582

2585-
append(sb, indent, "builder.append(\"" + fieldName + "=\");");
2583+
append(sb, indent, "builder.append(\"" + fieldName + Separators.KEY_VALUE + "\");");
25862584

25872585
switch (typeToken.signal())
25882586
{
@@ -2598,16 +2596,17 @@ private void writeTokenDisplay(
25982596
}
25992597
else
26002598
{
2601-
append(sb, indent, "builder.append('[');");
2599+
Separators.BEGIN_ARRAY.appendToGeneratedBuilder(sb, indent, "builder");
26022600
append(sb, indent, "if (" + fieldName + "Length() > 0)");
26032601
append(sb, indent, "{");
26042602
append(sb, indent, " for (int i = 0; i < " + fieldName + "Length(); i++)");
26052603
append(sb, indent, " {");
2606-
append(sb, indent, " builder.append(" + fieldName + "(i)).append(',');");
2604+
append(sb, indent, " builder.append(" + fieldName + "(i));");
2605+
Separators.ENTRY.appendToGeneratedBuilder(sb, indent + INDENT + INDENT, "builder");
26072606
append(sb, indent, " }");
26082607
append(sb, indent, " builder.setLength(builder.length()-1);");
26092608
append(sb, indent, "}");
2610-
append(sb, indent, "builder.append(']');");
2609+
Separators.END_ARRAY.appendToGeneratedBuilder(sb, indent, "builder");
26112610
}
26122611
}
26132612
else
@@ -2627,7 +2626,7 @@ private void writeTokenDisplay(
26272626
break;
26282627
}
26292628

2630-
append(sb, indent, "builder.append('|');");
2629+
Separators.FIELD.appendToGeneratedBuilder(sb, indent, "builder");
26312630
}
26322631

26332632
private void appendToString(final StringBuilder sb, final String indent)

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,46 @@
2727
*/
2828
public class JavaUtil
2929
{
30+
public enum Separators
31+
{
32+
BEGIN_GROUP('['),
33+
END_GROUP(']'),
34+
BEGIN_COMPOSITE('('),
35+
END_COMPOSITE(')'),
36+
BEGIN_SET('{'),
37+
END_SET('}'),
38+
BEGIN_ARRAY('['),
39+
END_ARRAY(']'),
40+
FIELD('|'),
41+
KEY_VALUE('='),
42+
ENTRY(',');
43+
44+
public final char symbol;
45+
46+
Separators(char symbol)
47+
{
48+
this.symbol = symbol;
49+
}
50+
51+
/**
52+
* Add separator to a generated StringBuilder
53+
*
54+
* @param builder the code generation builder to which information should be added
55+
* @param indent the current generated code indentation
56+
* @param generatedBuilder the name of the generated StringBuilder to which separator should be added
57+
*/
58+
public void appendToGeneratedBuilder(StringBuilder builder, String indent, String generatedBuilder)
59+
{
60+
append(builder, indent, generatedBuilder + ".append('" + symbol + "');");
61+
}
62+
63+
@Override
64+
public String toString()
65+
{
66+
return String.valueOf(symbol);
67+
}
68+
}
69+
3070
private static final Map<PrimitiveType, String> TYPE_NAME_BY_PRIMITIVE_TYPE_MAP = new EnumMap<>(PrimitiveType.class);
3171

3272
static
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package uk.co.real_logic.sbe;
2+
3+
import baseline.*;
4+
import org.agrona.concurrent.UnsafeBuffer;
5+
import org.junit.BeforeClass;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.nio.ByteBuffer;
9+
10+
public class EncodedCarTestBase
11+
{
12+
protected static final MessageHeaderEncoder MESSAGE_HEADER = new MessageHeaderEncoder();
13+
protected static final CarEncoder CAR = new CarEncoder();
14+
15+
private static byte[] vehicleCode;
16+
private static byte[] manufacturerCode;
17+
private static byte[] make;
18+
private static byte[] model;
19+
20+
@BeforeClass
21+
public static void setupExampleData()
22+
{
23+
try
24+
{
25+
vehicleCode = "abcdef".getBytes(CarEncoder.vehicleCodeCharacterEncoding());
26+
manufacturerCode = "123".getBytes(EngineEncoder.manufacturerCodeCharacterEncoding());
27+
make = "Honda".getBytes(CarEncoder.makeCharacterEncoding());
28+
model = "Civic VTi".getBytes(CarEncoder.modelCharacterEncoding());
29+
}
30+
catch (final UnsupportedEncodingException ex)
31+
{
32+
throw new RuntimeException(ex);
33+
}
34+
}
35+
36+
protected static void encodeTestMessage(final ByteBuffer buffer)
37+
{
38+
final UnsafeBuffer directBuffer = new UnsafeBuffer(buffer);
39+
40+
int bufferOffset = 0;
41+
MESSAGE_HEADER
42+
.wrap(directBuffer, bufferOffset)
43+
.blockLength(CAR.sbeBlockLength())
44+
.templateId(CAR.sbeTemplateId())
45+
.schemaId(CAR.sbeSchemaId())
46+
.version(CAR.sbeSchemaVersion());
47+
48+
bufferOffset += MESSAGE_HEADER.encodedLength();
49+
50+
final int srcOffset = 0;
51+
52+
CAR.wrap(directBuffer, bufferOffset)
53+
.serialNumber(1234)
54+
.modelYear(2013)
55+
.available(BooleanType.T)
56+
.code(Model.A)
57+
.putVehicleCode(vehicleCode, srcOffset);
58+
59+
for (int i = 0, size = CarEncoder.someNumbersLength(); i < size; i++)
60+
{
61+
CAR.someNumbers(i, i);
62+
}
63+
64+
CAR.extras()
65+
.clear()
66+
.cruiseControl(true)
67+
.sportsPack(true)
68+
.sunRoof(false);
69+
70+
CAR.engine()
71+
.capacity(2000)
72+
.numCylinders((short)4)
73+
.putManufacturerCode(manufacturerCode, srcOffset);
74+
75+
CAR.fuelFiguresCount(3)
76+
.next().speed(30).mpg(35.9f)
77+
.next().speed(55).mpg(49.0f)
78+
.next().speed(75).mpg(40.0f);
79+
80+
final CarEncoder.PerformanceFiguresEncoder perfFigures = CAR.performanceFiguresCount(2);
81+
perfFigures.next()
82+
.octaneRating((short)95)
83+
.accelerationCount(3)
84+
.next().mph(30).seconds(4.0f)
85+
.next().mph(60).seconds(7.5f)
86+
.next().mph(100).seconds(12.2f);
87+
perfFigures.next()
88+
.octaneRating((short)99)
89+
.accelerationCount(3)
90+
.next().mph(30).seconds(3.8f)
91+
.next().mph(60).seconds(7.1f)
92+
.next().mph(100).seconds(11.8f);
93+
94+
CAR.make(new String(make));
95+
CAR.putModel(model, srcOffset, model.length);
96+
97+
bufferOffset += CAR.encodedLength();
98+
99+
buffer.position(bufferOffset);
100+
}
101+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package uk.co.real_logic.sbe.generation.java;
2+
3+
import org.junit.Test;
4+
import uk.co.real_logic.sbe.EncodedCarTestBase;
5+
6+
import java.nio.ByteBuffer;
7+
8+
import static junit.framework.TestCase.assertEquals;
9+
10+
public class ToStringTest extends EncodedCarTestBase
11+
{
12+
private static final int MSG_BUFFER_CAPACITY = 4 * 1024;
13+
14+
@Test
15+
public void exampleMessagePrinted() throws Exception
16+
{
17+
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY);
18+
encodeTestMessage(encodedMsgBuffer);
19+
20+
final String result = CAR.toString();
21+
assertEquals(
22+
"[Car]" +
23+
"(sbeTemplateId=1|sbeSchemaId=1|sbeSchemaVersion=0|sbeBlockLength=45):" +
24+
"serialNumber=1234|modelYear=2013|available=T|code=A|" +
25+
"someNumbers=[0,1,2,3,4]|" +
26+
"vehicleCode=abcdef|" +
27+
"extras={sportsPack,cruiseControl}|" +
28+
"engine=(capacity=2000|numCylinders=4|manufacturerCode=123|)|" +
29+
"fuelFigures=[" +
30+
"(speed=30|mpg=35.9|)," +
31+
"(speed=55|mpg=49.0|)," +
32+
"(speed=75|mpg=40.0|)]|" +
33+
"performanceFigures=[" +
34+
"(octaneRating=95|acceleration=[(mph=30|seconds=4.0|),(mph=60|seconds=7.5|),(mph=100|seconds=12.2|)]|)," +
35+
"(octaneRating=99|acceleration=[(mph=30|seconds=3.8|),(mph=60|seconds=7.1|),(mph=100|seconds=11.8|)]|)]|" +
36+
"make=Honda|model=Civic VTi|activationCode=|",
37+
result);
38+
}
39+
}

0 commit comments

Comments
 (0)