Skip to content

Commit 87b7cd6

Browse files
committed
OperatorMatches caches compiled patterns
Issue: SPR-12610 (cherry picked from commit d34402d)
1 parent 901d7d0 commit 87b7cd6

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.expression.spel.ast;
1818

19+
import java.util.concurrent.ConcurrentHashMap;
20+
import java.util.concurrent.ConcurrentMap;
1921
import java.util.regex.Matcher;
2022
import java.util.regex.Pattern;
2123
import java.util.regex.PatternSyntaxException;
@@ -27,15 +29,20 @@
2729
import org.springframework.expression.spel.support.BooleanTypedValue;
2830

2931
/**
30-
* Implements the matches operator. Matches takes two operands. The first is a string and
31-
* the second is a java regex. It will return true when getValue() is called if the first
32-
* operand matches the regex.
32+
* Implements the matches operator. Matches takes two operands:
33+
* The first is a String and the second is a Java regex.
34+
* It will return {@code true} when {@link #getValue} is called
35+
* if the first operand matches the regex.
3336
*
3437
* @author Andy Clement
38+
* @author Juergen Hoeller
3539
* @since 3.0
3640
*/
3741
public class OperatorMatches extends Operator {
3842

43+
private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<String, Pattern>();
44+
45+
3946
public OperatorMatches(int pos, SpelNodeImpl... operands) {
4047
super("matches", pos, operands);
4148
}
@@ -66,8 +73,14 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
6673
}
6774

6875
try {
69-
Pattern pattern = Pattern.compile((String) right);
70-
Matcher matcher = pattern.matcher((String) left);
76+
String leftString = (String) left;
77+
String rightString = (String) right;
78+
Pattern pattern = this.patternCache.get(rightString);
79+
if (pattern == null) {
80+
pattern = Pattern.compile(rightString);
81+
this.patternCache.putIfAbsent(rightString, pattern);
82+
}
83+
Matcher matcher = pattern.matcher(leftString);
7184
return BooleanTypedValue.forValue(matcher.matches());
7285
}
7386
catch (PatternSyntaxException ex) {

0 commit comments

Comments
 (0)