简化代码。

feature/net-util
ZhouXY108 2023-05-31 20:01:35 +08:00
parent 41c079305e
commit 122fff1da6
1 changed files with 4 additions and 12 deletions

View File

@ -18,27 +18,19 @@ package xyz.zhouxy.plusone.commons.util;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class RegexUtil { public class RegexUtil {
private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>(); private static final Map<String, Pattern> PATTERN_CACHE = new SafeConcurrentHashMap<>();
public static Pattern getPattern(final String regex) { public static Pattern getPattern(final String regex) {
Objects.requireNonNull(regex); Objects.requireNonNull(regex);
if (!PATTERN_CACHE.containsKey(regex)) { return PATTERN_CACHE.computeIfAbsent(regex, Pattern::compile);
synchronized (RegexUtil.class) {
if (!PATTERN_CACHE.containsKey(regex)) {
Pattern pattern = Pattern.compile(regex);
PATTERN_CACHE.put(regex, pattern);
return pattern;
}
}
}
return PATTERN_CACHE.get(regex);
} }
public static boolean matches(@Nullable CharSequence input, String regex) { public static boolean matches(@Nullable CharSequence input, String regex) {