[英]How to use lombok with default constructor
My AssignedPropertiesDTO class is:我的 AssignedPropertiesDTO 类是:
@Data
@Builder
@AllArgsConstructor
public class AssignedPropertiesDTO {
@JsonProperty("hotel_id")
private Long hotelId;
@JsonProperty("oyo_id")
private String oyoId;
@JsonProperty("drn")
private Integer dsrn;
@JsonProperty("is_sold_out")
private Boolean isSoldOut;
@JsonProperty("is_purged")
private Boolean isPurged;
AssignedPropertiesDTO() {
this.isSoldOut = false;
this.isPurged = false;
}
}
I need to set isSoldOut and isPurged to false.我需要将 isSoldOut 和 isPurged 设置为 false。 That is why i have made default constructor.
这就是我创建默认构造函数的原因。 But i am using Builder() for setting class fields and somewhere just setting properties using getter/setter.
但是我使用 Builder() 来设置类字段,而在某处只是使用 getter/setter 设置属性。
AssignedPropertiesDTO matchingObject = assignedPropertiesDTOS.stream()
.filter(assignedPropertiesDTO ->
assignedPropertiesDTO.getHotelId().equals(Long.valueOf(entry.getKey())))
.findFirst().orElse(null);
if (matchingObject == null) {
assignedPropertiesDTOS.add(AssignedPropertiesDTO
.builder().hotelId(Long.valueOf(entry.getKey())).dsrn(count).build());
} else {
matchingObject.setDsrn(count);
}
My requirement is to set Ispurged/IsSoldOut to either True/False But not NULL.我的要求是将 Ispurged/IsSoldOut 设置为 True/False 但不是 NULL。
[ {
"hotel_id": 45693,
"oyo_id": "GOA2161",
"drn": null,
"is_sold_out": null,
"is_purged": null
},
{
"hotel_id": 45693,
"oyo_id": "GOA2161",
"drn": null,
"is_sold_out": true,
"is_purged": false
} ]
Please guide me how can i do this.请指导我如何做到这一点。
@Builder
makes an all-args constructor for you; @Builder
为您制作了一个全参数构造函数; it does not know about the non-default values required for isSoldOut and isPurged.它不知道 isSoldOut 和 isPurged 所需的非默认值。 You can use the
@Builder.Default
feature for this: @Builder.Default private Boolean isSoldOut = false;
您可以
@Builder.Default
使用@Builder.Default
功能: @Builder.Default private Boolean isSoldOut = false;
for example.例如。
Alternatively just make them lowercase b boolean and 'false' is now the natural default.或者,只需将它们设为小写 b 布尔值,'false' 现在是自然默认值。 If that's an option at all, it's by far the best solution here.
如果这是一个选项,那么它是迄今为止最好的解决方案。
Your final option is to make the all-args constructor yourself.您的最后一个选择是自己制作全参数构造函数。
When you define isPurged
and isSoldOut
as boolean
instead of as Boolean
the variables become initialised as false
by default (as per your constructor).当您将
isPurged
和isSoldOut
定义为boolean
而不是Boolean
,默认情况下变量将初始化为false
(根据您的构造函数)。
In case you need another initialisation, you can simply define it when declaring the variable, eg setting one to true, the other to false:如果您需要另一个初始化,您可以在声明变量时简单地定义它,例如将一个设置为 true,另一个设置为 false:
Data
@Builder
@AllArgsConstructor
public class AssignedPropertiesDTO {
@JsonProperty("is_sold_out")
private boolean isSoldOut = true;
@JsonProperty("is_purged")
private boolean isPurged = false;
}
Have you tried assigning the booleans with default values?您是否尝试使用默认值分配布尔值? It's worth noting that booleans default value is false.
值得注意的是,布尔值默认值为 false。 Also, use a boolean not a Boolean so that it can't support null.
另外,使用布尔值而不是布尔值,这样它就不能支持空值。
On an unrelated note you don't usually give booleans names starting with 'is'.在一个不相关的注释中,您通常不会给出以“is”开头的布尔值名称。 That is generally left for the accessor.
这通常留给访问者。
@JsonProperty("sold_out")
private boolean soldOut;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.