@@ -65,7 +65,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
65
65
private static final int BITS_PER_LONG = 64 ;
66
66
67
67
/**
68
- * Default array capacity in bit entries. Used in empty constructor
68
+ * Default array capacity in bit entries
69
69
*/
70
70
private static final int DEFAULT_CAPACITY = BITS_PER_LONG ;
71
71
@@ -80,7 +80,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
80
80
private int elements ;
81
81
82
82
/**
83
- * Default constructor. Sets initial capacity to 64
83
+ * Default constructor. Sets initial capacity to {@link #DEFAULT_CAPACITY}
84
84
*/
85
85
public BitArray () {
86
86
this (DEFAULT_CAPACITY );
@@ -90,7 +90,7 @@ public BitArray() {
90
90
* Initialises the array to store at least {@code initialCapacity} elements before resizing.
91
91
*
92
92
* <p>
93
- * Actual memory size of the array in bits is rounded up to the next multiple of 64 .
93
+ * Actual memory size of the array in bits is rounded up to the next multiple of {@link #BITS_PER_LONG} .
94
94
* </p>
95
95
*
96
96
* @param initialCapacity initial capacity of the array in bit entries
@@ -105,14 +105,10 @@ public BitArray(int initialCapacity) {
105
105
/**
106
106
* Builds the array from the specified collection in the order specified by its iterator
107
107
*
108
- * <p>
109
- * Copy of collection without {@code addAll()} for BitArray types
110
- * </p>
111
- *
112
108
* @param other the collection supplying the elements
113
109
* @throws NullPointerException if the collection is null
114
110
*/
115
- public BitArray (Collection <? extends Boolean > other ) {
111
+ public BitArray (@ NotNull Collection <? extends Boolean > other ) {
116
112
Objects .requireNonNull (other );
117
113
118
114
// fast copy for BitArray
@@ -611,23 +607,28 @@ public static BitArray fromString(String stringArray) {
611
607
try {
612
608
String arraySizeStr = stringArray .substring (start .length (), currentIndex );
613
609
arraySize = Integer .parseInt (arraySizeStr );
614
- } catch (IndexOutOfBoundsException | NumberFormatException e ) {
615
- throw new UnknownFormatConversionException ("Not a valid BitArray string" );
616
- }
617
610
618
- // move the cursor to the first element
619
- currentIndex += ", [" .length ();
620
611
621
- // read elements
622
- BitArray result = new BitArray (arraySize );
623
- for (int i = 0 ; i < arraySize ; i ++) {
624
- if (currentIndex >= stringArray .length () - 1 ) {
625
- throw new UnknownFormatConversionException ("Not a valid BitArray string" );
612
+ // move the cursor to the first element
613
+ currentIndex += ", [" .length ();
614
+
615
+ // read elements
616
+ List <Character > allowedElements = List .of ('0' , '1' );
617
+
618
+ BitArray result = new BitArray (arraySize );
619
+ for (int i = 0 ; i < arraySize ; i ++) {
620
+ char current = stringArray .charAt (currentIndex );
621
+ if (currentIndex >= stringArray .length () - 1 || !allowedElements .contains (current )) {
622
+ throw new UnknownFormatConversionException ("Not a valid BitArray string" );
623
+ }
624
+
625
+ result .add (current == '1' );
626
+ currentIndex += 2 ;
626
627
}
627
- result .add (stringArray .charAt (currentIndex ) == '1' );
628
- currentIndex += 2 ;
629
- }
630
628
631
- return result ;
629
+ return result ;
630
+ } catch (IndexOutOfBoundsException | NumberFormatException e ) {
631
+ throw new UnknownFormatConversionException ("Not a valid BitArray string" );
632
+ }
632
633
}
633
634
}
0 commit comments