Function接口
抛出异常处理
/*** @Classname ThrowExceptionFunction* @Author 6b92d6* @Description 抛异常接口*/@FunctionalInterfacepublic interface ThrowExceptionFunction {/*** 抛出异常信息* @param message 异常信息* @return void*/void throwException(String message);}
进行分支操作
/*** @Classname BranchHandle* @Author 6b92d6* @Description 分支处理接口*/@FunctionalInterfacepublic interface BranchHandle {/*** 分支操作* @param trueHandle 为true时要进行操作* @param falseHandle 为false时要进行操作* return void*/void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);}
空值与非空值
import java.util.function.Consumer;/*** @Classname PresentOrElseHandler* @Author 6b92d6* @Description 空值与非空值分支处理*/public interface PresentOrElseHandler<T extends Object> {/*** 值不为空时执行消费操作* 值为空时执行其他的操作** @param action 值不为空时,执行的消费操作* @param emptyAction 值不为空时,执行的消费操作*/void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);}
VUtils工具类
/*** @Classname VUtils* @Author 6b92d6* @Description 工具类*/public class VUtils {/*** 如果参数为true抛出异常* @param b* @return*/public static ThrowExceptionFunction isTrue(boolean b) {return (errorMessage) -> {if (b) {throw new RuntimeException(errorMessage);}};}/*** 参数为true或false时,分别进行不同的操作* @param b* @return BranchHandle*/public static BranchHandle isTureOrFalse(boolean b) {return (trueHandle, falseHandle) -> {if (b) {trueHandle.run();} else {falseHandle.run();}};}/*** 存在参数值** @param str* @return*/public static PresentOrElseHandler<?> isBlankOrNoBlank(String str) {return (consumer,runable) -> {if (str == null || str.length() == 0) {runable.run();} else {consumer.accept(str);}};}}
测试
import org.junit.Test;/*** @Classname TestOne* @Author 6b92d6* @Description*/public class TestOne {/*** 当出入的参数为true时抛出异常*/@Testpublic void isTrue(){VUtils.isTrue(true).throwException("👴要抛出异常了");}/*** 如果存在值执行消费操作,否则执行基于空的操作*/@Testpublic void isTrueOrFalse() {VUtils.isTureOrFalse(false).trueOrFalseHandle(() -> {System.out.println("true,👴要秀了");},() -> {System.out.println("false,👴不行");});}@Testpublic void isBlankOrNoBlank() {VUtils.isBlankOrNoBlank("hello").presentOrElseHandle(System.out::println, () -> {System.out.println("空字符串");});}}
