BAEL-2142 Code revision to WordIndexer and JUnit (#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
This commit is contained in:
Larry Chung 2018-10-26 22:06:48 -04:00 committed by KevinGilmore
parent 57f89ee1ee
commit 84ef2cd246
3 changed files with 121 additions and 1 deletions

18
java-strings/pom.xml Normal file → Executable file
View File

@ -68,6 +68,21 @@
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- Added for password generation -->
<dependency>
<groupId>org.passay</groupId>
@ -79,6 +94,7 @@
<artifactId>commons-text</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
@ -115,4 +131,4 @@
<guava.version>26.0-jre</guava.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,42 @@
package com.baeldung.string.searching;
import java.util.ArrayList;
import java.util.List;
public class WordIndexer {
public List<Integer> findWord(String textString, String word) {
int index = 0;
List<Integer> indexes = new ArrayList<Integer>();
String lowerCaseTextString = textString.toLowerCase();
String lowerCaseWord = word.toLowerCase();
while(index != -1){
index = lowerCaseTextString.indexOf(lowerCaseWord, index + 1);
if (index != -1) {
indexes.add(index);
}
}
return indexes;
}
public List<Integer> findWordUpgrade(String textString, String word) {
int index = 0;
List<Integer> indexes = new ArrayList<Integer>();
StringBuilder output = new StringBuilder();
String lowerCaseTextString = textString.toLowerCase();
String lowerCaseWord = word.toLowerCase();
int wordLength = 0;
while(index != -1){
index = lowerCaseTextString.indexOf(lowerCaseWord, index + wordLength); // Slight improvement
if (index != -1) {
indexes.add(index);
}
wordLength = word.length();
}
return indexes;
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.string.searching;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class WordIndexerUnitTest {
String theString;
WordIndexer wordIndexer;
@BeforeEach
public void setUp() throws Exception {
wordIndexer = new WordIndexer();
theString = "To be, or not to be: that is the question: "
+ "Whether 'tis nobler in the mind to suffer "
+ "The slings and arrows of outrageous fortune, "
+ "Or to take arms against a sea of troubles, "
+ "And by opposing end them? To die: to sleep; "
+ "No more; and by a sleep to say we end "
+ "The heart-ache and the thousand natural shocks "
+ "That flesh is heir to, 'tis a consummation "
+ "Devoutly to be wish'd. To die, to sleep; "
+ "To sleep: perchance to dream: ay, there's the rub: "
+ "For in that sleep of death what dreams may come,";
}
@Test
public void givenWord_whenSearching_thenFindAllIndexedLocations() {
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
List<Integer> actualResult = wordIndexer.findWord(theString, "or");
assertEquals(expectedResult, actualResult);
}
@Test
public void givenWordWithNoRepeatCharacters_whenImprovedSearching_thenFindAllIndexedLocations() {
List<Integer> expectedResult = Arrays.asList(7, 122, 130, 221, 438);
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "or");
assertEquals(expectedResult, actualResult);
}
@Test
public void givenWord_whenSearching_thenFindAtEndOfString() {
List<Integer> expectedResult = Arrays.asList(480);
List<Integer> actualResult = wordIndexer.findWordUpgrade(theString, "come,");
assertEquals(expectedResult, actualResult);
}
}