Skip to content

Commit abd5b4c

Browse files
committed
[Java] Added static methods for determining if an choice is set in a bitset for a raw value. Issue #489.
1 parent a44e7b6 commit abd5b4c

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,16 +1164,45 @@ private CharSequence generateChoiceDecoders(final List<Token> tokens)
11641164
final Encoding encoding = token.encoding();
11651165
final String choiceBitIndex = encoding.constValue().toString();
11661166
final String byteOrderStr = byteOrderString(encoding);
1167+
final PrimitiveType primitiveType = encoding.primitiveType();
1168+
final String argType;
1169+
1170+
switch (primitiveType)
1171+
{
1172+
case UINT8:
1173+
argType = "byte";
1174+
break;
1175+
1176+
case UINT16:
1177+
argType = "short";
1178+
break;
1179+
1180+
case UINT32:
1181+
argType = "int";
1182+
break;
1183+
1184+
case UINT64:
1185+
argType = "long";
1186+
break;
1187+
1188+
default:
1189+
throw new IllegalStateException("Invalid type: " + primitiveType);
1190+
}
11671191

11681192
return String.format(
11691193
"\n" +
1170-
" public boolean %s()\n" +
1194+
" public boolean %1$s()\n" +
11711195
" {\n" +
1172-
" return %s;\n" +
1196+
" return %2$s;\n" +
1197+
" }\n\n" +
1198+
" public static boolean %1$s(final %3$s value)\n" +
1199+
" {\n" +
1200+
" return %4$s;\n" +
11731201
" }\n",
11741202
choiceName,
1175-
generateChoiceGet(encoding.primitiveType(), choiceBitIndex, byteOrderStr)
1176-
);
1203+
generateChoiceGet(primitiveType, choiceBitIndex, byteOrderStr),
1204+
argType,
1205+
generateStaticChoiceGet(primitiveType, choiceBitIndex));
11771206
});
11781207
}
11791208

@@ -2602,6 +2631,26 @@ private String generateChoiceGet(final PrimitiveType type, final String bitIndex
26022631
throw new IllegalArgumentException("primitive type not supported: " + type);
26032632
}
26042633

2634+
private String generateStaticChoiceGet(final PrimitiveType type, final String bitIndex)
2635+
{
2636+
switch (type)
2637+
{
2638+
case UINT8:
2639+
return "0 != (value & (1 << " + bitIndex + "))";
2640+
2641+
case UINT16:
2642+
return "0 != (value & (1 << " + bitIndex + "))";
2643+
2644+
case UINT32:
2645+
return "0 != (value & (1 << " + bitIndex + "))";
2646+
2647+
case UINT64:
2648+
return "0 != (value & (1L << " + bitIndex + "))";
2649+
}
2650+
2651+
throw new IllegalArgumentException("primitive type not supported: " + type);
2652+
}
2653+
26052654
private String generateChoicePut(
26062655
final PrimitiveType type, final String bitIdx, final String byteOrder)
26072656
{

0 commit comments

Comments
 (0)