优化 RegexTools 代码

dev
ZhouXY108 2024-09-03 17:07:42 +08:00
parent e9c93a273c
commit 007e44c1d2
1 changed files with 43 additions and 55 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2022-2023 the original author or authors. * Copyright 2022-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,8 +50,8 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern getPattern(final String pattern, final boolean cachePattern) { public static Pattern getPattern(final String pattern, final boolean cachePattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return cachePattern ? getAndCachePatternInternal(pattern) : getPatternInternal(pattern); return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern);
} }
/** /**
@ -61,7 +61,7 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern getPattern(final String pattern) { public static Pattern getPattern(final String pattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return getPatternInternal(pattern); return getPatternInternal(pattern);
} }
@ -73,10 +73,10 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) { public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
return cachePattern return cachePattern
? getAndCachePatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
} }
@ -87,8 +87,8 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern[] getPatterns(final String[] patterns) { public static Pattern[] getPatterns(final String[] patterns) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
return getPatternsInternal(patterns); return getPatternsInternal(patterns);
} }
@ -116,7 +116,7 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) { public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return matchesInternal(input, pattern); return matchesInternal(input, pattern);
} }
@ -128,8 +128,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
return matchesOneInternal(input, patterns); return matchesOneInternal(input, patterns);
} }
@ -141,8 +141,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
return matchesAllInternal(input, patterns); return matchesAllInternal(input, patterns);
} }
@ -156,9 +156,9 @@ public final class RegexTools {
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern, public static boolean matches(@Nullable final CharSequence input, final String pattern,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
Pattern p = cachePattern Pattern p = cachePattern
? getAndCachePatternInternal(pattern) ? cacheAndGetPatternInternal(pattern)
: getPatternInternal(pattern); : getPatternInternal(pattern);
return matchesInternal(input, p); return matchesInternal(input, p);
} }
@ -171,7 +171,7 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern) { public static boolean matches(@Nullable final CharSequence input, final String pattern) {
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return matchesInternal(input, getPatternInternal(pattern)); return matchesInternal(input, getPatternInternal(pattern));
} }
@ -185,10 +185,10 @@ public final class RegexTools {
*/ */
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns, public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = cachePattern final Pattern[] patternSet = cachePattern
? getAndCachePatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
return matchesOneInternal(input, patternSet); return matchesOneInternal(input, patternSet);
} }
@ -201,8 +201,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) { public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns); final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesOneInternal(input, patternSet); return matchesOneInternal(input, patternSet);
} }
@ -217,10 +217,10 @@ public final class RegexTools {
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns, public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = cachePattern final Pattern[] patternSet = cachePattern
? getAndCachePatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
return matchesAllInternal(input, patternSet); return matchesAllInternal(input, patternSet);
} }
@ -233,8 +233,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) { public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) {
Preconditions.checkNotNull(patterns, "Patterns can not be null."); Preconditions.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns), "The pattern can not be null."); Preconditions.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns); final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesAllInternal(input, patternSet); return matchesAllInternal(input, patternSet);
} }
@ -247,8 +247,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) { public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(input, "The input can not be null."); Preconditions.checkNotNull(input);
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return pattern.matcher(input); return pattern.matcher(input);
} }
@ -261,10 +261,10 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) { public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) {
Preconditions.checkNotNull(input, "The input can not be null."); Preconditions.checkNotNull(input);
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
final Pattern p = cachePattern final Pattern p = cachePattern
? getAndCachePatternInternal(pattern) ? cacheAndGetPatternInternal(pattern)
: getPatternInternal(pattern); : getPatternInternal(pattern);
return p.matcher(input); return p.matcher(input);
} }
@ -277,8 +277,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern) { public static Matcher getMatcher(final CharSequence input, final String pattern) {
Preconditions.checkNotNull(input, "The input can not be null."); Preconditions.checkNotNull(input);
Preconditions.checkNotNull(pattern, "The pattern can not be null."); Preconditions.checkNotNull(pattern);
return getPatternInternal(pattern).matcher(input); return getPatternInternal(pattern).matcher(input);
} }
@ -292,7 +292,7 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
@Nonnull @Nonnull
private static Pattern getAndCachePatternInternal(@Nonnull final String pattern) { private static Pattern cacheAndGetPatternInternal(@Nonnull final String pattern) {
if (PATTERN_CACHE.size() < MAX_CACHE_SIZE) { if (PATTERN_CACHE.size() < MAX_CACHE_SIZE) {
return PATTERN_CACHE.computeIfAbsent(pattern, Pattern::compile); return PATTERN_CACHE.computeIfAbsent(pattern, Pattern::compile);
} }
@ -325,9 +325,9 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
@Nonnull @Nonnull
private static Pattern[] getAndCachePatternsInternal(@Nonnull final String[] patterns) { private static Pattern[] cacheAndGetPatternsInternal(@Nonnull final String[] patterns) {
return Arrays.stream(patterns) return Arrays.stream(patterns)
.map(RegexTools::getAndCachePatternInternal) .map(RegexTools::cacheAndGetPatternInternal)
.toArray(Pattern[]::new); .toArray(Pattern[]::new);
} }
@ -356,27 +356,15 @@ public final class RegexTools {
} }
private static boolean matchesOneInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) { private static boolean matchesOneInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) {
if (input == null) { return input != null
return false; && Arrays.stream(patterns)
} .anyMatch(pattern -> pattern.matcher(input).matches());
for (Pattern pattern : patterns) {
if (matchesInternal(input, Objects.requireNonNull(pattern))) {
return true;
}
}
return false;
} }
private static boolean matchesAllInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) { private static boolean matchesAllInternal(@Nullable final CharSequence input, @Nonnull final Pattern[] patterns) {
if (input == null) { return input != null
return false; && Arrays.stream(patterns)
} .allMatch(pattern -> pattern.matcher(input).matches());
for (Pattern pattern : patterns) {
if (!matchesInternal(input, Objects.requireNonNull(pattern))) {
return false;
}
}
return true;
} }
private static <T> boolean allNotNull(T[] array) { private static <T> boolean allNotNull(T[] array) {