0

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

2
  • private Set<Month> monthName; Commented Aug 6, 2015 at 16:41
  • Why you're not using Set<Month>? And try Month.valueOf(String). Commented Aug 6, 2015 at 16:42

2 Answers 2

2

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.

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

Comments

1

You might used Set<Month> instead of Set<String>, for example:

public class SampleDTO {
.    ...
     private Set<Month> month;
     ...
} 

Comments

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.