@@ -53,6 +53,34 @@ public static JBBPDslBuilder Begin() {
53
53
return new JBBPDslBuilder ();
54
54
}
55
55
56
+ protected void addItem (final Item item ) {
57
+ if (item .name == null || item .name .length () == 0 ) {
58
+ this .items .add (item );
59
+ } else {
60
+ int structCounter = 0 ;
61
+ for (int i = this .items .size () - 1 ; i >= 0 ; i --) {
62
+ final Item itm = this .items .get (i );
63
+
64
+ if (itm .type == BinType .STRUCT || itm .type == BinType .STRUCT_ARRAY ) {
65
+ if (structCounter == 0 ) {
66
+ break ;
67
+ }
68
+ structCounter ++;
69
+ } else if (itm instanceof ItemStructEnd ) {
70
+ structCounter --;
71
+ }
72
+
73
+ if (structCounter == 0 ) {
74
+ final String thatName = itm .name ;
75
+ if (thatName != null && thatName .length () > 0 && item .name .equalsIgnoreCase (thatName )) {
76
+ throw new IllegalArgumentException ("Duplicated item name '" + item .name + '\'' );
77
+ }
78
+ }
79
+ }
80
+ this .items .add (item );
81
+ }
82
+ }
83
+
56
84
protected static String assertTextNotNullAndTrimmedNotEmpty (final String text ) {
57
85
if (text == null ) {
58
86
throw new NullPointerException ("Must not be null" );
@@ -175,7 +203,7 @@ public JBBPDslBuilder Align(final int alignBytes) {
175
203
* @return the builder instance, must not be null
176
204
*/
177
205
public JBBPDslBuilder Align (final String sizeExpression ) {
178
- this .items . add (new ItemAlign (assertExpressionChars (sizeExpression )));
206
+ this .addItem (new ItemAlign (assertExpressionChars (sizeExpression )));
179
207
return this ;
180
208
}
181
209
@@ -207,7 +235,7 @@ public JBBPDslBuilder Skip(final int bytesToSkip) {
207
235
* @return the builder instance, must not be null
208
236
*/
209
237
public JBBPDslBuilder Skip (final String sizeExpression ) {
210
- this .items . add (new ItemSkip (assertExpressionChars (sizeExpression )));
238
+ this .addItem (new ItemSkip (assertExpressionChars (sizeExpression )));
211
239
return this ;
212
240
}
213
241
@@ -273,7 +301,7 @@ public JBBPDslBuilder Custom(final String type, final String name) {
273
301
public JBBPDslBuilder Custom (final String type , final String name , final String param ) {
274
302
final ItemCustom custom = new ItemCustom (type , name , this .byteOrder );
275
303
custom .bitLenExpression = param == null ? null : assertExpressionChars (param );
276
- this .items . add (custom );
304
+ this .addItem (custom );
277
305
return this ;
278
306
}
279
307
@@ -294,7 +322,7 @@ public JBBPDslBuilder Struct() {
294
322
*/
295
323
public JBBPDslBuilder Struct (final String name ) {
296
324
final Item item = new Item (BinType .STRUCT , name , this .byteOrder );
297
- this .items . add (item );
325
+ this .addItem (item );
298
326
this .openedStructCounter ++;
299
327
return this ;
300
328
}
@@ -340,7 +368,7 @@ public JBBPDslBuilder StructArray(final String name, final int size) {
340
368
public JBBPDslBuilder StructArray (final String name , final String sizeExpression ) {
341
369
final Item item = new Item (BinType .STRUCT_ARRAY , name , this .byteOrder );
342
370
item .sizeExpression = assertExpressionChars (sizeExpression );
343
- this .items . add (item );
371
+ this .addItem (item );
344
372
this .openedStructCounter ++;
345
373
return this ;
346
374
}
@@ -357,7 +385,7 @@ public JBBPDslBuilder Val(final String name, final String expression) {
357
385
assertTextNotNullAndTrimmedNotEmpty (name ),
358
386
assertExpressionChars (assertTextNotNullAndTrimmedNotEmpty (expression )).trim ()
359
387
);
360
- this .items . add (item );
388
+ this .addItem (item );
361
389
return this ;
362
390
}
363
391
@@ -369,7 +397,7 @@ public JBBPDslBuilder Val(final String name, final String expression) {
369
397
*/
370
398
public JBBPDslBuilder ResetCounter () {
371
399
final Item item = new ItemResetCounter ();
372
- this .items . add (item );
400
+ this .addItem (item );
373
401
return this ;
374
402
}
375
403
@@ -511,7 +539,7 @@ public JBBPDslBuilder CustomArray(final String type, final String name, final St
511
539
item .array = true ;
512
540
item .bitLenExpression = param == null ? null : assertExpressionChars (param );
513
541
item .sizeExpression = assertExpressionChars (sizeExpression );
514
- this .items . add (item );
542
+ this .addItem (item );
515
543
return this ;
516
544
}
517
545
@@ -536,7 +564,7 @@ public JBBPDslBuilder CloseStruct(final boolean closeAllOpened) {
536
564
if (this .openedStructCounter == 0 ) {
537
565
throw new IllegalStateException ("There is not any opened struct" );
538
566
}
539
- this .items . add (new ItemStructEnd (closeAllOpened ));
567
+ this .addItem (new ItemStructEnd (closeAllOpened ));
540
568
this .openedStructCounter = closeAllOpened ? 0 : this .openedStructCounter - 1 ;
541
569
return this ;
542
570
}
@@ -599,7 +627,7 @@ public JBBPDslBuilder Bits(final JBBPBitNumber bits) {
599
627
public JBBPDslBuilder Bits (final String name , final JBBPBitNumber bits ) {
600
628
final Item item = new Item (BinType .BIT , name , this .byteOrder );
601
629
item .bitNumber = bits ;
602
- this .items . add (item );
630
+ this .addItem (item );
603
631
return this ;
604
632
}
605
633
@@ -613,7 +641,7 @@ public JBBPDslBuilder Bits(final String name, final JBBPBitNumber bits) {
613
641
public JBBPDslBuilder Bits (final String name , final String bitLenExpression ) {
614
642
final Item item = new Item (BinType .BIT , name , this .byteOrder );
615
643
item .bitLenExpression = assertExpressionChars (bitLenExpression );
616
- this .items . add (item );
644
+ this .addItem (item );
617
645
return this ;
618
646
}
619
647
@@ -686,7 +714,7 @@ public JBBPDslBuilder BitArray(final String name, final JBBPBitNumber bits, fina
686
714
final Item item = new Item (BinType .BIT_ARRAY , name , this .byteOrder );
687
715
item .bitNumber = bits ;
688
716
item .sizeExpression = assertExpressionChars (sizeExpression );
689
- this .items . add (item );
717
+ this .addItem (item );
690
718
return this ;
691
719
}
692
720
@@ -702,7 +730,7 @@ public JBBPDslBuilder BitArray(final String name, final String bitLenExpression,
702
730
final Item item = new Item (BinType .BIT_ARRAY , name , this .byteOrder );
703
731
item .bitLenExpression = assertExpressionChars (bitLenExpression );
704
732
item .sizeExpression = assertExpressionChars (sizeExpression );
705
- this .items . add (item );
733
+ this .addItem (item );
706
734
return this ;
707
735
}
708
736
@@ -747,7 +775,7 @@ public JBBPDslBuilder BoolArray(final int size) {
747
775
public JBBPDslBuilder BoolArray (final String name , final String sizeExpression ) {
748
776
final Item item = new Item (BinType .BOOL_ARRAY , name , this .byteOrder );
749
777
item .sizeExpression = assertExpressionChars (sizeExpression );
750
- this .items . add (item );
778
+ this .addItem (item );
751
779
return this ;
752
780
}
753
781
@@ -768,7 +796,7 @@ public JBBPDslBuilder Bool() {
768
796
*/
769
797
public JBBPDslBuilder Bool (final String name ) {
770
798
final Item item = new Item (BinType .BOOL , name , this .byteOrder );
771
- this .items . add (item );
799
+ this .addItem (item );
772
800
return this ;
773
801
}
774
802
@@ -789,7 +817,7 @@ public JBBPDslBuilder Byte() {
789
817
*/
790
818
public JBBPDslBuilder Byte (final String name ) {
791
819
final Item item = new Item (BinType .BYTE , name , this .byteOrder );
792
- this .items . add (item );
820
+ this .addItem (item );
793
821
return this ;
794
822
}
795
823
@@ -834,7 +862,7 @@ public JBBPDslBuilder ByteArray(final String name, final int size) {
834
862
public JBBPDslBuilder ByteArray (final String name , final String sizeExpression ) {
835
863
final Item item = new Item (BinType .BYTE_ARRAY , name , this .byteOrder );
836
864
item .sizeExpression = assertExpressionChars (sizeExpression );
837
- this .items . add (item );
865
+ this .addItem (item );
838
866
return this ;
839
867
}
840
868
@@ -855,7 +883,7 @@ public JBBPDslBuilder UByte() {
855
883
*/
856
884
public JBBPDslBuilder UByte (final String name ) {
857
885
final Item item = new Item (BinType .UBYTE , name , this .byteOrder );
858
- this .items . add (item );
886
+ this .addItem (item );
859
887
return this ;
860
888
}
861
889
@@ -900,7 +928,7 @@ public JBBPDslBuilder UByteArray(final String name, final int size) {
900
928
public JBBPDslBuilder UByteArray (final String name , final String sizeExpression ) {
901
929
final Item item = new Item (BinType .UBYTE_ARRAY , name , this .byteOrder );
902
930
item .sizeExpression = assertExpressionChars (sizeExpression );
903
- this .items . add (item );
931
+ this .addItem (item );
904
932
return this ;
905
933
}
906
934
@@ -921,7 +949,7 @@ public JBBPDslBuilder Short() {
921
949
*/
922
950
public JBBPDslBuilder Short (final String name ) {
923
951
final Item item = new Item (BinType .SHORT , name , this .byteOrder );
924
- this .items . add (item );
952
+ this .addItem (item );
925
953
return this ;
926
954
}
927
955
@@ -966,7 +994,7 @@ public JBBPDslBuilder ShortArray(final String name, final int size) {
966
994
public JBBPDslBuilder ShortArray (final String name , final String sizeExpression ) {
967
995
final Item item = new Item (BinType .SHORT_ARRAY , name , this .byteOrder );
968
996
item .sizeExpression = assertExpressionChars (sizeExpression );
969
- this .items . add (item );
997
+ this .addItem (item );
970
998
return this ;
971
999
}
972
1000
@@ -987,7 +1015,7 @@ public JBBPDslBuilder UShort() {
987
1015
*/
988
1016
public JBBPDslBuilder UShort (final String name ) {
989
1017
final Item item = new Item (BinType .USHORT , name , this .byteOrder );
990
- this .items . add (item );
1018
+ this .addItem (item );
991
1019
return this ;
992
1020
}
993
1021
@@ -1021,7 +1049,7 @@ public JBBPDslBuilder UShortArray(final int size) {
1021
1049
public JBBPDslBuilder UShortArray (final String name , final String sizeExpression ) {
1022
1050
final Item item = new Item (BinType .USHORT_ARRAY , name , this .byteOrder );
1023
1051
item .sizeExpression = assertExpressionChars (sizeExpression );
1024
- this .items . add (item );
1052
+ this .addItem (item );
1025
1053
return this ;
1026
1054
}
1027
1055
@@ -1053,7 +1081,7 @@ public JBBPDslBuilder Int() {
1053
1081
*/
1054
1082
public JBBPDslBuilder Int (final String name ) {
1055
1083
final Item item = new Item (BinType .INT , name , this .byteOrder );
1056
- this .items . add (item );
1084
+ this .addItem (item );
1057
1085
return this ;
1058
1086
}
1059
1087
@@ -1098,7 +1126,7 @@ public JBBPDslBuilder IntArray(final String name, final int size) {
1098
1126
public JBBPDslBuilder IntArray (final String name , final String sizeExpression ) {
1099
1127
final Item item = new Item (BinType .INT_ARRAY , name , this .byteOrder );
1100
1128
item .sizeExpression = assertExpressionChars (sizeExpression );
1101
- this .items . add (item );
1129
+ this .addItem (item );
1102
1130
return this ;
1103
1131
}
1104
1132
@@ -1119,7 +1147,7 @@ public JBBPDslBuilder Long() {
1119
1147
*/
1120
1148
public JBBPDslBuilder Long (final String name ) {
1121
1149
final Item item = new Item (BinType .LONG , name , this .byteOrder );
1122
- this .items . add (item );
1150
+ this .addItem (item );
1123
1151
return this ;
1124
1152
}
1125
1153
@@ -1164,7 +1192,7 @@ public JBBPDslBuilder LongArray(final String name, final int size) {
1164
1192
public JBBPDslBuilder LongArray (final String name , final String sizeExpression ) {
1165
1193
final Item item = new Item (BinType .LONG_ARRAY , name , this .byteOrder );
1166
1194
item .sizeExpression = assertExpressionChars (sizeExpression );
1167
- this .items . add (item );
1195
+ this .addItem (item );
1168
1196
return this ;
1169
1197
}
1170
1198
@@ -1185,7 +1213,7 @@ public JBBPDslBuilder Float() {
1185
1213
*/
1186
1214
public JBBPDslBuilder Float (final String name ) {
1187
1215
final Item item = new Item (BinType .FLOAT , name , this .byteOrder );
1188
- this .items . add (item );
1216
+ this .addItem (item );
1189
1217
return this ;
1190
1218
}
1191
1219
@@ -1230,7 +1258,7 @@ public JBBPDslBuilder FloatArray(final String name, final int size) {
1230
1258
public JBBPDslBuilder FloatArray (final String name , final String sizeExpression ) {
1231
1259
final Item item = new Item (BinType .FLOAT_ARRAY , name , this .byteOrder );
1232
1260
item .sizeExpression = assertExpressionChars (sizeExpression );
1233
- this .items . add (item );
1261
+ this .addItem (item );
1234
1262
return this ;
1235
1263
}
1236
1264
@@ -1251,7 +1279,7 @@ public JBBPDslBuilder Double() {
1251
1279
*/
1252
1280
public JBBPDslBuilder Double (final String name ) {
1253
1281
final Item item = new Item (BinType .DOUBLE , name , this .byteOrder );
1254
- this .items . add (item );
1282
+ this .addItem (item );
1255
1283
return this ;
1256
1284
}
1257
1285
@@ -1262,7 +1290,7 @@ public JBBPDslBuilder Double(final String name) {
1262
1290
* @return the builder instance, must not be null
1263
1291
*/
1264
1292
public JBBPDslBuilder Comment (final String text ) {
1265
- this .items . add (new ItemComment (text == null ? "" : text ));
1293
+ this .addItem (new ItemComment (text == null ? "" : text ));
1266
1294
return this ;
1267
1295
}
1268
1296
@@ -1307,7 +1335,7 @@ public JBBPDslBuilder DoubleArray(final String name, final int size) {
1307
1335
public JBBPDslBuilder DoubleArray (final String name , final String sizeExpression ) {
1308
1336
final Item item = new Item (BinType .DOUBLE_ARRAY , name , this .byteOrder );
1309
1337
item .sizeExpression = assertExpressionChars (sizeExpression );
1310
- this .items . add (item );
1338
+ this .addItem (item );
1311
1339
return this ;
1312
1340
}
1313
1341
@@ -1327,7 +1355,7 @@ public JBBPDslBuilder String() {
1327
1355
*/
1328
1356
public JBBPDslBuilder String (final String name ) {
1329
1357
final Item item = new Item (BinType .STRING , name , this .byteOrder );
1330
- this .items . add (item );
1358
+ this .addItem (item );
1331
1359
return this ;
1332
1360
}
1333
1361
@@ -1372,7 +1400,7 @@ public JBBPDslBuilder StringArray(final String name, final int size) {
1372
1400
public JBBPDslBuilder StringArray (final String name , final String sizeExpression ) {
1373
1401
final Item item = new Item (BinType .STRING_ARRAY , name , this .byteOrder );
1374
1402
item .sizeExpression = assertExpressionChars (sizeExpression );
1375
- this .items . add (item );
1403
+ this .addItem (item );
1376
1404
return this ;
1377
1405
}
1378
1406
0 commit comments