Similar to this question. I have an interface DateRangeModel
:
I use this to automatically validate dates in implementers:
public interface DateRangeModel {
@ApiModelProperty(value = "From date. Must be less than to date.")
@Column()
Date getFromDate();
void setFromDate(Date date);
@ApiModelProperty(value = "To date. Must be greater than to date.")
@Column()
Date getToDate();
void setToDate(Date date);
/**
* Checks that if both dates are populated, a valid date range is used.
*
* @return true if the date is a valid range.
*/
@AssertTrue(message = "To date must be greater than or equal to from date.")
@JsonIgnore
default boolean areDatesValid() {
if (getToDate() != null && getFromDate() != null) {
return !getFromDate().after(getToDate());
}
return true;
}
}
I implement it like this:
@EqualsAndHashCode
@Data
@Builder
public class BirthdayParty implements DateRangeModel {
Date fromDate;
Date toDate;
String name;
}
Which compiles and seems to work, but I get that error when running PMD:
Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object.
How can I either accomplish what I want (an interface with to/from date validation) without having to implement the setDate methods in all implementers (which I think would defeat the purpose)?