From 122fff1da646832dc115dced8f29efe402e2c772 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 31 May 2023 20:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/util/RegexUtil.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java index 9ecbd1a..3db7630 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java @@ -18,27 +18,19 @@ package xyz.zhouxy.plusone.commons.util; import javax.annotation.Nullable; +import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; + import java.util.Map; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; public class RegexUtil { - private static final Map PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map PATTERN_CACHE = new SafeConcurrentHashMap<>(); public static Pattern getPattern(final String regex) { Objects.requireNonNull(regex); - if (!PATTERN_CACHE.containsKey(regex)) { - 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); + return PATTERN_CACHE.computeIfAbsent(regex, Pattern::compile); } public static boolean matches(@Nullable CharSequence input, String regex) {