feature/net-util
ZhouXY108 2023-04-29 15:09:53 +08:00
commit 3d51eab3b4
3 changed files with 10 additions and 22 deletions

View File

@ -166,7 +166,7 @@ public class Assert {
Assert.isFalse((arr == null || arr.length() == 0), errorMessageTemplate, args); Assert.isFalse((arr == null || arr.length() == 0), errorMessageTemplate, args);
} }
// private consrtuctor // private constructor
private Assert() { private Assert() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@ -74,12 +74,12 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
@SafeVarargs @SafeVarargs
public ValueSet(T... values) { public ValueSet(T... values) {
Map<Integer, T> valueMap = new HashMap<>(values.length); Map<Integer, T> temp = new HashMap<>(values.length);
for (T value : values) { for (T value : values) {
Assert.notNull(value, "Value must not be null."); Assert.notNull(value, "Value must not be null.");
valueMap.put(value.getId(), value); temp.put(value.getId(), value);
} }
this.valueMap = Collections.unmodifiableMap(valueMap); this.valueMap = Collections.unmodifiableMap(temp);
} }
public T get(int id) { public T get(int id) {

View File

@ -16,7 +16,6 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
@ -26,7 +25,7 @@ import java.util.regex.Pattern;
public class RegexUtil { public class RegexUtil {
private static final Map<String, @Nonnull Pattern> PATTERN_CACHE = new ConcurrentHashMap<>(); private static final Map<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
public static Pattern getPattern(final String regex) { public static Pattern getPattern(final String regex) {
Objects.requireNonNull(regex); Objects.requireNonNull(regex);
@ -34,9 +33,6 @@ public class RegexUtil {
return PATTERN_CACHE.get(regex); return PATTERN_CACHE.get(regex);
} }
Pattern pattern = Pattern.compile(regex); Pattern pattern = Pattern.compile(regex);
if (pattern == null) {
throw new IllegalArgumentException("Regex must not be null.");
}
PATTERN_CACHE.put(regex, pattern); PATTERN_CACHE.put(regex, pattern);
return pattern; return pattern;
} }
@ -47,14 +43,12 @@ public class RegexUtil {
public static boolean matches(@Nullable CharSequence input, Pattern pattern) { public static boolean matches(@Nullable CharSequence input, Pattern pattern) {
Assert.notNull(pattern, "Pattern must not be null."); Assert.notNull(pattern, "Pattern must not be null.");
return pattern.matcher(input).matches(); return input != null && pattern.matcher(input).matches();
} }
public static boolean matchesOr(@Nullable CharSequence input, String... regexes) { public static boolean matchesOr(@Nullable CharSequence input, String... regexes) {
boolean isMatched;
for (String regex : regexes) { for (String regex : regexes) {
isMatched = matches(input, regex); if (matches(input, regex)) {
if (isMatched) {
return true; return true;
} }
} }
@ -62,10 +56,8 @@ public class RegexUtil {
} }
public static boolean matchesOr(@Nullable CharSequence input, Pattern... patterns) { public static boolean matchesOr(@Nullable CharSequence input, Pattern... patterns) {
boolean isMatched;
for (Pattern pattern : patterns) { for (Pattern pattern : patterns) {
isMatched = matches(input, pattern); if (matches(input, pattern)) {
if (isMatched) {
return true; return true;
} }
} }
@ -73,10 +65,8 @@ public class RegexUtil {
} }
public static boolean matchesAnd(@Nullable CharSequence input, String... regexes) { public static boolean matchesAnd(@Nullable CharSequence input, String... regexes) {
boolean isMatched;
for (String regex : regexes) { for (String regex : regexes) {
isMatched = matches(input, regex); if (!matches(input, regex)) {
if (!isMatched) {
return false; return false;
} }
} }
@ -84,10 +74,8 @@ public class RegexUtil {
} }
public static boolean matchesAnd(@Nullable CharSequence input, Pattern... patterns) { public static boolean matchesAnd(@Nullable CharSequence input, Pattern... patterns) {
boolean isMatched;
for (Pattern pattern : patterns) { for (Pattern pattern : patterns) {
isMatched = matches(input, pattern); if (!matches(input, pattern)) {
if (!isMatched) {
return false; return false;
} }
} }