I have a java enum as
public enum Month {
JANUARY("01. January")
,FEBRUARY("02. February")
, MARCH("03. March")
, APRIL("04. April")
, MAY("05. May")
, JUNE("06. June")
, JULY("07. July")
, AUGUST("08. August")
, SEPTEMBER("09. September")
, OCTOBER("10. October")
, NOVEMBER("11. November")
, DECEMBER("12. December");
private final String displayName;
Month(final String display)
{
this.displayName = display;
}
@Override public String toString()
{
return this.displayName;
}
};
Also I have a DTO Object which is
public class SampleDTO {
private int variable1;
private Set<String> monthName;
public int getVariable1() {
return variable1;
}
public void setVariable1(int variable1) {
this.variable1 = variable1;
}
public Set<String> getMonthName() {
return monthName;
}
public void setMonthName(Set<String> monthName) {
this.monthName = monthName;
}
}
Last I have a method with input parameter as SampleDTO, Is there a way I can force that when a string is added to the monthName , it always comes from the valid values of the enum. for example adding "dfkjahdkjfadf" to the set should not be permitted
Change Set<String>
to Set<Month>
so that it can only contain Month
enum instances. Making sure it is a legal month seems to be part of your contract, so you have to enforce it inside SampleDTO
.
You might used Set<Month>
instead of Set<String>
, for example:
public class SampleDTO {
. ...
private Set<Month> month;
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.