Skip to content

Commit 3c7ee72

Browse files
committed
[Java] Return NULL_VAL enum value for a unknown version rather than null so it can be used in a switch. PR #584.
1 parent 9c00681 commit 3c7ee72

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,19 +1722,24 @@ private static CharSequence generateStringNotPresentCondition(final int sinceVer
17221722
}
17231723

17241724
private static CharSequence generatePropertyNotPresentCondition(
1725-
final boolean inComposite, final CodecType codecType, final int sinceVersion, final String indent)
1725+
final boolean inComposite,
1726+
final CodecType codecType,
1727+
final Token propertyToken,
1728+
final String enumName,
1729+
final String indent)
17261730
{
1727-
if (inComposite || codecType == ENCODER || 0 == sinceVersion)
1731+
if (inComposite || codecType == ENCODER || 0 == propertyToken.version())
17281732
{
17291733
return "";
17301734
}
17311735

17321736
return String.format(
17331737
indent + " if (parentMessage.actingVersion < %d)\n" +
17341738
indent + " {\n" +
1735-
indent + " return null;\n" +
1739+
indent + " return %s;\n" +
17361740
indent + " }\n\n",
1737-
sinceVersion);
1741+
propertyToken.version(),
1742+
enumName == null ? "null" : (enumName + ".NULL_VAL"));
17381743
}
17391744

17401745
private CharSequence generatePrimitiveArrayPropertyDecode(
@@ -2452,15 +2457,15 @@ private static void generateFieldMetaAttributeMethod(final StringBuilder sb, fin
24522457

24532458
private CharSequence generateEnumDecoder(
24542459
final boolean inComposite,
2455-
final Token signalToken,
2460+
final Token fieldToken,
24562461
final String propertyName,
2457-
final Token token,
2462+
final Token typeToken,
24582463
final String indent)
24592464
{
2460-
final String enumName = formatClassName(token.applicableTypeName());
2461-
final Encoding encoding = token.encoding();
2465+
final String enumName = formatClassName(typeToken.applicableTypeName());
2466+
final Encoding encoding = typeToken.encoding();
24622467

2463-
if (token.isConstantEncoding())
2468+
if (typeToken.isConstantEncoding())
24642469
{
24652470
return String.format(
24662471
"\n" +
@@ -2470,7 +2475,7 @@ private CharSequence generateEnumDecoder(
24702475
indent + " }\n\n",
24712476
enumName,
24722477
propertyName,
2473-
signalToken.encoding().constValue().toString());
2478+
fieldToken.encoding().constValue().toString());
24742479
}
24752480
else
24762481
{
@@ -2483,9 +2488,9 @@ private CharSequence generateEnumDecoder(
24832488
indent + " }\n\n",
24842489
enumName,
24852490
propertyName,
2486-
generatePropertyNotPresentCondition(inComposite, DECODER, signalToken.version(), indent),
2491+
generatePropertyNotPresentCondition(inComposite, DECODER, fieldToken, enumName, indent),
24872492
enumName,
2488-
generateGet(encoding.primitiveType(), "offset + " + token.offset(), byteOrderString(encoding)));
2493+
generateGet(encoding.primitiveType(), "offset + " + typeToken.offset(), byteOrderString(encoding)));
24892494
}
24902495
}
24912496

@@ -2541,7 +2546,7 @@ private CharSequence generateBitSetProperty(
25412546
generateFlyweightPropertyJavadoc(indent + INDENT, propertyToken, bitSetName),
25422547
bitSetName,
25432548
propertyName,
2544-
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken.version(), indent),
2549+
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken, null, indent),
25452550
propertyName,
25462551
bitsetToken.offset(),
25472552
propertyName));
@@ -2577,7 +2582,7 @@ private CharSequence generateCompositeProperty(
25772582
generateFlyweightPropertyJavadoc(indent + INDENT, propertyToken, compositeName),
25782583
compositeName,
25792584
propertyName,
2580-
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken.version(), indent),
2585+
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken, null, indent),
25812586
propertyName,
25822587
compositeToken.offset(),
25832588
propertyName));

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/SchemaExtensionTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.util.Map;
3232

33+
import static org.hamcrest.CoreMatchers.is;
3334
import static org.junit.Assert.*;
3435
import static uk.co.real_logic.sbe.generation.java.ReflectionUtil.get;
3536
import static uk.co.real_logic.sbe.generation.java.ReflectionUtil.set;
@@ -87,7 +88,7 @@ public void testMessage1() throws Exception
8788
assertEquals(100, get(decoderVersion0, "tag1"));
8889
assertEquals(Integer.MIN_VALUE, get(decoderVersion0, "tag2"));
8990
assertNull(get(decoderVersion0, "tag3"));
90-
assertNull(get(decoderVersion0, "tag4"));
91+
assertThat(get(decoderVersion0, "tag4").toString(), is("NULL_VAL"));
9192
assertNull(get(decoderVersion0, "tag5"));
9293

9394
assertEquals(0, decoderVersion0.getClass().getMethod("tag1SinceVersion").invoke(null));
@@ -102,7 +103,7 @@ public void testMessage1() throws Exception
102103
assertEquals(100, get(decoderVersion1, "tag1"));
103104
assertEquals(200, get(decoderVersion1, "tag2"));
104105
assertNull(get(decoderVersion1, "tag3"));
105-
assertNull(get(decoderVersion1, "tag4"));
106+
assertThat(get(decoderVersion1, "tag4").toString(), is("NULL_VAL"));
106107
assertNull(get(decoderVersion1, "tag5"));
107108
}
108109

@@ -113,7 +114,7 @@ public void testMessage1() throws Exception
113114
final Object compositeDecoder2 = get(decoderVersion2, "tag3");
114115
assertNotNull(compositeDecoder2);
115116
assertEquals(300, get(compositeDecoder2, "value"));
116-
assertNull(get(decoderVersion2, "tag4"));
117+
assertThat(get(decoderVersion2, "tag4").toString(), is("NULL_VAL"));
117118
assertNull(get(decoderVersion2, "tag5"));
118119
}
119120

@@ -172,7 +173,7 @@ public void testMessage2() throws Exception
172173
assertEquals(100, get(decoderVersion0, "tag1"));
173174
assertEquals(Integer.MIN_VALUE, get(decoderVersion0, "tag2"));
174175
assertNull(get(decoderVersion0, "tag3"));
175-
assertNull(get(decoderVersion0, "tag4"));
176+
assertThat(get(decoderVersion0, "tag4").toString(), is("NULL_VAL"));
176177
assertNull(get(decoderVersion0, "tag5"));
177178

178179
assertEquals(0, decoderVersion0.getClass().getMethod("tag1SinceVersion").invoke(null));
@@ -189,7 +190,7 @@ public void testMessage2() throws Exception
189190
final Object compositeDecoder2 = get(decoderVersion1, "tag3");
190191
assertNotNull(compositeDecoder2);
191192
assertEquals(300, get(compositeDecoder2, "value"));
192-
assertNull(get(decoderVersion1, "tag4"));
193+
assertThat(get(decoderVersion1, "tag4").toString(), is("NULL_VAL"));
193194
assertNull(get(decoderVersion1, "tag5"));
194195
}
195196

@@ -200,7 +201,7 @@ public void testMessage2() throws Exception
200201
final Object compositeDecoder2 = get(decoderVersion2, "tag3");
201202
assertNotNull(compositeDecoder2);
202203
assertEquals(300, get(compositeDecoder2, "value"));
203-
assertNull(get(decoderVersion2, "tag4"));
204+
assertThat(get(decoderVersion2, "tag4").toString(), is("NULL_VAL"));
204205
assertNull(get(decoderVersion2, "tag5"));
205206
}
206207

@@ -211,7 +212,7 @@ public void testMessage2() throws Exception
211212
final Object compositeDecoder3 = get(decoderVersion3, "tag3");
212213
assertNotNull(compositeDecoder3);
213214
assertEquals(300, get(compositeDecoder3, "value"));
214-
assertNull(get(decoderVersion3, "tag4"));
215+
assertThat(get(decoderVersion3, "tag4").toString(), is("NULL_VAL"));
215216
final Object setDecoder = get(decoderVersion3, "tag5");
216217
assertNotNull(setDecoder);
217218
assertEquals(false, get(setDecoder, "firstChoice"));

0 commit comments

Comments
 (0)