Skip to content

Commit 1a0cd24

Browse files
committed
Resolve SpotBugs
1 parent 761f7d1 commit 1a0cd24

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

src/main/java/com/thealgorithms/compression/BurrowsWheelerTransform.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,10 @@ public static BWTResult transform(String text) {
136136

137137
// Sort rotations lexicographically
138138
Arrays.sort(rotations);
139-
140-
// Extract the last column and find the original string's position
139+
int originalIndex = Arrays.binarySearch(rotations, text);
141140
StringBuilder lastColumn = new StringBuilder(n);
142-
int originalIndex = -1;
143141
for (int i = 0; i < n; i++) {
144142
lastColumn.append(rotations[i].charAt(n - 1));
145-
if (rotations[i].equals(text)) {
146-
originalIndex = i;
147-
}
148143
}
149144

150145
return new BWTResult(lastColumn.toString(), originalIndex);

src/main/java/com/thealgorithms/compression/MoveToFront.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.compression;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.stream.Collectors;
@@ -128,15 +129,14 @@ public static List<Integer> transform(String text, String initialAlphabet) {
128129
* to the one used in the forward transform, including character order, or the
129130
* output will be incorrect.</p>
130131
*
131-
* @param indices the list of integers from the forward transform; if empty or {@code null},
132-
* returns an empty string
132+
* @param indices The list of integers from the forward transform.
133133
* @param initialAlphabet the exact same initial alphabet string used for the forward transform;
134134
* if {@code null} or empty, returns an empty string
135135
* @return the original, untransformed string
136136
* @throws IllegalArgumentException if any index in {@code indices} is negative or
137137
* exceeds the current alphabet size
138138
*/
139-
public static String inverseTransform(List<Integer> indices, String initialAlphabet) {
139+
public static String inverseTransform(Collection<Integer> indices, String initialAlphabet) {
140140
if (indices == null || indices.isEmpty() || initialAlphabet == null || initialAlphabet.isEmpty()) {
141141
return "";
142142
}

src/test/java/com/thealgorithms/compression/BurrowsWheelerTransformTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.thealgorithms.compression;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
47

58
import org.junit.jupiter.api.Test;
69

@@ -71,4 +74,48 @@ public void testSingleCharacter() {
7174
String reconstructed = BurrowsWheelerTransform.inverseTransform(actualTransform.transformed, actualTransform.originalIndex);
7275
assertEquals(original, reconstructed);
7376
}
77+
78+
@Test
79+
public void testTransformNull() {
80+
assertEquals(new BurrowsWheelerTransform.BWTResult("", -1), BurrowsWheelerTransform.transform(null));
81+
}
82+
83+
@Test
84+
public void testInverseTransformNullString() {
85+
// bwtString == null
86+
assertEquals("", BurrowsWheelerTransform.inverseTransform(null, 1));
87+
}
88+
89+
@Test
90+
public void testInverseTransformIndexOutOfBounds() {
91+
String bwt = "annb$aa";
92+
int n = bwt.length(); // n = 7
93+
94+
// originalIndex >= n
95+
assertThrows(IllegalArgumentException.class, () -> BurrowsWheelerTransform.inverseTransform(bwt, n));
96+
assertThrows(IllegalArgumentException.class, () -> BurrowsWheelerTransform.inverseTransform(bwt, 8));
97+
98+
// originalIndex < 0
99+
assertThrows(IllegalArgumentException.class, () -> BurrowsWheelerTransform.inverseTransform(bwt, -2));
100+
}
101+
102+
@Test
103+
public void testBWTResultHelpers() {
104+
BurrowsWheelerTransform.BWTResult res1 = new BurrowsWheelerTransform.BWTResult("annb$aa", 4);
105+
BurrowsWheelerTransform.BWTResult res2 = new BurrowsWheelerTransform.BWTResult("annb$aa", 4);
106+
BurrowsWheelerTransform.BWTResult res3 = new BurrowsWheelerTransform.BWTResult("other", 4);
107+
BurrowsWheelerTransform.BWTResult res4 = new BurrowsWheelerTransform.BWTResult("annb$aa", 1);
108+
109+
assertEquals(res1, res2);
110+
assertNotEquals(res1, null); // obj == null
111+
assertNotEquals(res1, new Object()); // different class
112+
assertNotEquals(res1, res3); // different transformed
113+
assertNotEquals(res1, res4); // different originalIndex
114+
115+
assertEquals(res1.hashCode(), res2.hashCode());
116+
assertNotEquals(res1.hashCode(), res3.hashCode());
117+
118+
assertTrue(res1.toString().contains("annb$aa"));
119+
assertTrue(res1.toString().contains("originalIndex=4"));
120+
}
74121
}

src/test/java/com/thealgorithms/compression/MoveToFrontTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public void testSymbolNotInAlphabet() {
6767
@Test
6868
public void testIndexOutOfBounds() {
6969
// Index 5 is out of bounds for alphabet "abc"
70+
// 1. test index >= alphabet.size()
7071
assertThrows(IllegalArgumentException.class, () -> MoveToFront.inverseTransform(List.of(1, 2, 5), "abc"));
72+
73+
// 2. test index < 0
74+
assertThrows(IllegalArgumentException.class, () -> MoveToFront.inverseTransform(List.of(1, -1, 2), "abc"));
75+
}
76+
77+
@Test
78+
public void testTransformNull() {
79+
List<Integer> expected = List.of();
80+
assertEquals(expected, MoveToFront.transform(null, "abc"));
81+
assertThrows(IllegalArgumentException.class, () -> MoveToFront.transform("abc", null));
82+
}
83+
84+
@Test
85+
public void testInverseTransformNulls() {
86+
// 1. test indices == null
87+
assertEquals("", MoveToFront.inverseTransform(null, "abc"));
88+
89+
// 2. test initialAlphabet == null
90+
assertEquals("", MoveToFront.inverseTransform(List.of(1, 2), null));
7191
}
7292
}

0 commit comments

Comments
 (0)