Skip to content

Instantly share code, notes, and snippets.

@friddle
Last active June 29, 2022 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friddle/bb33390f4070b01540e8d7a0a6d180fb to your computer and use it in GitHub Desktop.
Save friddle/bb33390f4070b01540e8d7a0a6d180fb to your computer and use it in GitHub Desktop.
Java Lambda Wrapper for Exception Java的Lambda一些Wrapper。模拟kt操作
@Slf4j
public class BizUtils {
public static<R> R invoke(Function<Void,R> function, R defaultValue){
try {
return function.apply(null);
} catch (Exception e) {
log.error("try method failed", e);
return defaultValue;
}
}
public static String invokeS(Function<Void,String> function, String defaultValue){
var result=invoke(function,defaultValue);
if(result.isEmpty()){return defaultValue;}
return result;
}
public static<T,R> R ignoreError(Function<T,R> function, T args)
{
try {
return function.apply(args);
} catch (Exception e) {
log.error("try method failed", e);
return null;
}
}
}
//File file=BizUtils.invoke(item->new File("xxxx"),aaaa);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment