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:
parent
57f89ee1ee
commit
84ef2cd246
16
java-strings/pom.xml
Normal file → Executable file
16
java-strings/pom.xml
Normal file → Executable file
@ -68,6 +68,21 @@
|
|||||||
<artifactId>emoji-java</artifactId>
|
<artifactId>emoji-java</artifactId>
|
||||||
<version>4.0.0</version>
|
<version>4.0.0</version>
|
||||||
</dependency>
|
</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 -->
|
<!-- Added for password generation -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.passay</groupId>
|
<groupId>org.passay</groupId>
|
||||||
@ -79,6 +94,7 @@
|
|||||||
<artifactId>commons-text</artifactId>
|
<artifactId>commons-text</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user