resolve conflicts
This commit is contained in:
@@ -4,4 +4,4 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance)
|
||||
- [Pre-compile Regex Patterns Into Pattern Objects](https://www.baeldung.com/java-regex-pre-compile)
|
||||
- [Pre-compile Regex Patterns Into Pattern Objects](https://www.baeldung.com/java-regex-pre-compile)
|
||||
@@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-text</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-text</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-text</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.patternreuse;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
import org.openjdk.jmh.runner.RunnerException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Fork(value = 1, warmups = 1)
|
||||
@Warmup(iterations = 5)
|
||||
@State(Scope.Benchmark)
|
||||
public class PatternPerformanceComparison {
|
||||
|
||||
private static final String PATTERN = "\\d*[02468]";
|
||||
private static List<String> values;
|
||||
|
||||
private static Matcher matcherFromPreCompiledPattern;
|
||||
private static Pattern preCompiledPattern;
|
||||
|
||||
public static void main(String[] args) throws IOException, RunnerException {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void matcherFromPreCompiledPatternResetMatches(Blackhole bh) {
|
||||
//With pre-compiled pattern and reusing the matcher
|
||||
// 1 Pattern object created
|
||||
// 1 Matcher objects created
|
||||
for (String value : values) {
|
||||
bh.consume(matcherFromPreCompiledPattern.reset(value).matches());
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void preCompiledPatternMatcherMatches(Blackhole bh) {
|
||||
// With pre-compiled pattern
|
||||
// 1 Pattern object created
|
||||
// 5_000_000 Matcher objects created
|
||||
for (String value : values) {
|
||||
bh.consume(preCompiledPattern.matcher(value).matches());
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void patternCompileMatcherMatches(Blackhole bh) {
|
||||
// Above approach "Pattern.matches(PATTERN, value)" makes this internally
|
||||
// 5_000_000 Pattern objects created
|
||||
// 5_000_000 Matcher objects created
|
||||
for (String value : values) {
|
||||
bh.consume(Pattern.compile(PATTERN).matcher(value).matches());
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void patternMatches(Blackhole bh) {
|
||||
// Above approach "value.matches(PATTERN)" makes this internally
|
||||
// 5_000_000 Pattern objects created
|
||||
// 5_000_000 Matcher objects created
|
||||
for (String value : values) {
|
||||
bh.consume(Pattern.matches(PATTERN, value));
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void stringMatchs(Blackhole bh) {
|
||||
// 5_000_000 Pattern objects created
|
||||
// 5_000_000 Matcher objects created
|
||||
Instant start = Instant.now();
|
||||
for (String value : values) {
|
||||
bh.consume(value.matches(PATTERN));
|
||||
}
|
||||
}
|
||||
|
||||
@Setup()
|
||||
public void setUp() {
|
||||
preCompiledPattern = Pattern.compile(PATTERN);
|
||||
matcherFromPreCompiledPattern = preCompiledPattern.matcher("");
|
||||
|
||||
values = new ArrayList<>();
|
||||
for (int x = 1; x <= 5_000_000; x++) {
|
||||
values.add(String.valueOf(x));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.patternreuse;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PatternUnitTest {
|
||||
|
||||
private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}");
|
||||
private static final Pattern SPLIT_PRE_COMPILED_PATTERN = Pattern.compile("__");
|
||||
|
||||
@Test
|
||||
public void givenPreCompiledPattern_whenCallMatcher_thenReturnAMatcherToMatches() {
|
||||
Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Fabio Silva");
|
||||
Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Mr. Silva");
|
||||
|
||||
boolean matchesName1 = matcherName1.matches();
|
||||
boolean matchesName2 = matcherName2.matches();
|
||||
|
||||
assertTrue(matchesName1);
|
||||
assertFalse(matchesName2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreCompiledPattern_whenCallAsPredicate_thenReturnPredicateToFindPatternInTheList() {
|
||||
List<String> namesToValidate = Arrays.asList("Fabio Silva", "Mr. Silva");
|
||||
Predicate<String> patternsAsPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asPredicate();
|
||||
|
||||
List<String> validNames = namesToValidate.stream()
|
||||
.filter(patternsAsPredicate)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(1, validNames.size());
|
||||
assertTrue(validNames.contains("Fabio Silva"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreCompiledPattern_whenCallSplit_thenReturnArrayWithValuesSplitByThePattern() {
|
||||
String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split("My_Name__is__Fabio_Silva");
|
||||
|
||||
assertEquals("My_Name", textSplit[0]);
|
||||
assertEquals("is", textSplit[1]);
|
||||
assertEquals("Fabio_Silva", textSplit[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPreCompiledPattern_whenCallSplitAsStream_thenReturnArraySplitByThePattern() {
|
||||
Stream<String> textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream("My_Name__is__Fabio_Silva");
|
||||
String[] textSplit = textSplitAsStream.toArray(String[]::new);
|
||||
|
||||
assertEquals("My_Name", textSplit[0]);
|
||||
assertEquals("is", textSplit[1]);
|
||||
assertEquals("Fabio_Silva", textSplit[2]);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.regex.matcher;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MatcherUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFindFourDigitWorks_thenCorrect() {
|
||||
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
|
||||
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
|
||||
|
||||
assertTrue(m.find());
|
||||
assertEquals(8, m.start());
|
||||
assertEquals("2019", m.group());
|
||||
assertEquals(12, m.end());
|
||||
|
||||
assertTrue(m.find());
|
||||
assertEquals(25, m.start());
|
||||
assertEquals("2020", m.group());
|
||||
assertEquals(29, m.end());
|
||||
|
||||
assertFalse(m.find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStartIndex_whenFindFourDigitWorks_thenCorrect() {
|
||||
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
|
||||
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
|
||||
|
||||
assertTrue(m.find(20));
|
||||
assertEquals(25, m.start());
|
||||
assertEquals("2020", m.group());
|
||||
assertEquals(29, m.end());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchFourDigitWorks_thenFail() {
|
||||
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
|
||||
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
|
||||
assertFalse(m.matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchFourDigitWorks_thenCorrect() {
|
||||
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
|
||||
Matcher m = stringPattern.matcher("2019");
|
||||
|
||||
assertTrue(m.matches());
|
||||
assertEquals(0, m.start());
|
||||
assertEquals("2019", m.group());
|
||||
assertEquals(4, m.end());
|
||||
|
||||
assertTrue(m.matches());// matches will always return the same return
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user