[JAVA] switch and EnumMap

switch

The switch can take not only byte, short, int type values and String type values but also enumeration types in expressions, so it is convenient when you want to make a conditional branch quickly.

However, when that claim becomes fierce ...

DayOfWeek enumeration


enum DayOfWeek {
    SUNDAY    ("Sunday"),
    MONDAY    ("Monday"),
    TUESDAY   ("Tuesday"),
    WEDNESDAY ("Wednesday"),
    THURSDAY  ("Thursday"),
    FRIDAY    ("Friday"),
    SATURDAY  ("Saturday");

    private String name;

    private DayOfWeek(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
};

Person class


class Person {
    private DayOfWeek dayOfWeek;
    private String lyrics;

    private Person(DayOfWeek dayOfWeek) {
        setDayOfWeek(dayOfWeek);
    }

    //Static method to create an instance
    static Person todayIs(DayOfWeek dayOfWeek) {
        return new Person(dayOfWeek).doSometing();
    }
    
    //Method to set the day of the week
    Person setDayOfWeek(DayOfWeek dayOfWeek) {
        Objects.requireNonNull(dayOfWeek, "Go home");
        this.dayOfWeek = dayOfWeek;
        return this;
    }

    //Method to do something
    Person doSometing() {
        lyrics = dayOfWeek.getName();
        switch (dayOfWeek) {
            case SUNDAY:
                lyrics += "Go to the market";
                break;
            case MONDAY:
                lyrics += "Hit the bath";
                break;
            case TUESDAY:
                lyrics += "Enter the bath";
                break;
            case WEDNESDAY:
                lyrics += "My friends are coming";
                break;
            case THURSDAY:
                lyrics += "Sent";
                break;
            case FRIDAY:
                lyrics += "Without threading";
                break;
            case SATURDAY:
                lyrics += "Is just talking";
                break;
        }
        return this;
    }

    //Gambling method
    Person gambling(int amount) {
        lyrics = dayOfWeek.getName();
        switch (dayOfWeek) {
            case SUNDAY:
            case TUESDAY:
            case THURSDAY:
            case FRIDAY:
            case SATURDAY:
                lyrics += "To" + amount + "Lose";
                break;
            case MONDAY:
            case WEDNESDAY:
                lyrics += "To" + amount + "Win a million";
                break;
        }
        return this;
    }

    //Tuller method
    Person turya() {
        switch (dayOfWeek) {
            case SUNDAY:
                lyrics += "Turya";
                break;
            case MONDAY:
                lyrics += "Turya Lya";
                break;
            case TUESDAY:
                lyrics += "Turya Turya Turya";
                break;
            case WEDNESDAY:
                lyrics += "Turya Turya Turya";
                break;
            case THURSDAY:
                lyrics += ",Ah?";
                break;
            case FRIDAY:
                lyrics += "Teyuriya";
                break;
            case SATURDAY:
                lyrics += "turya";
                break;
            default:
                break;
        }
        return this;
    }

    //Singing method
    Person sing() {
        System.out.println(lyrics);
        lyrics = "";
        return this;
    }
}

In this way, you will get sick. There is also a risk of unexpected fallthrough due to omission of description of break.

Example of using the Person class


Person.todayIs(MONDAY).sing()
    .setDayOfWeek(TUESDAY).doSometing().turya().sing()
    .setDayOfWeek(THURSDAY).gambling(100).turya().sing()
    .setDayOfWeek(SATURDAY).doSometing().turya().sing();

output


Hit the bath on monday
I'm in the bath on Tuesday Turya Turya Turya
Losing 1 million on Thursday, huh?
Saturday is just chatting turya

EnumMap

There is an EnumMap that can manage data by associating it with an enumeration. If you rewrite using this,

Person class (correction)


class Person {
    ... //Omit from field to method to set day of the week

    //Method to do something
    Person doSometing() {
        lyrics = dayOfWeek.getName() + SOMETHING_MAP.get(dayOfWeek);
        return this;
    }

    //Gambling method
    Person gambling(int amount) {
        lyrics = dayOfWeek.getName() + "To" + amount + "Ten thousand" + GAMBLING_MAP.get(dayOfWeek);
        return this;
    }

    //Tuller method
    Person turya() {
        lyrics += TURYA_MAP.get(dayOfWeek);
        return this;
    }

    //Singing method
    Person sing() {
        System.out.println(lyrics);
        lyrics = "";
        return this;
    }

    // EnumMap

    /**something*/
    private static final EnumMap<DayOfWeek, String> SOMETHING_MAP =
        new EnumMap<>(DayOfWeek.class) {{
            put(SUNDAY, "Go to the market");
            put(MONDAY, "Hit the bath");
            put(TUESDAY, "Enter the bath");
            put(WEDNESDAY, "My friends are coming");
            put(THURSDAY, "Sent");
            put(FRIDAY, "Without threading");
            put(SATURDAY, "Is just talking");
        }};

    /**gambling*/
    private static final EnumMap<DayOfWeek, String> GAMBLING_MAP =
        new EnumMap<>(DayOfWeek.class) {{
            final String WIN = "Wins";
            final String LOSE = "Lose";
            put(SUNDAY, LOSE);
            put(MONDAY, WIN);
            put(TUESDAY, LOSE);
            put(WEDNESDAY, WIN);
            put(THURSDAY, LOSE);
            put(FRIDAY, LOSE);
            put(SATURDAY, LOSE);
        }};

    /**Turya*/
    private static final EnumMap<DayOfWeek, String> TURYA_MAP =
        new EnumMap<>(DayOfWeek.class) {{
            put(SUNDAY, "Turya");
            put(MONDAY, "Turya Lya");
            put(TUESDAY, "Turya Turya Turya");
            put(WEDNESDAY, "Turya Turya Turya");
            put(THURSDAY, ",Ah?");
            put(FRIDAY, "Teyuriya");
            put(SATURDAY, "turya");
        }};
}

For the time being, it should be easier to manage ...

Not good enough! I'm not sure how to use it, but if it's data that is directly linked to the enumerator, it's defined directly in the enumerator, otherwise it's used like using EnumMap in each class [^ 1]?

[^ 1]: This time, assuming the latter (assuming that the DayOfWeek enumeration is reused in other classes), we did not associate any data other than the day of the week name with the enumeration.

Recommended Posts

switch and EnumMap
Switch between JDK 7 and JDK 8
Java if and switch statements
Upgrade and switch Ruby versions on Windows 10
switch statement
== and equals
Switch between GUI and CUI on CentOS6.
Basics of java basics ② ~ if statement and switch statement ~
Switch beans with @ConditionalOnExpression and SpEL regular expressions
Java switch statement and break, Kotlin when expression ...
Install and switch between multiple Javas on Ubuntu