Skip to content

Commit 84ef2cd

Browse files
codewarrior2000KevinGilmore
authored andcommitted
BAEL-2142 Code revision to WordIndexer and JUnit (eugenp#5484)
* Refactoring and Unit testing added based on Kevin Gilmore's advice of Sep 16, 2018 * Refactoring and Unit testing added based on Kevin Gilmore's advice of Sep 16, 2018 * Refactoring and Unit testing added based on Kevin Gilmore's advice of Sep 16, 2018 * Resolve merge conflict Added dependency for org.passay and for org.apache.commons * Create searching/WordIndexer.java * Create WordIndexerUnitTest.java * Updated in response to Kevin Gilmore's questions "What happens if the string ends with the word you're searching for? Would the next iteration of the loop cause an error if index + wordLength is greater than the last position of the string? Be sure to test this scenario." * Updated WordIndexer in response to Kevin Gilmore Generally speaking, it's good practice to declare return types, parameters, variables using an interface rather than an implementation, if the interface is sufficient to satisfy the need. In this case, there's no reason you can't simply return a List of Integer. (Excellent point, fixed) The two methods are identical. Isn't the first one supposed to be the naive approach? (It was a copy and paste error, fixed) * saving changes... * Update java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java Co-Authored-By: codewarrior2000 <lpc34@columbia.edu> * Update java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java Co-Authored-By: codewarrior2000 <lpc34@columbia.edu> * Update java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java Co-Authored-By: codewarrior2000 <lpc34@columbia.edu> * Respond to Kevin Gilmore's code changes * Debugging code change to JUnit
1 parent 57f89ee commit 84ef2cd

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

java-strings/pom.xml

100644100755
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@
6868
<artifactId>emoji-java</artifactId>
6969
<version>4.0.0</version>
7070
</dependency>
71+
72+
<dependency>
73+
<groupId>org.junit.jupiter</groupId>
74+
<artifactId>junit-jupiter-api</artifactId>
75+
<version>5.3.1</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.hamcrest</groupId>
81+
<artifactId>hamcrest-library</artifactId>
82+
<version>1.3</version>
83+
<scope>test</scope>
84+
</dependency>
85+
7186
<!-- Added for password generation -->
7287
<dependency>
7388
<groupId>org.passay</groupId>
@@ -79,6 +94,7 @@
7994
<artifactId>commons-text</artifactId>
8095
<version>1.4</version>
8196
</dependency>
97+
8298
</dependencies>
8399

84100
<build>
@@ -115,4 +131,4 @@
115131
<guava.version>26.0-jre</guava.version>
116132
</properties>
117133

118-
</project>
134+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.string.searching;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class WordIndexer {
7+
8+
public List<Integer> findWord(String textString, String word) {
9+
int index = 0;
10+
List<Integer> indexes = new ArrayList<Integer>();
11+
String lowerCaseTextString = textString.toLowerCase();
12+
String lowerCaseWord = word.toLowerCase();
13+
14+
while(index != -1){
15+
index = lowerCaseTextString.indexOf(lowerCaseWord, index + 1);
16+
if (index != -1) {
17+
indexes.add(index);
18+
}
19+
}
20+
return indexes;
21+
}
22+
23+
24+
25+
public List<Integer> findWordUpgrade(String textString, String word) {
26+
int index = 0;
27+
List<Integer> indexes = new ArrayList<Integer>();
28+
StringBuilder output = new StringBuilder();
29+
String lowerCaseTextString = textString.toLowerCase();
30+
String lowerCaseWord = word.toLowerCase();
31+
int wordLength = 0;
32+
33+
while(index != -1){
34+
index = lowerCaseTextString.indexOf(lowerCaseWord, index + wordLength); // Slight improvement
35+
if (index != -1) {
36+
indexes.add(index);
37+
}
38+
wordLength = word.length();
39+
}
40+
return indexes;
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.string.searching;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
12+
public class WordIndexerUnitTest {
13+
14+
String theString;
15+
WordIndexer wordIndexer;
16+
17+
@BeforeEach
18+
public void setUp() throws Exception {
19+
wordIndexer = new WordIndexer();
20+
21+
theString = "To be, or not to be: that is the question: "
22+
+ "Whether 'tis nobler in the mind to suffer "
23+
+ "The slings and arrows of outrageous fortune, "
24+
+ "Or to take arms against a sea of troubles, "
25+
+ "And by opposing end them? To die: to sleep; "
26+
+ "No more; and by a sleep to say we end "
27+
+ "The heart-ache and the thousand natural shocks "
28+
+ "That flesh is heir to, 'tis a consummation "
29+
+ "Devoutly to be wish'd. To die, to sleep; "
30+
+ "To sleep: perchance to dream: ay, there's the rub: "
31+
+ "For in that sleep of death what dreams may come,";
32+
}
33+
34+
@Test
35+
36+
public void givenWord_whenSearching_thenFindAllIndexedLocations() {
37+
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
38+
39+
List<Integer> actualResult = wordIndexer.findWord(theString, "or");
40+
41+
assertEquals(expectedResult, actualResult);
42+
}
43+
44+
@Test
45+
public void givenWordWithNoRepeatCharacters_whenImprovedSearching_thenFindAllIndexedLocations() {
46+
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
47+
48+
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "or");
49+
50+
assertEquals(expectedResult, actualResult);
51+
}
52+
53+
54+
@Test
55+
public void givenWord_whenSearching_thenFindAtEndOfString() {
56+
List<Integer> expectedResult = Arrays.asList(480);
57+
58+
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "come,");
59+
60+
assertEquals(expectedResult, actualResult);
61+
}
62+
}

0 commit comments

Comments
 (0)