妙用Java 8中的 Function接口 消滅if...else(非常新穎的寫(xiě)法)
在開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)使用if...else...進(jìn)行判斷拋出異常、分支處理等操作。這些if...else...充斥在代碼中嚴(yán)重影響了代碼代碼的美觀,這時(shí)我們可以利用Java 8的Function接口來(lái)消滅if...else...。
- if (...){
- throw new RuntimeException("出現(xiàn)異常了");
- }
- if (...){
- doSomething();
- } else {
- doOther();
- }
Function 函數(shù)式接口
使用注解@FunctionalInterface標(biāo)識(shí),并且只包含一個(gè)抽象方法的接口是函數(shù)式接口。函數(shù)式接口主要分為Supplier供給型函數(shù)、Consumer消費(fèi)型函數(shù)、Runnable無(wú)參無(wú)返回型函數(shù)和Function有參有返回型函數(shù)。
Function可以看作轉(zhuǎn)換型函數(shù)
Supplier供給型函數(shù)
Supplier的表現(xiàn)形式為不接受參數(shù)、只返回?cái)?shù)據(jù)
Consumer消費(fèi)型函數(shù)
Consumer消費(fèi)型函數(shù)和Supplier剛好相反。Consumer接收一個(gè)參數(shù),沒(méi)有返回值
Runnable無(wú)參無(wú)返回型函數(shù)
Runnable的表現(xiàn)形式為即沒(méi)有參數(shù)也沒(méi)有返回值
Function函數(shù)的表現(xiàn)形式為接收一個(gè)參數(shù),并返回一個(gè)值。Supplier、Consumer和Runnable可以看作Function的一種特殊表現(xiàn)形式
使用小技巧
處理拋出異常的if
1.定義函數(shù)
定義一個(gè)拋出異常的形式的函數(shù)式接口, 這個(gè)接口只有參數(shù)沒(méi)有返回值是個(gè)消費(fèi)型接口
- /**
- * 拋異常接口
- **/
- @FunctionalInterface
- public interface ThrowExceptionFunction {
- /**
- * 拋出異常信息
- *
- * @param message 異常信息
- * @return void
- **/
- void throwMessage(String message);
- }
2.編寫(xiě)判斷方法
創(chuàng)建工具類(lèi)VUtils并創(chuàng)建一個(gè)isTure方法,方法的返回值為剛才定義的函數(shù)式接口-ThrowExceptionFunction。ThrowExceptionFunction的接口實(shí)現(xiàn)邏輯為當(dāng)參數(shù)b為true時(shí)拋出異常
- /**
- * 如果參數(shù)為true拋出異常
- *
- * @param b
- * @return com.example.demo.func.ThrowExceptionFunction
- **/
- public static ThrowExceptionFunction isTure(boolean b){
- return (errorMessage) -> {
- if (b){
- throw new RuntimeException(errorMessage);
- }
- };
- }
3.使用方式
調(diào)用工具類(lèi)參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的throwMessage方法傳入異常信息。當(dāng)出入的參數(shù)為false時(shí)正常執(zhí)行
當(dāng)出入的參數(shù)為true時(shí)拋出異常
處理if分支操作
1.定義函數(shù)式接口
創(chuàng)建一個(gè)名為BranchHandle的函數(shù)式接口,接口的參數(shù)為兩個(gè)Runnable接口。這兩個(gè)兩個(gè)Runnable接口分別代表了為true或false時(shí)要進(jìn)行的操作
- /**
- * 分支處理接口
- **/
- @FunctionalInterface
- public interface BranchHandle {
- /**
- * 分支操作
- *
- * @param trueHandle 為true時(shí)要進(jìn)行的操作
- * @param falseHandle 為false時(shí)要進(jìn)行的操作
- * @return void
- **/
- void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);
- }
2.編寫(xiě)判斷方法
創(chuàng)建一個(gè)名為isTureOrFalse的方法,方法的返回值為剛才定義的函數(shù)式接口-BranchHandle。
- /**
- * 參數(shù)為true或false時(shí),分別進(jìn)行不同的操作
- *
- * @param b
- * @return com.example.demo.func.BranchHandle
- **/
- public static BranchHandle isTureOrFalse(boolean b){
- return (trueHandle, falseHandle) -> {
- if (b){
- trueHandle.run();
- } else {
- falseHandle.run();
- }
- };
- }
3.使用方式
參數(shù)為true時(shí),執(zhí)行trueHandle
參數(shù)為false時(shí),執(zhí)行falseHandle
如果存在值執(zhí)行消費(fèi)操作,否則執(zhí)行基于空的操作
1.定義函數(shù)
創(chuàng)建一個(gè)名為PresentOrElseHandler的函數(shù)式接口,接口的參數(shù)一個(gè)為Consumer接口。一個(gè)為Runnable,分別代表值不為空時(shí)執(zhí)行消費(fèi)操作和值為空時(shí)執(zhí)行的其他操作
- /**
- * 空值與非空值分支處理
- */
- public interface PresentOrElseHandler<T extends Object> {
- /**
- * 值不為空時(shí)執(zhí)行消費(fèi)操作
- * 值為空時(shí)執(zhí)行其他的操作
- *
- * @param action 值不為空時(shí),執(zhí)行的消費(fèi)操作
- * @param emptyAction 值為空時(shí),執(zhí)行的操作
- * @return void
- **/
- void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction);
- }
2.編寫(xiě)判斷方法
創(chuàng)建一個(gè)名為isBlankOrNoBlank的方法,方法的返回值為剛才定義的函數(shù)式接口-PresentOrElseHandler。
- /**
- * 參數(shù)為true或false時(shí),分別進(jìn)行不同的操作
- *
- * @param b
- * @return com.example.demo.func.BranchHandle
- **/
- public static PresentOrElseHandler<?> isBlankOrNoBlank(String str){
- return (consumer, runnable) -> {
- if (str == null || str.length() == 0){
- runnable.run();
- } else {
- consumer.accept(str);
- }
- };
- }
3.使用方式
調(diào)用工具類(lèi)參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的presentOrElseHandle方法傳入一個(gè)Consumer和Runnable
參數(shù)不為空時(shí),打印參數(shù)
參數(shù)不為空時(shí)
結(jié)尾
Function函數(shù)式接口是java 8非常重要的特性,利用好Function函數(shù)可以極大的簡(jiǎn)化代碼。