From 84ef2cd246e1d4776175ab5820ddeba7f88f7996 Mon Sep 17 00:00:00 2001 From: Larry Chung Date: Fri, 26 Oct 2018 22:06:48 -0400 Subject: [PATCH] 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 * Update java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java Co-Authored-By: codewarrior2000 * Update java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java Co-Authored-By: codewarrior2000 * Respond to Kevin Gilmore's code changes * Debugging code change to JUnit --- java-strings/pom.xml | 18 +++++- .../string/searching/WordIndexer.java | 42 +++++++++++++ .../string/searching/WordIndexerUnitTest.java | 62 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) mode change 100644 => 100755 java-strings/pom.xml create mode 100644 java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java create mode 100644 java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java diff --git a/java-strings/pom.xml b/java-strings/pom.xml old mode 100644 new mode 100755 index a43490ce5c..ab94c28d4d --- a/java-strings/pom.xml +++ b/java-strings/pom.xml @@ -68,6 +68,21 @@ emoji-java 4.0.0 + + + org.junit.jupiter + junit-jupiter-api + 5.3.1 + test + + + + org.hamcrest + hamcrest-library + 1.3 + test + + org.passay @@ -79,6 +94,7 @@ commons-text 1.4 + @@ -115,4 +131,4 @@ 26.0-jre - \ No newline at end of file + diff --git a/java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java b/java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java new file mode 100644 index 0000000000..5314efb0b4 --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/searching/WordIndexer.java @@ -0,0 +1,42 @@ +package com.baeldung.string.searching; + +import java.util.ArrayList; +import java.util.List; + +public class WordIndexer { + + public List findWord(String textString, String word) { + int index = 0; + List indexes = new ArrayList(); + 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 findWordUpgrade(String textString, String word) { + int index = 0; + List indexes = new ArrayList(); + 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; + } +} diff --git a/java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java b/java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java new file mode 100644 index 0000000000..f3f76db01f --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/searching/WordIndexerUnitTest.java @@ -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 expectedResult = Arrays.asList(7, 122, 130, 221, 438); + + List actualResult = wordIndexer.findWord(theString, "or"); + + assertEquals(expectedResult, actualResult); + } + + @Test + public void givenWordWithNoRepeatCharacters_whenImprovedSearching_thenFindAllIndexedLocations() { + List expectedResult = Arrays.asList(7, 122, 130, 221, 438); + + List actualResult = wordIndexer.findWordUpgrade(theString, "or"); + + assertEquals(expectedResult, actualResult); + } + + + @Test + public void givenWord_whenSearching_thenFindAtEndOfString() { + List expectedResult = Arrays.asList(480); + + List actualResult = wordIndexer.findWordUpgrade(theString, "come,"); + + assertEquals(expectedResult, actualResult); + } +}