Java8中一個(gè)極其強(qiáng)悍的新接口,非常實(shí)用
在開(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ù)式接口
Function可以看作轉(zhuǎn)換型函數(shù)
Supplier供給型函數(shù)
Supplier的表現(xiàn)形式為不接受參數(shù)、只返回?cái)?shù)據(jù)
圖片
Consumer消費(fèi)型函數(shù)
圖片
Runnable無(wú)參無(wú)返回型函數(shù)
圖片
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.編寫判斷方法
創(chuàng)建工具類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)用工具類參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的throwMessage方法傳入異常信息。當(dāng)出入的參數(shù)為false時(shí)正常執(zhí)行
圖片
當(dāng)出入的參數(shù)為true時(shí)拋出異常
圖片
處理if分支操作
1、定義函數(shù)式接口
/**
* 分支處理接口
**/
@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.編寫判斷方法
創(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。
圖片
1、定義函數(shù)
/**
* 空值與非空值分支處理
*/
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.編寫判斷方法
創(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)用工具類參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的presentOrElseHandle方法傳入一個(gè)Consumer和Runnable。
參數(shù)不為空時(shí),打印參數(shù)。
圖片
參數(shù)不為空時(shí)。
圖片