Skip to content

Commit 3e5bf5e

Browse files
committed
javadoc and fix deserialization error where
- StringIndexOutOfBoundsException could be thrown instead - any content value other than '1' would be interpreted as FALSE
1 parent ee5cb76 commit 3e5bf5e

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/main/java/gr/geompokon/bitarray/BitArray.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
6565
private static final int BITS_PER_LONG = 64;
6666

6767
/**
68-
* Default array capacity in bit entries. Used in empty constructor
68+
* Default array capacity in bit entries
6969
*/
7070
private static final int DEFAULT_CAPACITY = BITS_PER_LONG;
7171

@@ -80,7 +80,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
8080
private int elements;
8181

8282
/**
83-
* Default constructor. Sets initial capacity to 64
83+
* Default constructor. Sets initial capacity to {@link #DEFAULT_CAPACITY}
8484
*/
8585
public BitArray() {
8686
this(DEFAULT_CAPACITY);
@@ -90,7 +90,7 @@ public BitArray() {
9090
* Initialises the array to store at least {@code initialCapacity} elements before resizing.
9191
*
9292
* <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}.
9494
* </p>
9595
*
9696
* @param initialCapacity initial capacity of the array in bit entries
@@ -105,14 +105,10 @@ public BitArray(int initialCapacity) {
105105
/**
106106
* Builds the array from the specified collection in the order specified by its iterator
107107
*
108-
* <p>
109-
* Copy of collection without {@code addAll()} for BitArray types
110-
* </p>
111-
*
112108
* @param other the collection supplying the elements
113109
* @throws NullPointerException if the collection is null
114110
*/
115-
public BitArray(Collection<? extends Boolean> other) {
111+
public BitArray(@NotNull Collection<? extends Boolean> other) {
116112
Objects.requireNonNull(other);
117113

118114
// fast copy for BitArray
@@ -611,23 +607,28 @@ public static BitArray fromString(String stringArray) {
611607
try {
612608
String arraySizeStr = stringArray.substring(start.length(), currentIndex);
613609
arraySize = Integer.parseInt(arraySizeStr);
614-
} catch (IndexOutOfBoundsException | NumberFormatException e) {
615-
throw new UnknownFormatConversionException("Not a valid BitArray string");
616-
}
617610

618-
// move the cursor to the first element
619-
currentIndex += ", [".length();
620611

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;
626627
}
627-
result.add(stringArray.charAt(currentIndex) == '1');
628-
currentIndex += 2;
629-
}
630628

631-
return result;
629+
return result;
630+
} catch (IndexOutOfBoundsException | NumberFormatException e) {
631+
throw new UnknownFormatConversionException("Not a valid BitArray string");
632+
}
632633
}
633634
}

0 commit comments

Comments
 (0)