If I have a Java record with 2 properties and I want to define default values for the properties which should be used instead of null. I can either override the getters
public record MyRecord(
Set<String> strings,
Boolean required) {
@Override
public Boolean required() {
return Objects.requireNonNullElse(this.required, Boolean.TRUE);
}
@Override
public Set<String> strings() {
return Objects.requireNonNullElse(this.strings, Set.of());
}
}
Or I can achieve much the same thing by overriding the default constructor
public record MyRecord(
Set<String> strings,
Boolean required) {
public MyRecord(Set<String> strings, Boolean required) {
this.strings = Objects.requireNonNullElse(strings, Set.of());
this.required = Objects.requireNonNullElse(required, Boolean.TRUE);
}
}
Both of these seem a bit verbose, is there a more concise way to assign default values to record properties?