From 25407f2a4315b8ef8d4b75eb8260520a98a1a0c3 Mon Sep 17 00:00:00 2001 From: jose Date: Sat, 27 Apr 2019 00:36:55 -0300 Subject: [PATCH 1/2] BAEL-2832: new java-strings-2 project, with tests and benchmar --- java-strings-2/pom.xml | 150 ++++++++++++++++++ .../SubstringSearchPerformanceComparison.java | 58 +++++++ java-strings-2/src/main/resources/logback.xml | 13 ++ .../string/search/SubstringSearch.java | 70 ++++++++ java-strings-2/src/test/resources/.gitignore | 13 ++ 5 files changed, 304 insertions(+) create mode 100755 java-strings-2/pom.xml create mode 100644 java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java create mode 100644 java-strings-2/src/main/resources/logback.xml create mode 100644 java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java create mode 100644 java-strings-2/src/test/resources/.gitignore diff --git a/java-strings-2/pom.xml b/java-strings-2/pom.xml new file mode 100755 index 0000000000..c314cd8cad --- /dev/null +++ b/java-strings-2/pom.xml @@ -0,0 +1,150 @@ + + 4.0.0 + java-strings-2 + 0.1.0-SNAPSHOT + jar + java-strings-2 + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + commons-io + commons-io + ${commons-io.version} + + + log4j + log4j + ${log4j.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-core.version} + + + com.ibm.icu + icu4j + ${icu4j.version} + + + com.google.guava + guava + ${guava.version} + + + com.vdurmont + emoji-java + ${emoji-java.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + junit + junit + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter-api.version} + test + + + + org.hamcrest + hamcrest-library + ${hamcrest-library.version} + test + + + + + org.passay + passay + ${passay.version} + + + org.apache.commons + commons-text + ${commons-text.version} + + + + org.ahocorasick + ahocorasick + ${ahocorasick.version} + + + + + + java-strings-2 + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + ${java.version} + -parameters + + + + + + + + 3.8.1 + 1.10 + + 3.6.1 + 1.19 + 61.1 + 27.0.1-jre + 4.0.0 + 5.3.1 + 1.3 + 1.3.1 + 1.4 + 0.4.0 + + + \ No newline at end of file diff --git a/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java b/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java new file mode 100644 index 0000000000..bf33c47a7e --- /dev/null +++ b/java-strings-2/src/main/java/com/baeldung/string/search/performance/SubstringSearchPerformanceComparison.java @@ -0,0 +1,58 @@ +package com.baeldung.string.search.performance; + +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +/** + * Based on https://github.com/tedyoung/indexof-contains-benchmark + */ +@Fork(5) +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +public class SubstringSearchPerformanceComparison { + + private String message; + + private Pattern pattern; + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } + + @Setup + public void setup() { + message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; + pattern = Pattern.compile("(? + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java b/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java new file mode 100644 index 0000000000..4a5adb45ef --- /dev/null +++ b/java-strings-2/src/test/java/com/baeldung/string/search/SubstringSearch.java @@ -0,0 +1,70 @@ +package com.baeldung.string.search; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * BAEL-2832: Different ways to check if a Substring could be found in a String. + */ +public class SubstringSearch { + + @Test + public void searchSubstringWithIndexOf() { + Assert.assertEquals(9, "Bohemian Rhapsodyan".indexOf("Rhap")); + + // indexOf will return -1, because it's case sensitive + Assert.assertEquals(-1, "Bohemian Rhapsodyan".indexOf("rhap")); + + // indexOf will return 9, because it's all lowercase + Assert.assertEquals(9, "Bohemian Rhapsodyan".toLowerCase() + .indexOf("rhap")); + + // it will return 6, because it's the first occurrence. Sorry Queen for being blasphemic + Assert.assertEquals(6, "Bohemian Rhapsodyan".indexOf("an")); + } + + @Test + public void searchSubstringWithContains() { + Assert.assertTrue("Hey Ho, let's go".contains("Hey")); + + // contains will return false, because it's case sensitive + Assert.assertFalse("Hey Ho, let's go".contains("hey")); + + // contains will return true, because it's all lowercase + Assert.assertTrue("Hey Ho, let's go".toLowerCase().contains("hey")); + + // contains will return false, because 'jey' can't be found + Assert.assertFalse("Hey Ho, let's go".contains("jey")); + } + + @Test + public void searchSubstringWithStringUtils() { + Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "train")); + + // it will also be true, because ignores case ;) + Assert.assertTrue(StringUtils.containsIgnoreCase("Runaway train", "Train")); + } + + @Test + public void searchUsingPattern() { + + // We create the Pattern first + Pattern pattern = Pattern.compile("(? Date: Sat, 27 Apr 2019 00:40:12 -0300 Subject: [PATCH 2/2] BAEL-2832: added java-strings-2 module to main pom.xml --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 5e1098d216..403e3c7ed5 100644 --- a/pom.xml +++ b/pom.xml @@ -462,6 +462,7 @@ java-streams java-streams-2 java-strings + java-strings-2 java-vavr-stream java-websocket javafx @@ -1133,6 +1134,7 @@ java-streams java-streams-2 java-strings + java-strings-2 java-vavr-stream java-websocket javafx