@@ -53,6 +53,34 @@ public static JBBPDslBuilder Begin() {
5353 return new JBBPDslBuilder ();
5454 }
5555
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+
5684 protected static String assertTextNotNullAndTrimmedNotEmpty (final String text ) {
5785 if (text == null ) {
5886 throw new NullPointerException ("Must not be null" );
@@ -175,7 +203,7 @@ public JBBPDslBuilder Align(final int alignBytes) {
175203 * @return the builder instance, must not be null
176204 */
177205 public JBBPDslBuilder Align (final String sizeExpression ) {
178- this .items . add (new ItemAlign (assertExpressionChars (sizeExpression )));
206+ this .addItem (new ItemAlign (assertExpressionChars (sizeExpression )));
179207 return this ;
180208 }
181209
@@ -207,7 +235,7 @@ public JBBPDslBuilder Skip(final int bytesToSkip) {
207235 * @return the builder instance, must not be null
208236 */
209237 public JBBPDslBuilder Skip (final String sizeExpression ) {
210- this .items . add (new ItemSkip (assertExpressionChars (sizeExpression )));
238+ this .addItem (new ItemSkip (assertExpressionChars (sizeExpression )));
211239 return this ;
212240 }
213241
@@ -273,7 +301,7 @@ public JBBPDslBuilder Custom(final String type, final String name) {
273301 public JBBPDslBuilder Custom (final String type , final String name , final String param ) {
274302 final ItemCustom custom = new ItemCustom (type , name , this .byteOrder );
275303 custom .bitLenExpression = param == null ? null : assertExpressionChars (param );
276- this .items . add (custom );
304+ this .addItem (custom );
277305 return this ;
278306 }
279307
@@ -294,7 +322,7 @@ public JBBPDslBuilder Struct() {
294322 */
295323 public JBBPDslBuilder Struct (final String name ) {
296324 final Item item = new Item (BinType .STRUCT , name , this .byteOrder );
297- this .items . add (item );
325+ this .addItem (item );
298326 this .openedStructCounter ++;
299327 return this ;
300328 }
@@ -340,7 +368,7 @@ public JBBPDslBuilder StructArray(final String name, final int size) {
340368 public JBBPDslBuilder StructArray (final String name , final String sizeExpression ) {
341369 final Item item = new Item (BinType .STRUCT_ARRAY , name , this .byteOrder );
342370 item .sizeExpression = assertExpressionChars (sizeExpression );
343- this .items . add (item );
371+ this .addItem (item );
344372 this .openedStructCounter ++;
345373 return this ;
346374 }
@@ -357,7 +385,7 @@ public JBBPDslBuilder Val(final String name, final String expression) {
357385 assertTextNotNullAndTrimmedNotEmpty (name ),
358386 assertExpressionChars (assertTextNotNullAndTrimmedNotEmpty (expression )).trim ()
359387 );
360- this .items . add (item );
388+ this .addItem (item );
361389 return this ;
362390 }
363391
@@ -369,7 +397,7 @@ public JBBPDslBuilder Val(final String name, final String expression) {
369397 */
370398 public JBBPDslBuilder ResetCounter () {
371399 final Item item = new ItemResetCounter ();
372- this .items . add (item );
400+ this .addItem (item );
373401 return this ;
374402 }
375403
@@ -511,7 +539,7 @@ public JBBPDslBuilder CustomArray(final String type, final String name, final St
511539 item .array = true ;
512540 item .bitLenExpression = param == null ? null : assertExpressionChars (param );
513541 item .sizeExpression = assertExpressionChars (sizeExpression );
514- this .items . add (item );
542+ this .addItem (item );
515543 return this ;
516544 }
517545
@@ -536,7 +564,7 @@ public JBBPDslBuilder CloseStruct(final boolean closeAllOpened) {
536564 if (this .openedStructCounter == 0 ) {
537565 throw new IllegalStateException ("There is not any opened struct" );
538566 }
539- this .items . add (new ItemStructEnd (closeAllOpened ));
567+ this .addItem (new ItemStructEnd (closeAllOpened ));
540568 this .openedStructCounter = closeAllOpened ? 0 : this .openedStructCounter - 1 ;
541569 return this ;
542570 }
@@ -599,7 +627,7 @@ public JBBPDslBuilder Bits(final JBBPBitNumber bits) {
599627 public JBBPDslBuilder Bits (final String name , final JBBPBitNumber bits ) {
600628 final Item item = new Item (BinType .BIT , name , this .byteOrder );
601629 item .bitNumber = bits ;
602- this .items . add (item );
630+ this .addItem (item );
603631 return this ;
604632 }
605633
@@ -613,7 +641,7 @@ public JBBPDslBuilder Bits(final String name, final JBBPBitNumber bits) {
613641 public JBBPDslBuilder Bits (final String name , final String bitLenExpression ) {
614642 final Item item = new Item (BinType .BIT , name , this .byteOrder );
615643 item .bitLenExpression = assertExpressionChars (bitLenExpression );
616- this .items . add (item );
644+ this .addItem (item );
617645 return this ;
618646 }
619647
@@ -686,7 +714,7 @@ public JBBPDslBuilder BitArray(final String name, final JBBPBitNumber bits, fina
686714 final Item item = new Item (BinType .BIT_ARRAY , name , this .byteOrder );
687715 item .bitNumber = bits ;
688716 item .sizeExpression = assertExpressionChars (sizeExpression );
689- this .items . add (item );
717+ this .addItem (item );
690718 return this ;
691719 }
692720
@@ -702,7 +730,7 @@ public JBBPDslBuilder BitArray(final String name, final String bitLenExpression,
702730 final Item item = new Item (BinType .BIT_ARRAY , name , this .byteOrder );
703731 item .bitLenExpression = assertExpressionChars (bitLenExpression );
704732 item .sizeExpression = assertExpressionChars (sizeExpression );
705- this .items . add (item );
733+ this .addItem (item );
706734 return this ;
707735 }
708736
@@ -747,7 +775,7 @@ public JBBPDslBuilder BoolArray(final int size) {
747775 public JBBPDslBuilder BoolArray (final String name , final String sizeExpression ) {
748776 final Item item = new Item (BinType .BOOL_ARRAY , name , this .byteOrder );
749777 item .sizeExpression = assertExpressionChars (sizeExpression );
750- this .items . add (item );
778+ this .addItem (item );
751779 return this ;
752780 }
753781
@@ -768,7 +796,7 @@ public JBBPDslBuilder Bool() {
768796 */
769797 public JBBPDslBuilder Bool (final String name ) {
770798 final Item item = new Item (BinType .BOOL , name , this .byteOrder );
771- this .items . add (item );
799+ this .addItem (item );
772800 return this ;
773801 }
774802
@@ -789,7 +817,7 @@ public JBBPDslBuilder Byte() {
789817 */
790818 public JBBPDslBuilder Byte (final String name ) {
791819 final Item item = new Item (BinType .BYTE , name , this .byteOrder );
792- this .items . add (item );
820+ this .addItem (item );
793821 return this ;
794822 }
795823
@@ -834,7 +862,7 @@ public JBBPDslBuilder ByteArray(final String name, final int size) {
834862 public JBBPDslBuilder ByteArray (final String name , final String sizeExpression ) {
835863 final Item item = new Item (BinType .BYTE_ARRAY , name , this .byteOrder );
836864 item .sizeExpression = assertExpressionChars (sizeExpression );
837- this .items . add (item );
865+ this .addItem (item );
838866 return this ;
839867 }
840868
@@ -855,7 +883,7 @@ public JBBPDslBuilder UByte() {
855883 */
856884 public JBBPDslBuilder UByte (final String name ) {
857885 final Item item = new Item (BinType .UBYTE , name , this .byteOrder );
858- this .items . add (item );
886+ this .addItem (item );
859887 return this ;
860888 }
861889
@@ -900,7 +928,7 @@ public JBBPDslBuilder UByteArray(final String name, final int size) {
900928 public JBBPDslBuilder UByteArray (final String name , final String sizeExpression ) {
901929 final Item item = new Item (BinType .UBYTE_ARRAY , name , this .byteOrder );
902930 item .sizeExpression = assertExpressionChars (sizeExpression );
903- this .items . add (item );
931+ this .addItem (item );
904932 return this ;
905933 }
906934
@@ -921,7 +949,7 @@ public JBBPDslBuilder Short() {
921949 */
922950 public JBBPDslBuilder Short (final String name ) {
923951 final Item item = new Item (BinType .SHORT , name , this .byteOrder );
924- this .items . add (item );
952+ this .addItem (item );
925953 return this ;
926954 }
927955
@@ -966,7 +994,7 @@ public JBBPDslBuilder ShortArray(final String name, final int size) {
966994 public JBBPDslBuilder ShortArray (final String name , final String sizeExpression ) {
967995 final Item item = new Item (BinType .SHORT_ARRAY , name , this .byteOrder );
968996 item .sizeExpression = assertExpressionChars (sizeExpression );
969- this .items . add (item );
997+ this .addItem (item );
970998 return this ;
971999 }
9721000
@@ -987,7 +1015,7 @@ public JBBPDslBuilder UShort() {
9871015 */
9881016 public JBBPDslBuilder UShort (final String name ) {
9891017 final Item item = new Item (BinType .USHORT , name , this .byteOrder );
990- this .items . add (item );
1018+ this .addItem (item );
9911019 return this ;
9921020 }
9931021
@@ -1021,7 +1049,7 @@ public JBBPDslBuilder UShortArray(final int size) {
10211049 public JBBPDslBuilder UShortArray (final String name , final String sizeExpression ) {
10221050 final Item item = new Item (BinType .USHORT_ARRAY , name , this .byteOrder );
10231051 item .sizeExpression = assertExpressionChars (sizeExpression );
1024- this .items . add (item );
1052+ this .addItem (item );
10251053 return this ;
10261054 }
10271055
@@ -1053,7 +1081,7 @@ public JBBPDslBuilder Int() {
10531081 */
10541082 public JBBPDslBuilder Int (final String name ) {
10551083 final Item item = new Item (BinType .INT , name , this .byteOrder );
1056- this .items . add (item );
1084+ this .addItem (item );
10571085 return this ;
10581086 }
10591087
@@ -1098,7 +1126,7 @@ public JBBPDslBuilder IntArray(final String name, final int size) {
10981126 public JBBPDslBuilder IntArray (final String name , final String sizeExpression ) {
10991127 final Item item = new Item (BinType .INT_ARRAY , name , this .byteOrder );
11001128 item .sizeExpression = assertExpressionChars (sizeExpression );
1101- this .items . add (item );
1129+ this .addItem (item );
11021130 return this ;
11031131 }
11041132
@@ -1119,7 +1147,7 @@ public JBBPDslBuilder Long() {
11191147 */
11201148 public JBBPDslBuilder Long (final String name ) {
11211149 final Item item = new Item (BinType .LONG , name , this .byteOrder );
1122- this .items . add (item );
1150+ this .addItem (item );
11231151 return this ;
11241152 }
11251153
@@ -1164,7 +1192,7 @@ public JBBPDslBuilder LongArray(final String name, final int size) {
11641192 public JBBPDslBuilder LongArray (final String name , final String sizeExpression ) {
11651193 final Item item = new Item (BinType .LONG_ARRAY , name , this .byteOrder );
11661194 item .sizeExpression = assertExpressionChars (sizeExpression );
1167- this .items . add (item );
1195+ this .addItem (item );
11681196 return this ;
11691197 }
11701198
@@ -1185,7 +1213,7 @@ public JBBPDslBuilder Float() {
11851213 */
11861214 public JBBPDslBuilder Float (final String name ) {
11871215 final Item item = new Item (BinType .FLOAT , name , this .byteOrder );
1188- this .items . add (item );
1216+ this .addItem (item );
11891217 return this ;
11901218 }
11911219
@@ -1230,7 +1258,7 @@ public JBBPDslBuilder FloatArray(final String name, final int size) {
12301258 public JBBPDslBuilder FloatArray (final String name , final String sizeExpression ) {
12311259 final Item item = new Item (BinType .FLOAT_ARRAY , name , this .byteOrder );
12321260 item .sizeExpression = assertExpressionChars (sizeExpression );
1233- this .items . add (item );
1261+ this .addItem (item );
12341262 return this ;
12351263 }
12361264
@@ -1251,7 +1279,7 @@ public JBBPDslBuilder Double() {
12511279 */
12521280 public JBBPDslBuilder Double (final String name ) {
12531281 final Item item = new Item (BinType .DOUBLE , name , this .byteOrder );
1254- this .items . add (item );
1282+ this .addItem (item );
12551283 return this ;
12561284 }
12571285
@@ -1262,7 +1290,7 @@ public JBBPDslBuilder Double(final String name) {
12621290 * @return the builder instance, must not be null
12631291 */
12641292 public JBBPDslBuilder Comment (final String text ) {
1265- this .items . add (new ItemComment (text == null ? "" : text ));
1293+ this .addItem (new ItemComment (text == null ? "" : text ));
12661294 return this ;
12671295 }
12681296
@@ -1307,7 +1335,7 @@ public JBBPDslBuilder DoubleArray(final String name, final int size) {
13071335 public JBBPDslBuilder DoubleArray (final String name , final String sizeExpression ) {
13081336 final Item item = new Item (BinType .DOUBLE_ARRAY , name , this .byteOrder );
13091337 item .sizeExpression = assertExpressionChars (sizeExpression );
1310- this .items . add (item );
1338+ this .addItem (item );
13111339 return this ;
13121340 }
13131341
@@ -1327,7 +1355,7 @@ public JBBPDslBuilder String() {
13271355 */
13281356 public JBBPDslBuilder String (final String name ) {
13291357 final Item item = new Item (BinType .STRING , name , this .byteOrder );
1330- this .items . add (item );
1358+ this .addItem (item );
13311359 return this ;
13321360 }
13331361
@@ -1372,7 +1400,7 @@ public JBBPDslBuilder StringArray(final String name, final int size) {
13721400 public JBBPDslBuilder StringArray (final String name , final String sizeExpression ) {
13731401 final Item item = new Item (BinType .STRING_ARRAY , name , this .byteOrder );
13741402 item .sizeExpression = assertExpressionChars (sizeExpression );
1375- this .items . add (item );
1403+ this .addItem (item );
13761404 return this ;
13771405 }
13781406
0 commit comments