优化代码。

feature/net-util
ZhouXY108 2023-04-25 09:06:00 +08:00
parent a771b207f8
commit d2f50cede5
4 changed files with 12 additions and 26 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

@ -21,8 +21,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
/** /**
* *
*/ */
@ -71,7 +69,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
} }
protected static final class ValueSet<T extends Enumeration<T>> { protected static final class ValueSet<T extends Enumeration<T>> {
private final Map<Integer, @Nonnull T> values = new ConcurrentHashMap<>(); private final Map<Integer, T> values = new ConcurrentHashMap<>();
@SafeVarargs @SafeVarargs
public ValueSet(T... values) { public ValueSet(T... values) {
@ -89,7 +87,7 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
return this.values.get(id); return this.values.get(id);
} }
public Collection<@Nonnull T> getValues() { public Collection<T> getValues() {
return this.values.values(); return this.values.values();
} }
} }

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;
} }
} }

View File

@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.commons; package xyz.zhouxy.plusone.commons;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertSame;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -17,8 +17,8 @@ class EnumerationTests {
@Test @Test
void testEnumeration() { void testEnumeration() {
assertTrue(EntityStatus.AVAILABLE == EntityStatus.of(0)); assertSame(EntityStatus.AVAILABLE, EntityStatus.of(0));
assertTrue(Result.SUCCESSFUL == Result.of(1)); assertSame(Result.SUCCESSFUL, Result.of(1));
Collection<Comparable<? extends Enumeration<?>>> enums = Lists.newArrayList(); Collection<Comparable<? extends Enumeration<?>>> enums = Lists.newArrayList();
enums.addAll(EntityStatus.constants()); enums.addAll(EntityStatus.constants());
enums.addAll(Result.constants()); enums.addAll(Result.constants());