使用Lombok构建Phone对象。如何跳过设置属性,例如";name";使用Builder模式,如果由于某种原因它将为空
protected Phone createPhone(String number, String name) {
return Phone.builder()
.number(number)
.name(name)
.type(PhoneType.MOBILE)
.build();
}
可以"覆盖";Lombok中的生成器方法。例如,您可以这样做(简化的Phone
(:
@Builder
@Getter @Setter
public class Phone {
private String number;
private String name;
public static class PhoneBuilder {
// Lombok does not generate name "setter" but uses this instead
public PhoneBuilder name(String name) {
if (null == name) {
// throw or ignore or whatever
throw new NullPointerException("Name NULL");
}
this.name = name;
return this;
}
}
}