Skip to content

[Java] Generate put CharSequence methods for ASCII encoded fields #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,32 @@ private void generateDataEncodeMethods(
byteOrderStr,
indent);

if (null == characterEncoding)
if (null != characterEncoding)
{
return;
generateCharArrayEncodeMethods(
sb,
propertyName,
sizeOfLengthField,
maxLengthValue,
lengthType,
byteOrderStr,
characterEncoding,
className,
indent);
}
}

private void generateCharArrayEncodeMethods(
final StringBuilder sb,
final String propertyName,
final int sizeOfLengthField,
final int maxLengthValue,
final PrimitiveType lengthType,
final String byteOrderStr,
final String characterEncoding,
final String className,
final String indent)
{
if (characterEncoding.contains("ASCII"))
{
sb.append(String.format("\n" +
Expand All @@ -864,6 +885,32 @@ private void generateDataEncodeMethods(
maxLengthValue,
sizeOfLengthField,
generatePut(lengthType, "limit", "length", byteOrderStr)));

sb.append(String.format("\n" +
indent + " public %1$s %2$s(final CharSequence value)\n" +
indent + " {\n" +
indent + " final int length = value.length();\n" +
indent + " if (length > %3$d)\n" +
indent + " {\n" +
indent + " throw new IllegalStateException(\"length > maxValue for type: \" + length);\n" +
indent + " }\n\n" +
indent + " final int headerLength = %4$d;\n" +
indent + " final int limit = parentMessage.limit();\n" +
indent + " parentMessage.limit(limit + headerLength + length);\n" +
indent + " %5$s;\n" +
indent + " for (int i = 0; i < length; ++i)\n" +
indent + " {\n" +
indent + " final char charValue = value.charAt(i);\n" +
indent + " final byte byteValue = charValue > 127 ? (byte)'?' : (byte)charValue;\n" +
indent + " buffer.putByte(limit + headerLength + i, byteValue);\n" +
indent + " }\n\n" +
indent + " return this;\n" +
indent + " }\n",
className,
formatPropertyName(propertyName),
maxLengthValue,
sizeOfLengthField,
generatePut(lengthType, "limit", "length", byteOrderStr)));
}
else
{
Expand Down Expand Up @@ -1858,6 +1905,32 @@ private void generateCharArrayEncodeMethods(
propertyName,
fieldLength,
offset));
sb.append(String.format("\n" +
indent + " public %1$s %2$s(final CharSequence src)\n" +
indent + " {\n" +
indent + " final int length = %3$d;\n" +
indent + " final int srcLength = src.length();\n" +
indent + " if (srcLength > length)\n" +
indent + " {\n" +
indent + " throw new IndexOutOfBoundsException(" +
"\"CharSequence too large for copy: byte length=\" + srcLength);\n" +
indent + " }\n\n" +
indent + " for (int i = 0; i < srcLength; ++i)\n" +
indent + " {\n" +
indent + " final char charValue = src.charAt(i);\n" +
indent + " final byte byteValue = charValue > 127 ? (byte)'?' : (byte)charValue;\n" +
indent + " buffer.putByte(this.offset + %4$d + i, byteValue);\n" +
indent + " }\n\n" +
indent + " for (int i = srcLength; i < length; ++i)\n" +
indent + " {\n" +
indent + " buffer.putByte(this.offset + %4$d + i, (byte)0);\n" +
indent + " }\n\n" +
indent + " return this;\n" +
indent + " }\n",
formatClassName(containingClassName),
propertyName,
fieldLength,
offset));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@ public void shouldGenerateGetString() throws Exception
assertThat(get(decoder, "vehicleCode"), is("R11R12"));
}

@Test
public void shouldGeneratePutCharSequence() throws Exception
{
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]);
generator().generate();

final Object encoder = wrap(buffer, compileCarEncoder().newInstance());
final Object decoder = getCarDecoder(buffer, encoder);

set(encoder, "vehicleCode", CharSequence.class, "R11");
assertThat(get(decoder, "vehicleCode"), is("R11"));

set(encoder, "vehicleCode", CharSequence.class, "");
assertThat(get(decoder, "vehicleCode"), is(""));

set(encoder, "vehicleCode", CharSequence.class, "R11R12");
assertThat(get(decoder, "vehicleCode"), is("R11R12"));
}

private Class<?> getModelClass(final Object encoder) throws ClassNotFoundException
{
final String className = "Model";
Expand Down