一坨代码的痛定思痛,我的眼睛真的近视 。(反思二)

原代码:

    /**
     * 获取界面传递的id集合
     * @param idLists
     * @return
     */
    private BomIdListDTO queryIdList(String idLists) {
        BomIdListDTO query = new BomIdListDTO();
        if (null != idLists && StringUtils.isNotBlank(idLists.toString())) {
            String idListStr = idLists;
            if (StringUtils.isNotBlank(idListStr)) {
                // 集合
                String[] temp2 = idListStr.split(",");
                if (StringUtils.isNotBlank(idListStr)) {
                    List<Long> idList = new ArrayList<>();

                    for (int i = 0; i < temp2.length; i++) {
                        String str = temp2[i].substring(0, 1);
                        String str2 = temp2[i].substring(temp2[i].length() - 1);
                        if ("[".equals(str)) {
                            temp2[i] = temp2[i].substring(1, temp2[i].length() - 1);
                            idList.add(Long.valueOf(temp2[i].trim()));
                        } else if ("]".equals(str2)) {
                            temp2[i] = temp2[i].substring(0, temp2[i].length() - 1);
                            idList.add(Long.valueOf(temp2[i].trim()));
                        } else {
                            idList.add(Long.valueOf(temp2[i].trim()));
                        }
                    }
                    query.setIdList(idList);
                }
            }
        }

        return query;
    }

  优化后一:

       

/**
 * 获取界面传递的 id 集合
 * 支持格式:
 *   1,2,3
 *   [1,2,3]
 *   [ 1 , 2 , 3 ]
 */
private BomIdListDTO queryIdList(String idLists) {

    BomIdListDTO query = new BomIdListDTO();

    if (StringUtils.isBlank(idLists)) {
        return query;
    }

    // 去掉首尾中括号(如果有)
    String cleaned = idLists.trim();
    if (cleaned.startsWith("[")) {
        cleaned = cleaned.substring(1);
    }
    if (cleaned.endsWith("]")) {
        cleaned = cleaned.substring(0, cleaned.length() - 1);
    }

    if (StringUtils.isBlank(cleaned)) {
        return query;
    }

    List<Long> idList = new ArrayList<>();

    for (String part : cleaned.split(",")) {
        if (StringUtils.isBlank(part)) {
            continue;
        }
        try {
            idList.add(Long.valueOf(part.trim()));
        } catch (NumberFormatException e) {
            // 非法 ID,直接忽略(可按需要记录日志)
        }
    }

    query.setIdList(idList);
    return query;
}

  优化后二:

private BomIdListDTO queryIdList(String idLists) {

    BomIdListDTO query = new BomIdListDTO();

    if (StringUtils.isBlank(idLists)) {
        return query;
    }

    String cleaned = idLists
            .replace(",", ",")
            .replace("[", "")
            .replace("]", "")
            .trim();

    if (StringUtils.isBlank(cleaned)) {
        return query;
    }

    List<Long> idList = Arrays.stream(cleaned.split(","))
            .map(String::trim)
            .filter(StringUtils::isNotBlank)
            .map(s -> {
                try {
                    return Long.valueOf(s);
                } catch (NumberFormatException e) {
                    return null;
                }
            })
            .filter(Objects::nonNull)
            .distinct()
            .collect(Collectors.toList());

    query.setIdList(idList);
    return query;
}

  

 

   

posted @ 2025-12-28 10:30  升鲜宝供应链管理系统  阅读(5)  评论(0)    收藏  举报