Skip to content

Commit 6186109

Browse files
authored
Merge pull request #10 from Abductcows/dev
Proper-er unit tests, bit of javadoc and annotations
2 parents a2e1be3 + dc0a698 commit 6186109

File tree

6 files changed

+324
-246
lines changed

6 files changed

+324
-246
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*
22
!src/**
3+
!test/**
34
!pom.xml
45
!.gitignore

pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,30 @@
2424
<dependency>
2525
<groupId>org.junit.jupiter</groupId>
2626
<artifactId>junit-jupiter-engine</artifactId>
27-
<version>5.1.0</version>
27+
<version>5.8.2</version>
2828
<scope>test</scope>
2929
<optional>true</optional>
3030
</dependency>
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter-params</artifactId>
34-
<version>5.1.0</version>
34+
<version>5.8.2</version>
3535
<scope>test</scope>
3636
<optional>true</optional>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.jetbrains</groupId>
40+
<artifactId>annotations</artifactId>
41+
<version>23.0.0</version>
42+
<scope>compile</scope>
43+
</dependency>
44+
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
45+
<dependency>
46+
<groupId>org.assertj</groupId>
47+
<artifactId>assertj-core</artifactId>
48+
<version>3.21.0</version>
49+
<scope>test</scope>
50+
</dependency>
3851
</dependencies>
3952

4053
<build>

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

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package gr.geompokon.bitarray;
1818

19+
import org.jetbrains.annotations.NotNull;
20+
1921
import java.util.*;
2022

2123
/**
@@ -63,7 +65,7 @@ public final class BitArray extends AbstractList<Boolean> implements RandomAcces
6365
private static final int BITS_PER_LONG = 64;
6466

6567
/**
66-
* Default array capacity in bit entries. Used in empty constructor
68+
* Default array capacity in bit entries
6769
*/
6870
private static final int DEFAULT_CAPACITY = BITS_PER_LONG;
6971

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

8082
/**
81-
* Default constructor. Sets initial capacity to 64
83+
* Default constructor. Sets initial capacity to {@link #DEFAULT_CAPACITY}
8284
*/
8385
public BitArray() {
8486
this(DEFAULT_CAPACITY);
@@ -88,7 +90,7 @@ public BitArray() {
8890
* Initialises the array to store at least {@code initialCapacity} elements before resizing.
8991
*
9092
* <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}.
9294
* </p>
9395
*
9496
* @param initialCapacity initial capacity of the array in bit entries
@@ -103,14 +105,10 @@ public BitArray(int initialCapacity) {
103105
/**
104106
* Builds the array from the specified collection in the order specified by its iterator
105107
*
106-
* <p>
107-
* Copy of collection without {@code addAll()} for BitArray types
108-
* </p>
109-
*
110108
* @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
112110
*/
113-
public BitArray(Collection<? extends Boolean> other) {
111+
public BitArray(@NotNull Collection<? extends Boolean> other) {
114112
Objects.requireNonNull(other);
115113

116114
// fast copy for BitArray
@@ -146,10 +144,10 @@ private void initMembers(int initialCapacity) {
146144
*
147145
* @param index array index to insert the element in
148146
* @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
150148
*/
151-
public void add(int index, Boolean bit) {
152-
Objects.requireNonNull(bit);
149+
@Override
150+
public void add(int index, @NotNull Boolean bit) {
153151
ensureIndexInRange(index, elements);
154152
modCount++;
155153
ensureCapacity();
@@ -170,14 +168,27 @@ public void add(int index, Boolean bit) {
170168
elements = elements + 1;
171169
}
172170

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+
173183
/**
174184
* Returns the boolean value of the bit at the selected array index.
175185
*
176186
* @param index index of the element in the array
177187
* @return boolean value of the bit entry
178188
* @throws IndexOutOfBoundsException if index is out of array bounds
179189
*/
180-
public Boolean get(int index) {
190+
@Override
191+
public @NotNull Boolean get(int index) {
181192
ensureIndexInRange(index, elements - 1);
182193
// get bit indices
183194
int longIndex = getLongIndex(index);
@@ -193,9 +204,10 @@ public Boolean get(int index) {
193204
* @param index index of the array element to be changed
194205
* @param bit the new value of the array element
195206
* @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
197208
*/
198-
public Boolean set(int index, Boolean bit) {
209+
@Override
210+
public @NotNull Boolean set(int index, @NotNull Boolean bit) {
199211
Objects.requireNonNull(bit);
200212
ensureIndexInRange(index, elements - 1);
201213
// get bit indices
@@ -217,7 +229,8 @@ public Boolean set(int index, Boolean bit) {
217229
* @return boolean value of the removed bit
218230
* @throws IndexOutOfBoundsException if index is out of array bounds
219231
*/
220-
public Boolean remove(int index) {
232+
@Override
233+
public @NotNull Boolean remove(int index) {
221234
ensureIndexInRange(index, elements - 1);
222235
modCount++;
223236

@@ -240,13 +253,15 @@ public Boolean remove(int index) {
240253
*
241254
* @return number of elements in the array
242255
*/
256+
@Override
243257
public int size() {
244258
return elements;
245259
}
246260

247261
/**
248262
* Clears the contents of the array and releases memory used previously.
249263
*/
264+
@Override
250265
public void clear() {
251266
modCount++;
252267
initMembers(DEFAULT_CAPACITY);
@@ -526,6 +541,7 @@ private int longsRequiredForNBits(int nBits) {
526541
*
527542
* @return deep copy of {@code this}
528543
*/
544+
@Override
529545
public BitArray clone() {
530546
return new BitArray(this);
531547
}
@@ -548,6 +564,7 @@ public BitArray clone() {
548564
*
549565
* @return String representation of the array and its elements
550566
*/
567+
@Override
551568
public String toString() {
552569
StringBuilder s = new StringBuilder(this.size() * 2);
553570

@@ -590,23 +607,28 @@ public static BitArray fromString(String stringArray) {
590607
try {
591608
String arraySizeStr = stringArray.substring(start.length(), currentIndex);
592609
arraySize = Integer.parseInt(arraySizeStr);
593-
} catch (IndexOutOfBoundsException | NumberFormatException e) {
594-
throw new UnknownFormatConversionException("Not a valid BitArray string");
595-
}
596610

597-
// move the cursor to the first element
598-
currentIndex += ", [".length();
599611

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;
605627
}
606-
result.add(stringArray.charAt(currentIndex) == '1');
607-
currentIndex += 2;
608-
}
609628

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

src/test/java/gr/geompokon/bitarray/BitArrayInterfaceTest.java

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)