修改方法名。

dev
ZhouXY108 2024-10-09 18:45:26 +08:00
parent 62fd1b900f
commit 5e450a9bdb
3 changed files with 8 additions and 5 deletions

View File

@ -46,7 +46,7 @@ public final class EnumTools {
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉 public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
Preconditions.checkNotNull(clazz, "Clazz must not be null."); Preconditions.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants(); E[] values = clazz.getEnumConstants();
PreconditionsExt.check((ordinal >= 0 && ordinal < values.length), PreconditionsExt.checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal))); () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
return values[ordinal]; return values[ordinal];
} }

View File

@ -32,7 +32,7 @@ import com.google.common.base.Preconditions;
*/ */
public class PreconditionsExt { public class PreconditionsExt {
public static <E extends Throwable> void check(boolean condition, @Nonnull Supplier<E> e) throws E { public static <E extends Throwable> void checkCondition(boolean condition, @Nonnull Supplier<E> e) throws E {
if (!condition) { if (!condition) {
throw e.get(); throw e.get();
} }

View File

@ -2,17 +2,20 @@ package xyz.zhouxy.plusone.commons.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@Slf4j
class PreconditionsExtTests { class PreconditionsExtTests {
@Test @Test
void testCheck() { void testCheck() {
assertThrows(TestException.class, () -> { assertThrows(TestException.class, () -> {
PreconditionsExt.check(false, () -> new TestException("Test error message.")); PreconditionsExt.checkCondition(false, () -> new TestException("Test error message."));
}); });
} }
@ -25,7 +28,7 @@ class PreconditionsExtTests {
Object obj = new Object(); Object obj = new Object();
assertNotNull(obj); assertNotNull(obj);
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(new Object[]{obj, null})); () -> PreconditionsExt.checkAllNotNull(new Object[] { obj, null }));
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(obj, null)); () -> PreconditionsExt.checkAllNotNull(obj, null));
List<Object> list = Arrays.asList(obj, null); List<Object> list = Arrays.asList(obj, null);
@ -33,7 +36,7 @@ class PreconditionsExtTests {
assertThrows(NullPointerException.class, assertThrows(NullPointerException.class,
() -> PreconditionsExt.checkAllNotNull(list)); () -> PreconditionsExt.checkAllNotNull(list));
PreconditionsExt.checkAllNotNull(new Object[]{obj, "Test"}); PreconditionsExt.checkAllNotNull(new Object[] { obj, "Test" });
PreconditionsExt.checkAllNotNull(obj, "Test"); PreconditionsExt.checkAllNotNull(obj, "Test");
PreconditionsExt.checkAllNotNull(Arrays.asList(obj, "Test")); PreconditionsExt.checkAllNotNull(Arrays.asList(obj, "Test"));
} }