mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
686142442a
@ -2,8 +2,11 @@ package cn.hutool.core.lang;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
import cn.hutool.core.lang.func.VoidFunc0;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@ -16,6 +19,23 @@ import cn.hutool.core.util.StrUtil;
|
||||
*/
|
||||
public class Assert {
|
||||
|
||||
/**
|
||||
* 断言是否为真,如果为 {@code false} 抛出给定的异常<br>
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.isTrue(i > 0, IllegalArgumentException::new);
|
||||
* </pre>
|
||||
*
|
||||
* @param expression 布尔值
|
||||
* @param supplier 指定断言不通过时抛出的异常
|
||||
* @throws X if expression is {@code false}
|
||||
*/
|
||||
public static <X extends Throwable> void isTrue(boolean expression, Func0<? extends X> supplier) throws X {
|
||||
if (false == expression) {
|
||||
throw supplier.callWithRuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常<br>
|
||||
*
|
||||
|
@ -14,4 +14,16 @@ public class AssertTest {
|
||||
String a = null;
|
||||
cn.hutool.core.lang.Assert.isNull(a);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void isTrueTest() {
|
||||
int i = 0;
|
||||
cn.hutool.core.lang.Assert.isTrue(i > 0, IllegalArgumentException::new);
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
public void isTrueTest2() {
|
||||
int i = -1;
|
||||
cn.hutool.core.lang.Assert.isTrue(i >= 0, IndexOutOfBoundsException::new);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user