mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-12-25 07:08:52 +00:00
feat: 添加断言类 简化操作流程
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
fa168c6617
commit
2ca0872f7c
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>pw.yumc</groupId>
|
||||
<artifactId>YumCore</artifactId>
|
||||
<version>1.8.5</version>
|
||||
<version>1.8.6</version>
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
|
46
src/main/java/pw/yumc/YumCore/utils/Assert.java
Normal file
46
src/main/java/pw/yumc/YumCore/utils/Assert.java
Normal file
@ -0,0 +1,46 @@
|
||||
package pw.yumc.YumCore.utils;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* Created on 2017/9/1 10:49.
|
||||
*/
|
||||
public class Assert<T> {
|
||||
private T t;
|
||||
|
||||
public static <T> Assert<T> of(T value) {
|
||||
return new Assert<>(value);
|
||||
}
|
||||
|
||||
public static void runIsTrue(Boolean bool, Runnable runnable) {
|
||||
if (bool)
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
public static void runOrElse(Boolean bool, Runnable t, Runnable f) {
|
||||
if (bool)
|
||||
t.run();
|
||||
else
|
||||
f.run();
|
||||
}
|
||||
|
||||
public Assert(T t) {
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
public void runNonNull(Consumer<? super T> consumer) {
|
||||
runIsTrue(Objects.nonNull(t), () -> consumer.accept(t));
|
||||
}
|
||||
|
||||
public void runIsNull(Consumer<? super T> consumer) {
|
||||
runIsTrue(Objects.isNull(t), () -> consumer.accept(t));
|
||||
}
|
||||
|
||||
public void runIsAssignable(Class clazz, Consumer<? super T> consumer) {
|
||||
runIsTrue(clazz.isAssignableFrom(t.getClass()), () -> consumer.accept(t));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user