segment4java8

    public static ServiceInfoBo getServicesInfoByCargo(String plusName, String swimlane) {
        ServiceInfoBo finalServiceInfo = null;
        try {
            String queryResult = HttpUtil.get(CARGO_QUERY_URL, Collections.singletonMap("query", plusName));
            checkCargoQueryResult(queryResult);
            JSONPath.extract(queryResult, "$.data.runner");
            List<ServiceInfoBo> serviceInfoBos = JSONArray.parseArray(JSONPath.extract(queryResult, "$.data.runner").toString(), ServiceInfoBo.class);
            serviceInfoBos.removeIf(serviceInfoBo -> !"hulk-test".equals(serviceInfoBo.getArea()));

            //before java8
//            Map<String, ServiceInfoBo> swimlaneServiceInfoMap = Maps.newHashMap();
//            for (ServiceInfoBo bo : serviceInfoBos) {
//                if (null != swimlaneServiceInfoMap.get(bo.getSwimlane())) {
//                    continue;
//                }
//                swimlaneServiceInfoMap.put(bo.getSwimlane(), bo);
//            }
            //java8
            Map<String, ServiceInfoBo> serviceInfoBoMap = serviceInfoBos.stream().collect(groupingBy(ServiceInfoBo::getSwimlane,
                    collectingAndThen(mapping(bo -> bo, toList()),
                            list -> list.stream().findFirst().get())));

            ServiceInfoBo serviceInfoBo = serviceInfoBoMap.get("");
            if (Strings.isNullOrEmpty(swimlane) || "null".equals(swimlane)) {
                return serviceInfoBo;
            }

            finalServiceInfo = Optional.ofNullable(serviceInfoBoMap.get(swimlane)).orElseGet(() -> {
                log.warn("查詢的泳道不存在,默認返回主幹環境服務信息:{}", JSON.toJSONString(serviceInfoBo));
                return serviceInfoBo;
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        return finalServiceInfo;

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章