前言
我们在写业务代码的时候或多或少会写比较多的if else 进行判断抛出异常、分支处理等操作。这些if...else...充斥在代码中严重影响了代码代码的美观,这时我们可以利用java8的新特性来优雅的写if...else...。
if if if 这种结构
原来的写法
@Test
public void testOrigin1() {
String s = "abc";
if (s.contains("a")) {
System.out.println("a");
}
if (s.contains("b")) {
System.out.println("b");
}
System.out.println(s);
}
现在的
@Test
public void testNew1() {
String s = "abc";
BranchUtil.of(s)
.chain()
.match(str -> str.contains("a")).ifTrue(str-> System.out.println("a"))
.match(str -> str.contains("b")).ifTrue(str-> System.out.println("b"))
.orElse(str -> System.out.println(str));
}
if else if else if 这种结构
原来的写法
@Test
public void test22() {
String s = "123";
if (s.contains("1")) {
System.out.println("1");
}else if (s.contains("2")) {
System.out.println("2");
}else{
throw new BizException("未匹配到数据");
}
}
现在的写法
@Test
public void test2() {
String s = "123";
Object o = BranchUtil.of(s)
.whenIf()
.match(str -> str.contains("1")).ifTrue(str -> System.out.println("1"))
.match(str -> str.contains("2")).ifTrue(str -> System.out.println("2"))
.orElseThrow(()->new BizException("未匹配到数据"));
System.out.println(o);
}
附上BranchUtil源码
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class BranchUtil<T> {
private T value;
private Supplier fastSupplier;
private static final BranchUtil<?> EMPTY = new BranchUtil<>();
public static <T> BranchUtil<T> of(T value) {
return new BranchUtil<>(value);
}
/**
* 构建快速 if else 语义
*
* @param value
* @return
*/
public static BranchUtil<Boolean> ofBoolean(Boolean value) {
return new BranchUtil<>(value);
}
public static <T> BranchUtil<T> of() {
return new BranchUtil<>();
}
private BranchUtil(T value) {
this();
this.value = value;
}
private BranchUtil() {
}
/**
*
*
* @return
*/
public Chain<T> chain() {
Chain<T> chain = new Chain<>();
chain.value = this.value;
return chain;
}
/**
* If else 模式
* if(true){
* return true;
* }else{
* return false;
* }
*
* @return
*/
public IfOrElse<T> whenIf() {
return new IfOrElse<>(value);
}
/**
*
*
* @param thenGet
* @return
*/
public BranchUtil<Boolean> ifTrue(Supplier thenGet) {
if ((Boolean) this.value) {
this.fastSupplier = thenGet;
}
return (BranchUtil<Boolean>) this;
}
public Object orElse(Supplier orElseSupplier) {
if (!(Boolean) this.value) {
return orElseSupplier.get();
}
return fastSupplier.get();
}
/**
* if(true){
* runnable.run();
* }
*
* @param runnable
*/
public void consumeTrue(Runnable runnable) {
if ((Boolean) this.value) {
runnable.run();
}
}
/**
* if(true){
* throw new RuntimeException
* }
*
*
* @param exceptionSupplier
* @param <X>
* @throws X
*/
public <X extends Throwable> void thenThrow(Supplier<? extends X> exceptionSupplier) throws X {
if ((Boolean) this.value) {
throw exceptionSupplier.get();
}
}
/**
*
*
* @param <T>
*/
public final class Chain<T> {
private Supplier<Boolean> condition;
private T value;
private boolean hadMatch;
public Chain<T> match(Predicate<T> predicate) {
this.condition = () -> predicate.test(value);
return this;
}
public Chain<T> ifTrue(Supplier resultSupplier) {
if (checkCondition()) {
hadMatch = true;
resultSupplier.get();
}
return this;
}
public Chain<T> ifTrue(Consumer<T> resultConsume) {
if (checkCondition()) {
resultConsume.accept(value);
}
return this;
}
public boolean checkCondition() {
return condition.get();
}
public void orElse(Supplier resultSupplier) {
if (!hadMatch) {
resultSupplier.get();
}
}
public void orElse(Consumer<T> resultConsumer) {
if (!hadMatch) {
resultConsumer.accept(value);
}
}
}
/**
* if else 模式
*
* @param <T>
*/
public static final class IfOrElse<T> {
private Supplier<Boolean> condition;
private boolean hadMatch;
private T value;
private Object result;
public IfOrElse(T value) {
this.value = value;
}
public IfOrElse<T> match(Predicate<T> predicate) {
this.condition = () -> predicate.test(value);
return this;
}
public IfOrElse<T> ifTrue(Supplier resultSupplier) {
//只允许匹配一次
if (!hadMatch && condition.get()) {
this.result = resultSupplier.get();
hadMatch = true;
}
return this;
}
public IfOrElse<T> ifTrue(Consumer<T> resultConsume) {
if (!hadMatch && condition.get()) {
resultConsume.accept(value);
hadMatch = true;
}
return this;
}
public <X extends Throwable> IfOrElse<T> orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if (result == null && !hadMatch) {
throw exceptionSupplier.get();
}
return this;
}
public Object orElseGet(Supplier resultSupplier) {
if (!hadMatch) {
return resultSupplier.get();
}
return this.result;
}
public void orElse(Consumer<T> resultConsumer) {
if (!hadMatch) {
resultConsumer.accept(value);
}
}
}
}