- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的方法,用于创建 POJO 并使用所述 POJO 的字段填充父对象
public void createMyPOJO(
ParentObject parentObject,
Boolean i,
Boolean j,
Boolean k) {
if (i != null || j != null || k != null) {
MyPojo myPojo = new MyPojo();
if (i != null) myPojo.setIndicatorI(i);
if (j != null) myPojo.setIndicatorJ(j);
if (k != null) myPojo.setIndicatorK(k);
parentObject.setMyPojo(myPojo);
}
}
使用可选类型或 @Nullable 只会让这个变得困惑,并且使用像 Boolean[] 这样的数据类型也感觉不到太大的改进。什么是更惯用或更优雅的方法来避免基本上每一行都进行空检查?
最佳答案
您可以使用 boolean
而不是 Boolean
并让 autoboxing为您完成这项工作(在调用 createMyPOJO(...)
时使用 false
而不是 null
)。这也删除了空检查验证。
public void createMyPOJO(
ParentObject parentObject,
boolean i,
boolean j,
boolean k) {
if (i || j || k) {
MyPojo myPojo = new MyPojo();
if(i) myPojo.setIndicatorI(i);
if(j) myPojo.setIndicatorJ(j);
if(k) myPojo.setIndicatorK(k);
parentObject.setMyPojo(myPojo);
}
}
您可以选择添加 builder到 MyPojo
类并使用它来构建 MyPojo
的新实例。
编辑:
随着需求的增长,每个指标都应该支持 null
、true
和 false
值,我认为最好的解决方案是使用构建器。
public class MyPojoBuilder {
private Boolean indicatorI;
private Boolean indicatorJ;
private Boolean indicatorK;
private ParentObject parentObject;
private MyPojoBuilder(ParentObject parentObject) {
this.parentObject = parentObject;
}
public static MyPojoBuilder withParentObject(ParentObject parentObject) {
return new MyPojoBuilder(parentObject);
}
public MyPojoBuilder withIndicatorI(Boolean value) {
indicatorI = value;
return this;
}
public MyPojoBuilder withIndicatorJ(Boolean value) {
indicatorJ = value;
return this;
}
public MyPojoBuilder withIndicatorK(Boolean value) {
indicatorK = value;
return this;
}
public void build() {
if (indicatorI == null && indicatorJ == null && indicatorK == null) {
// Throw some exception instead???
return;
}
if (parentObject == null) {
throw new IllegalStateException("ParentObject cannot be null");
}
MyPojo myPojo = new MyPojo();
myPojo.setIndicatorI(indicatorI);
myPojo.setIndicatorJ(indicatorJ);
myPojo.setIndicatorK(indicatorK);
parentObject.setMyPojo(myPojo);
}
}
如果需要,可以扩展构建器以创建并返回新的 ParentObject
实例。
使用示例
ParentObject parentObjectWithIndicatorIAndK = new ParentObject();
MyPojoBuilder.withParentObject(parentObjectWithIndicatorIAndK)
.withIndicatorI(true)
.withIndicatorK(false)
.build();
System.out.println(parentObjectWithIndicatorIAndK);
输出:
ParentObject{myPojo=MyPojo{indicatorI=true, indicatorJ=null, indicatorK=false}}
这是一个demo project实现这一概念。
关于java - 对方法的多个参数进行 null 检查的优雅或惯用的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509357/
我对 Clojure 和函数式编程有了大约一周的了解——我的所有背景都是 OOP。我想利用 Clojure 备受争议的易读性和固有逻辑,但现在我不知道我是否成功地做到了这一点,只是没有完全理解它,或者
场景: val col: IndexedSeq[Array[Char]] = for (i = 0 && arr(last.y)(west) == '.') { arr(last.y)(w
我正面临 AngularJS、服务和范围的“问题”。 这不是一个真正的问题(我找到了几种使其工作的方法),但我想知道我是否在做正确的事情,或者我正在做的事情是否会导致将来出现问题 我有一个保存一些全局
进行以下数据结构转换的“Rubyist”方法是什么: 我有 incoming = [ {:date => 20090501, :width => 2}, {:
如何在 go 中编写返回集合最小值的函数?我不只是在寻找解决方案(我知道我可以在遍历第一个元素时只初始化最小值,然后设置一个我初始化最小值的 bool 变量),而是一个惯用的解决方案。由于 go 没有
好的,我知道我应该对我的特定应用程序进行基准测试,等等,但是: -Xmx 的默认 JVM 设置、默认垃圾收集器等,对于大多数典型的 Java 程序来说是合理的默认设置,并且可能不适合惯用的 Scala
既然 shared_ptr 在 tr1 中,你认为 std::auto_ptr 的使用会发生什么?它们都有不同的用例,但 auto_ptr 的所有用例也都可以用 shared_ptr 解决。你会放弃
这个问题在这里已经有了答案: What are the differences between type() and isinstance()? (8 个回答) 关闭 9 年前。 我需要知道 Pyth
在指定和创建数字函数时,是否有关于何时返回 null 以及何时返回 NaN 的任何 C# 惯用准则,当两者似乎都是有效输出时。 导致这个问题的具体例子是我正在为 Enumerable 集合创建一个百分
这个问题在这里已经有了答案: Retrieving the top 100 numbers from one hundred million of numbers [duplicate] (12 个
我可以通过反射检索方法,以某种方式将其与目标对象结合起来,并将其作为看起来像 Scala 中的函数的东西返回(即您可以使用括号调用它)吗?参数列表是可变的。它不一定是“一流”函数(我已经更新了问题),
我是一名优秀的程序员,十分优秀!