java 使用Optional.ofNullable替代if的非空判断

例:

      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信息");
                }
            });

具体需要再研究研究,目前我没适应这种方式

你可能感兴趣的:(工作总结,java)