You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更优雅地将int数组传入重载的BWsAnnotation.make方法?

Java数组适配重载方法的代码优化建议

原代码存在重复逻辑冗余、未处理null和非法数组长度的问题,以下是几种优化方案:

方案一:提取重复逻辑,简化分支判断

将重复的child.add操作抽离,只在分支中构建BWsAnnotation实例,减少代码重复,同时补充边界校验:

public static void customAdd(String name, BComponent parent, BComponent child, int[] locations, String[] link) {
    parent.add(name, child);

    if (locations == null) {
        return;
    }

    BWsAnnotation annotation = null;
    switch (locations.length) {
        case 2:
            annotation = BWsAnnotation.make(locations[0], locations[1]);
            break;
        case 3:
            annotation = BWsAnnotation.make(locations[0], locations[1], locations[2]);
            break;
        case 4:
            annotation = BWsAnnotation.make(locations[0], locations[1], locations[2], locations[3]);
            break;
        default:
            // 可根据需求添加日志或抛异常,避免静默失败
            // throw new IllegalArgumentException("locations数组长度必须为2、3或4");
            break;
    }

    if (annotation != null) {
        child.add("WsAnnotation", annotation);
    }
}

方案二:新增辅助重载方法(推荐)

给BWsAnnotation新增一个接收int[]的静态make方法,把长度判断逻辑封装进去,让主方法更简洁:

// 在BWsAnnotation类中新增静态方法
public static BWsAnnotation make(int[] locations) {
    if (locations == null) {
        throw new IllegalArgumentException("locations数组不能为null");
    }
    switch (locations.length) {
        case 2:
            return make(locations[0], locations[1]);
        case 3:
            return make(locations[0], locations[1], locations[2]);
        case 4:
            return make(locations[0], locations[1], locations[2], locations[3]);
        default:
            throw new IllegalArgumentException("locations数组长度必须为2、3或4");
    }
}

// 原customAdd方法简化为
public static void customAdd(String name, BComponent parent, BComponent child, int[] locations, String[] link) {
    parent.add(name, child);

    if (locations != null && locations.length >= 2 && locations.length <= 4) {
        child.add("WsAnnotation", BWsAnnotation.make(locations));
    }
}

方案三:用条件判断替代switch(风格选择)

如果偏好更紧凑的写法,可将switch替换为if-else链,逻辑保持一致:

public static void customAdd(String name, BComponent parent, BComponent child, int[] locations, String[] link) {
    parent.add(name, child);

    if (locations == null) {
        return;
    }

    BWsAnnotation annotation = null;
    int len = locations.length;
    if (len == 2) {
        annotation = BWsAnnotation.make(locations[0], locations[1]);
    } else if (len == 3) {
        annotation = BWsAnnotation.make(locations[0], locations[1], locations[2]);
    } else if (len == 4) {
        annotation = BWsAnnotation.make(locations[0], locations[1], locations[2], locations[3]);
    } else {
        // 处理非法长度
    }

    if (annotation != null) {
        child.add("WsAnnotation", annotation);
    }
}

额外优化细节

  • 补充null校验:原代码直接调用locations.length可能触发NullPointerException,需先判断数组是否为null
  • 处理非法长度:原代码对长度为1或大于4的情况直接忽略,建议添加日志或抛出异常,避免隐性错误
  • 简化注释:冗余注释(如//add object)可删除,代码逻辑清晰时无需额外说明

内容的提问来源于stack exchange,提问作者Peter Reeves CplRabbit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:35:12