Skip to content

Instantly share code, notes, and snippets.

@friddle
Created July 11, 2022 01:25
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/bd57392e11d199be2901432c0405ef0f to your computer and use it in GitHub Desktop.
Save friddle/bd57392e11d199be2901432c0405ef0f to your computer and use it in GitHub Desktop.
AlsoIf(Java函数式的判断后执行操作)
/**
* AlsoIf.alsoIf(xxx,xxx,xx).alsoIf().get()
* 进行长条件判断.如果条件成立,则执行相应的操作.主要用于LambadaQuery上
* java lambda for predicate and do things
*/
@Slf4j
public class BizAlsoIf<R> {
R obj;
private BizAlsoIf(R objj){
obj=objj;
}
public static<R> BizAlsoIf<R> alsoIf(R obj, boolean isIf, Consumer<R> consumer){
BizAlsoIf<R> r=new BizAlsoIf<R>(obj);
return r.alsoIf(isIf,consumer);
}
public BizAlsoIf<R> alsoIf(boolean isIf, Consumer<R> consumer){
try {
if(isIf){
consumer.accept(obj);
}
return this;
} catch (Exception e) {
log.error("try method failed", e);
throw e;
}
}
public R get(){
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment