1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-21 01:38:51 +00:00

feat: 添加断言类 简化操作流程

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-09-06 20:43:17 +08:00
parent fa168c6617
commit 2ca0872f7c
2 changed files with 47 additions and 1 deletions

View File

@ -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>

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