Skip to content

Commit 7682b3c

Browse files
kamilkrzywanskimp911de
authored andcommitted
Improve PropertyMatch performance using precompiled regex patterns.
Signed-off-by: Kamil Krzywanski <kamilkrzywanski01@gmail.com> Closes #3375 Original pull request: #3376
1 parent c3edbd5 commit 7682b3c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.locks.Lock;
3030
import java.util.concurrent.locks.ReentrantReadWriteLock;
3131
import java.util.function.Predicate;
32+
import java.util.regex.Pattern;
3233
import java.util.stream.Collectors;
3334

3435
import org.apache.commons.logging.Log;
@@ -93,6 +94,7 @@
9394
* @author Mark Paluch
9495
* @author Mikael Klamra
9596
* @author Christoph Strobl
97+
* @author Kamil Krzywański
9698
*/
9799
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
98100
implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, BeanFactoryAware,
@@ -819,8 +821,8 @@ public boolean matches(Property property) {
819821
*/
820822
static class PropertyMatch {
821823

822-
private final @Nullable String namePattern;
823-
private final @Nullable String typeName;
824+
private final @Nullable Pattern namePatternRegex;
825+
private final @Nullable String typeName;
824826

825827
/**
826828
* Creates a new {@link PropertyMatch} for the given name pattern and type name. At least one of the parameters
@@ -833,8 +835,8 @@ public PropertyMatch(@Nullable String namePattern, @Nullable String typeName) {
833835

834836
Assert.isTrue(!((namePattern == null) && (typeName == null)), "Either name pattern or type name must be given");
835837

836-
this.namePattern = namePattern;
837-
this.typeName = typeName;
838+
this.namePatternRegex = namePattern == null ? null : Pattern.compile(namePattern);
839+
this.typeName = typeName;
838840
}
839841

840842
/**
@@ -869,9 +871,9 @@ public boolean matches(String name, Class<?> type) {
869871
Assert.notNull(name, "Name must not be null");
870872
Assert.notNull(type, "Type must not be null");
871873

872-
if ((namePattern != null) && !name.matches(namePattern)) {
873-
return false;
874-
}
874+
if (namePatternRegex != null && !namePatternRegex.matcher(name).matches()) {
875+
return false;
876+
}
875877

876878
if ((typeName != null) && !type.getName().equals(typeName)) {
877879
return false;

0 commit comments

Comments
 (0)