|
15 | 15 | */ |
16 | 16 | package uk.co.real_logic.sbe.generation.java; |
17 | 17 |
|
| 18 | +import uk.co.real_logic.sbe.PrimitiveType; |
18 | 19 | import uk.co.real_logic.sbe.generation.CodeGenerator; |
19 | 20 | import uk.co.real_logic.sbe.generation.OutputManager; |
20 | 21 | import uk.co.real_logic.sbe.ir.Encoding; |
@@ -361,12 +362,82 @@ private CharSequence generatePrimitivePropertyMethods( |
361 | 362 | } |
362 | 363 | else if (arrayLength > 1) |
363 | 364 | { |
364 | | - return generateSingleValueProperty(containingClassName, propertyName, javaTypeName+"[]", indent); |
| 365 | + return generateArrayProperty(encoding, containingClassName, propertyName, javaTypeName, indent); |
365 | 366 | } |
366 | 367 |
|
367 | 368 | return ""; |
368 | 369 | } |
369 | 370 |
|
| 371 | + private CharSequence generateArrayProperty( |
| 372 | + final Encoding encoding, |
| 373 | + final String containingClassName, final String propertyName, |
| 374 | + final String javaTypeName, final String indent) |
| 375 | + { |
| 376 | + final StringBuilder sb = new StringBuilder(); |
| 377 | + |
| 378 | + sb.append(String.format( |
| 379 | + "\n" + indent + " private %s[] %s;\n", |
| 380 | + javaTypeName, |
| 381 | + propertyName |
| 382 | + )); |
| 383 | + |
| 384 | + sb.append(String.format( |
| 385 | + indent + " public %1$s %2$s(final int index)\n" + |
| 386 | + indent + " {\n" + |
| 387 | + indent + " return %2$s[index];\n" + |
| 388 | + indent + " }\n\n", |
| 389 | + javaTypeName, |
| 390 | + propertyName |
| 391 | + )); |
| 392 | + |
| 393 | + sb.append(String.format( |
| 394 | + indent + " public void %1$s(final int index, final %2$s value)\n" + |
| 395 | + indent + " {\n" + |
| 396 | + indent + " %1$s[index] = value;\n" + |
| 397 | + indent + " }\n", |
| 398 | + propertyName, |
| 399 | + javaTypeName |
| 400 | + )); |
| 401 | + |
| 402 | + if (encoding.primitiveType() == PrimitiveType.CHAR) |
| 403 | + { |
| 404 | + sb.append(String.format( |
| 405 | + "\n" + |
| 406 | + indent + " public int get%1$s(final byte[] dst, final int dstOffset)\n" + |
| 407 | + indent + " {\n" + |
| 408 | + indent + " System.arraycopy(%2$s, 0, dst, dstOffset, %2$sLength());\n" + |
| 409 | + indent + " return %2$sLength();\n" + |
| 410 | + indent + " }\n\n", |
| 411 | + JavaUtil.toUpperFirstChar(propertyName), |
| 412 | + propertyName |
| 413 | + )); |
| 414 | + |
| 415 | + sb.append(String.format( |
| 416 | + indent + " public %1$s put%2$s(final byte[] src, final int srcOffset)\n" + |
| 417 | + indent + " {\n" + |
| 418 | + indent + " System.arraycopy(src, srcOffset, %3$s, 0, src.length - srcOffset);\n" + |
| 419 | + indent + " return this;\n" + |
| 420 | + indent + " }\n", |
| 421 | + containingClassName, |
| 422 | + JavaUtil.toUpperFirstChar(propertyName), |
| 423 | + propertyName |
| 424 | + )); |
| 425 | + |
| 426 | + sb.append(String.format( |
| 427 | + indent + " public %1$s put%2$s(final byte[] src)\n" + |
| 428 | + indent + " {\n" + |
| 429 | + indent + " %3$s = Arrays.copyOf(src, %3$sLength());\n" + |
| 430 | + indent + " return this;\n" + |
| 431 | + indent + " }\n", |
| 432 | + containingClassName, |
| 433 | + JavaUtil.toUpperFirstChar(propertyName), |
| 434 | + propertyName |
| 435 | + )); |
| 436 | + } |
| 437 | + |
| 438 | + return sb; |
| 439 | + } |
| 440 | + |
370 | 441 | private CharSequence generateSingleValueProperty( |
371 | 442 | final String containingClassName, final String propertyName, final String javaTypeName, final String indent) |
372 | 443 | { |
|
0 commit comments