1

My AssignedPropertiesDTO class is:

@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. 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.

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.

[
    {
        "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.

1
  • Have you tried assigning the booleans with default values? It's worth noting that booleans default value is false. Also, use a boolean not a Boolean so that it can't support null. Commented Mar 31, 2020 at 20:23

3 Answers 3

3

@Builder makes an all-args constructor for you; it does not know about the non-default values required for isSoldOut and isPurged. You can use the @Builder.Default feature for this: @Builder.Default private Boolean isSoldOut = false; for example.

Alternatively just make them lowercase b boolean and 'false' is now the natural default. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Builder.Default private Boolean isSoldOut = false; This worked for me. Thanks
1

Have you tried assigning the booleans with default values? It's worth noting that booleans default value is 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'. That is generally left for the accessor.

@JsonProperty("sold_out")
private boolean soldOut;

2 Comments

After adding this, I am getting error for setSoldOut(isSoldeOut); to implement setSoldOut method.
Change sold_out to is_sold_out and variable to match
1

When you define isPurged and isSoldOut as boolean instead of as Boolean the variables become initialised as false by default (as per your constructor).

In case you need another initialisation, you can simply define it when declaring the variable, e.g. setting one to true, the other to false:

Data
@Builder
@AllArgsConstructor
public class AssignedPropertiesDTO {

  @JsonProperty("is_sold_out")
  private boolean isSoldOut = true;

  @JsonProperty("is_purged")
  private boolean isPurged = false;
}

1 Comment

And I was wondering, why this question was still unanswered. Lucky me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.