Skip to content

Commit a8fbf55

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 5902d65 commit a8fbf55

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,
@@ -812,8 +814,8 @@ public boolean matches(Property property) {
812814
*/
813815
static class PropertyMatch {
814816

815-
private final @Nullable String namePattern;
816-
private final @Nullable String typeName;
817+
private final @Nullable Pattern namePatternRegex;
818+
private final @Nullable String typeName;
817819

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

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

829-
this.namePattern = namePattern;
830-
this.typeName = typeName;
831+
this.namePatternRegex = namePattern == null ? null : Pattern.compile(namePattern);
832+
this.typeName = typeName;
831833
}
832834

833835
/**
@@ -862,9 +864,9 @@ public boolean matches(String name, Class<?> type) {
862864
Assert.notNull(name, "Name must not be null");
863865
Assert.notNull(type, "Type must not be null");
864866

865-
if ((namePattern != null) && !name.matches(namePattern)) {
866-
return false;
867-
}
867+
if (namePatternRegex != null && !namePatternRegex.matcher(name).matches()) {
868+
return false;
869+
}
868870

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

0 commit comments

Comments
 (0)