|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.expression.spel.ast; |
18 | 18 |
|
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | +import java.util.concurrent.ConcurrentMap; |
19 | 21 | import java.util.regex.Matcher; |
20 | 22 | import java.util.regex.Pattern; |
21 | 23 | import java.util.regex.PatternSyntaxException; |
|
27 | 29 | import org.springframework.expression.spel.support.BooleanTypedValue; |
28 | 30 |
|
29 | 31 | /** |
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. |
33 | 36 | * |
34 | 37 | * @author Andy Clement |
| 38 | + * @author Juergen Hoeller |
35 | 39 | * @since 3.0 |
36 | 40 | */ |
37 | 41 | public class OperatorMatches extends Operator { |
38 | 42 |
|
| 43 | + private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<String, Pattern>(); |
| 44 | + |
| 45 | + |
39 | 46 | public OperatorMatches(int pos, SpelNodeImpl... operands) { |
40 | 47 | super("matches", pos, operands); |
41 | 48 | } |
@@ -66,8 +73,14 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati |
66 | 73 | } |
67 | 74 |
|
68 | 75 | 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); |
71 | 84 | return BooleanTypedValue.forValue(matcher.matches()); |
72 | 85 | } |
73 | 86 | catch (PatternSyntaxException ex) { |
|
0 commit comments