16
16
17
17
package gr .geompokon .bitarray ;
18
18
19
+ import org .jetbrains .annotations .NotNull ;
20
+
19
21
import java .util .*;
20
22
21
23
/**
@@ -63,7 +65,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
63
65
private static final int BITS_PER_LONG = 64 ;
64
66
65
67
/**
66
- * Default array capacity in bit entries. Used in empty constructor
68
+ * Default array capacity in bit entries
67
69
*/
68
70
private static final int DEFAULT_CAPACITY = BITS_PER_LONG ;
69
71
@@ -78,7 +80,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
78
80
private int elements ;
79
81
80
82
/**
81
- * Default constructor. Sets initial capacity to 64
83
+ * Default constructor. Sets initial capacity to {@link #DEFAULT_CAPACITY}
82
84
*/
83
85
public BitArray () {
84
86
this (DEFAULT_CAPACITY );
@@ -88,7 +90,7 @@ public BitArray() {
88
90
* Initialises the array to store at least {@code initialCapacity} elements before resizing.
89
91
*
90
92
* <p>
91
- * 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} .
92
94
* </p>
93
95
*
94
96
* @param initialCapacity initial capacity of the array in bit entries
@@ -103,14 +105,10 @@ public BitArray(int initialCapacity) {
103
105
/**
104
106
* Builds the array from the specified collection in the order specified by its iterator
105
107
*
106
- * <p>
107
- * Copy of collection without {@code addAll()} for BitArray types
108
- * </p>
109
- *
110
108
* @param other the collection supplying the elements
111
- * @throws java.lang. NullPointerException if the collection is null
109
+ * @throws NullPointerException if the collection is null
112
110
*/
113
- public BitArray (Collection <? extends Boolean > other ) {
111
+ public BitArray (@ NotNull Collection <? extends Boolean > other ) {
114
112
Objects .requireNonNull (other );
115
113
116
114
// fast copy for BitArray
@@ -146,10 +144,10 @@ private void initMembers(int initialCapacity) {
146
144
*
147
145
* @param index array index to insert the element in
148
146
* @param bit the boolean value to be inserted
149
- * @throws java.lang. IndexOutOfBoundsException if index is out of array insertion bounds
147
+ * @throws IndexOutOfBoundsException if index is out of array insertion bounds
150
148
*/
151
- public void add ( int index , Boolean bit ) {
152
- Objects . requireNonNull ( bit );
149
+ @ Override
150
+ public void add ( int index , @ NotNull Boolean bit ) {
153
151
ensureIndexInRange (index , elements );
154
152
modCount ++;
155
153
ensureCapacity ();
@@ -170,14 +168,27 @@ public void add(int index, Boolean bit) {
170
168
elements = elements + 1 ;
171
169
}
172
170
171
+ /**
172
+ * Inserts the boolean value as a bit at the tail of the array
173
+ *
174
+ * @param bit the boolean value to be inserted
175
+ * @return success / failure of the add operation
176
+ */
177
+ @ Override
178
+ public boolean add (@ NotNull Boolean bit ) {
179
+ add (elements , bit );
180
+ return true ;
181
+ }
182
+
173
183
/**
174
184
* Returns the boolean value of the bit at the selected array index.
175
185
*
176
186
* @param index index of the element in the array
177
187
* @return boolean value of the bit entry
178
188
* @throws IndexOutOfBoundsException if index is out of array bounds
179
189
*/
180
- public Boolean get (int index ) {
190
+ @ Override
191
+ public @ NotNull Boolean get (int index ) {
181
192
ensureIndexInRange (index , elements - 1 );
182
193
// get bit indices
183
194
int longIndex = getLongIndex (index );
@@ -193,9 +204,10 @@ public Boolean get(int index) {
193
204
* @param index index of the array element to be changed
194
205
* @param bit the new value of the array element
195
206
* @return boolean value of the previous bit at that index
196
- * @throws java.lang. IndexOutOfBoundsException if index is out of array bounds
207
+ * @throws IndexOutOfBoundsException if index is out of array bounds
197
208
*/
198
- public Boolean set (int index , Boolean bit ) {
209
+ @ Override
210
+ public @ NotNull Boolean set (int index , @ NotNull Boolean bit ) {
199
211
Objects .requireNonNull (bit );
200
212
ensureIndexInRange (index , elements - 1 );
201
213
// get bit indices
@@ -217,7 +229,8 @@ public Boolean set(int index, Boolean bit) {
217
229
* @return boolean value of the removed bit
218
230
* @throws IndexOutOfBoundsException if index is out of array bounds
219
231
*/
220
- public Boolean remove (int index ) {
232
+ @ Override
233
+ public @ NotNull Boolean remove (int index ) {
221
234
ensureIndexInRange (index , elements - 1 );
222
235
modCount ++;
223
236
@@ -240,13 +253,15 @@ public Boolean remove(int index) {
240
253
*
241
254
* @return number of elements in the array
242
255
*/
256
+ @ Override
243
257
public int size () {
244
258
return elements ;
245
259
}
246
260
247
261
/**
248
262
* Clears the contents of the array and releases memory used previously.
249
263
*/
264
+ @ Override
250
265
public void clear () {
251
266
modCount ++;
252
267
initMembers (DEFAULT_CAPACITY );
@@ -526,6 +541,7 @@ private int longsRequiredForNBits(int nBits) {
526
541
*
527
542
* @return deep copy of {@code this}
528
543
*/
544
+ @ Override
529
545
public BitArray clone () {
530
546
return new BitArray (this );
531
547
}
@@ -548,6 +564,7 @@ public BitArray clone() {
548
564
*
549
565
* @return String representation of the array and its elements
550
566
*/
567
+ @ Override
551
568
public String toString () {
552
569
StringBuilder s = new StringBuilder (this .size () * 2 );
553
570
@@ -590,23 +607,28 @@ public static BitArray fromString(String stringArray) {
590
607
try {
591
608
String arraySizeStr = stringArray .substring (start .length (), currentIndex );
592
609
arraySize = Integer .parseInt (arraySizeStr );
593
- } catch (IndexOutOfBoundsException | NumberFormatException e ) {
594
- throw new UnknownFormatConversionException ("Not a valid BitArray string" );
595
- }
596
610
597
- // move the cursor to the first element
598
- currentIndex += ", [" .length ();
599
611
600
- // read elements
601
- BitArray result = new BitArray (arraySize );
602
- for (int i = 0 ; i < arraySize ; i ++) {
603
- if (currentIndex >= stringArray .length () - 1 ) {
604
- 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 ;
605
627
}
606
- result .add (stringArray .charAt (currentIndex ) == '1' );
607
- currentIndex += 2 ;
608
- }
609
628
610
- return result ;
629
+ return result ;
630
+ } catch (IndexOutOfBoundsException | NumberFormatException e ) {
631
+ throw new UnknownFormatConversionException ("Not a valid BitArray string" );
632
+ }
611
633
}
612
634
}
0 commit comments