反向操作

今天修改个东西,看到有一段代码被人优化了!!!

优化前

  public Optional>> getByCodes(String type, List codeList) {
        if (codeList.size() <= DEFAULT_PAGE_SIZE) {
            return getByBatch(type, codeList);
        }

        return IntStream.range(0, (codeList.size() + DEFAULT_PAGE_SIZE - 1) / DEFAULT_PAGE_SIZE)
                .mapToObj(i -> codeList.subList(i * DEFAULT_PAGE_SIZE, Math.min(DEFAULT_PAGE_SIZE * (i + 1), codeList.size())))
                .map(list -> getByBatch(type, list).orElse(new ArrayList<>()))
                .reduce((a, b) -> {
                    a.addAll(b);
                    return a;
                });

    }    

private Optional>> getByBatch(String type, List codeList) {}

优化后

    public Optional>> getByCodes(String type, List codeList) {
        Optional>> result = null;
        if (codeList.size() <= DEFAULT_PAGE_SIZE) {
            result = getByBatch(type, codeList);

            if(result.isPresent() && CollectionUtils.isNotEmpty(result.get())){
                return result;
            }
            return Optional.empty();
        }

        result = IntStream.range(0, (codeList.size() + DEFAULT_PAGE_SIZE - 1) / DEFAULT_PAGE_SIZE)
                .mapToObj(i -> codeList.subList(i * DEFAULT_PAGE_SIZE, Math.min(DEFAULT_PAGE_SIZE * (i + 1), codeList.size())))
                .map(list -> getByBatch(type, list).orElse(new ArrayList<>()))
                .reduce((a, b) -> {
                    a.addAll(b);
                    return a;
                });
        if(result.isPresent() && CollectionUtils.isNotEmpty(result.get())){
            return result;
        }
        return Optional.empty();
    }

 private Optional>> getByBatch(String type, List codeList) {}

你可能感兴趣的:(反向操作)