优化这段代码 String deviceCode1 = ""; String pointName1 = ""; if (Objects.equals(codePointDTO.getKey1(), "null") || Objects.equals(codePointDTO.getKey2(), "null") || StringUtils.isEmpty(codePointDTO.getKey1()) || StringUtils.isEmpty(codePointDTO.getKey2())) { deviceCode1 = obj.getDeviceCode(); pointName1 = codePointDTO.getPointName(); } else { deviceCode1 = codePointDTO.getKey1(); pointName1 = codePointDTO.getKey2(); }
时间: 2023-05-18 12:06:24 浏览: 123
可以将代码优化为:
String deviceCode1 = StringUtils.defaultIfBlank(codePointDTO.getKey1(), obj.getDeviceCode());
String pointName1 = StringUtils.defaultIfBlank(codePointDTO.getKey2(), codePointDTO.getPointName());
这样可以避免使用 Objects.equals 和 StringUtils.isEmpty,同时也更加简洁和易读。
阅读全文