java 使用Optional.ofNullable替代if的非空判断
原创
©著作权归作者所有:来自51CTO博客作者yOhohoh的原创作品,请联系作者获取转载授权,否则将追究法律责任
例:
String namespace = Optional.ofNullable(client.getNamespace()).orElse("default");
例:
Optional.ofNullable(webhookSettings).ifPresent(webhookSetting -> {
Map map = gson.fromJson(gson.toJson(entity), Map.class);
QueryWrapper<Webhook> webhookQw = new QueryWrapper<>();
webhookQw.eq("app_id", appId);
webhookQw.eq("whether_enabled",IS_ENABLED);
List<Webhook> list = grayScaleListener.webhookService.list(webhookQw);
if(!CollectionUtils.isEmpty(list)){
boolean doWebhookUrlRequest = grayScaleListener.webhookService.doWebhookUrlRequest(list.get(0), map);
if(true == doWebhookUrlRequest){
log.info("已发送向指定webhook url转发消息!");
}
}else{
log.info("该平台下未能找到匹配的webhook信息");
}
});
具体需要再研究研究,目前我没适应这种方式