重写 RegexUtil。

feature/net-util
ZhouXY108 2023-06-27 00:07:19 +08:00
parent 59f5a1d17b
commit fc667e5c72
1 changed files with 240 additions and 33 deletions

View File

@ -16,42 +16,132 @@
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.Matcher;
import java.util.regex.Pattern;
public class RegexUtil {
import javax.annotation.Nullable;
private static final Map<String, Pattern> PATTERN_CACHE = new SafeConcurrentHashMap<>();
import com.google.common.base.Preconditions;
public static Pattern getPattern(final String regex) {
Objects.requireNonNull(regex);
return PATTERN_CACHE.computeIfAbsent(regex, Pattern::compile);
/**
* {@link Pattern} 256
*
* @author ZhouXY
*
*/
public final class RegexUtil {
private static final int DEFAULT_CACHE_INITIAL_CAPACITY = 64;
private static final int MAX_CACHE_SIZE = 256;
private static final ConcurrentHashMap<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>(
DEFAULT_CACHE_INITIAL_CAPACITY);
/**
* {@link Pattern}
*
* @param pattern
* @param cachePattern {@link Pattern}
* @return {@link Pattern}
*/
public static Pattern getPattern(final String pattern, final boolean cachePattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null.");
Pattern result = PATTERN_CACHE.get(pattern);
if (result == null) {
result = Pattern.compile(pattern);
if (cachePattern && PATTERN_CACHE.size() < MAX_CACHE_SIZE) {
PATTERN_CACHE.putIfAbsent(pattern, result);
result = PATTERN_CACHE.get(pattern);
}
}
return result;
}
public static boolean matches(@Nullable CharSequence input, String regex) {
return matches(input, getPattern(regex));
/**
* {@link Pattern}
*
* @param pattern
* @return {@link Pattern}
*/
public static Pattern getPattern(final String pattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null.");
Pattern result = PATTERN_CACHE.get(pattern);
if (result == null) {
result = Pattern.compile(pattern);
}
return result;
}
public static boolean matches(@Nullable CharSequence input, Pattern pattern) {
Assert.notNull(pattern, "Pattern must not be null.");
/**
* {@link Pattern}
*
* @param patterns
* @param cachePattern {@link Pattern}
* @return Pattern
*/
public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) {
Preconditions.checkNotNull(patterns, "The patterns can not be null.");
final Pattern[] result = new Pattern[patterns.length];
for (int i = 0; i < patterns.length; i++) {
result[i] = getPattern(patterns[i], cachePattern);
}
return result;
}
/**
* {@link Pattern}
*
* @param patterns
* @return {@link Pattern}
*/
public static Pattern[] getPatterns(final String[] patterns) {
Preconditions.checkNotNull(patterns, "The patterns can not be null.");
final Pattern[] result = new Pattern[patterns.length];
for (int i = 0; i < patterns.length; i++) {
result[i] = getPattern(patterns[i]);
}
return result;
}
/**
* Pattern
*
* @param pattern {@link Pattern}
* @return Pattern {@code null}
*/
public static Pattern cachePattern(final Pattern pattern) {
if (PATTERN_CACHE.size() >= MAX_CACHE_SIZE) {
return null;
}
final String patternStr = pattern.pattern();
PATTERN_CACHE.putIfAbsent(patternStr, pattern);
return PATTERN_CACHE.get(patternStr);
}
/**
* {@code input} {@code pattern}
*
* @param input
* @param pattern
* @return
*/
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null.");
return input != null && pattern.matcher(input).matches();
}
public static boolean matchesOr(@Nullable CharSequence input, String... regexes) {
for (String regex : regexes) {
if (matches(input, regex)) {
return true;
}
/**
* {@code input} {@code patterns}
*
* @param input
* @param patterns
* @return
*/
public static boolean matchOne(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns, "The patterns can not be null.");
if (input == null) {
return false;
}
return false;
}
public static boolean matchesOr(@Nullable CharSequence input, Pattern... patterns) {
for (Pattern pattern : patterns) {
if (matches(input, pattern)) {
return true;
@ -60,16 +150,18 @@ public class RegexUtil {
return false;
}
public static boolean matchesAnd(@Nullable CharSequence input, String... regexes) {
for (String regex : regexes) {
if (!matches(input, regex)) {
return false;
}
/**
* {@code input}
*
* @param input
* @param patterns
* @return
*/
public static boolean matchAll(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns, "The patterns can not be null.");
if (input == null) {
return false;
}
return true;
}
public static boolean matchesAnd(@Nullable CharSequence input, Pattern... patterns) {
for (Pattern pattern : patterns) {
if (!matches(input, pattern)) {
return false;
@ -78,7 +170,122 @@ public class RegexUtil {
return true;
}
/**
* {@code input} {@code pattern}
*
* @param input
* @param pattern
* @param cachePattern {@link Pattern}
* @return
*/
public static boolean matches(@Nullable final CharSequence input, final String pattern,
final boolean cachePattern) {
return matches(input, getPattern(pattern, cachePattern));
}
/**
* {@code input} {@code pattern} {@link Pattern}
*
* @param input
* @param pattern
* @return
*/
public static boolean matches(@Nullable final CharSequence input, final String pattern) {
return matches(input, getPattern(pattern));
}
/**
* {@code input} {@code patterns}
*
* @param input
* @param patterns
* @param cachePattern {@link Pattern}
* @return
*/
public static boolean matchOne(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) {
final Pattern[] patternSet = getPatterns(patterns, cachePattern);
return matchOne(input, patternSet);
}
/**
* {@code input} {@code patterns} {@link Pattern}
*
* @param input
* @param patterns
* @return
*/
public static boolean matchOne(@Nullable final CharSequence input, final String[] patterns) {
final Pattern[] patternSet = getPatterns(patterns);
return matchOne(input, patternSet);
}
/**
* {@code input}
*
* @param input
* @param patterns
* @param cachePattern {@link Pattern}
* @return
*/
public static boolean matchAll(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) {
final Pattern[] patternSet = getPatterns(patterns, cachePattern);
return matchAll(input, patternSet);
}
/**
* {@code input} {@link Pattern}
*
* @param input
* @param patterns
* @return
*/
public static boolean matchAll(@Nullable final CharSequence input, final String[] patterns) {
final Pattern[] patternSet = getPatterns(patterns);
return matchAll(input, patternSet);
}
/**
*
*
* @param input
* @param pattern
* @return
*/
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(input, "The input can not be null");
Preconditions.checkNotNull(pattern, "The pattern can not be null");
return pattern.matcher(input);
}
/**
*
*
* @param input
* @param pattern
* @param cachePattern {@link Pattern}
* @return
*/
public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) {
Preconditions.checkNotNull(input, "The input can not be null");
return getPattern(pattern, cachePattern).matcher(input);
}
/**
* {@link Pattern}
*
* @param input
* @param pattern
* @return
*/
public static Matcher getMatcher(final CharSequence input, final String pattern) {
Preconditions.checkNotNull(input, "The input can not be null");
return getPattern(pattern).matcher(input);
}
private RegexUtil() {
// 不允许实例化
throw new IllegalStateException("Utility class");
}
}