user1224036
user1224036

Reputation: 1008

How to simplify chained if conditions using Java 8

   public class ShiftPatternDTO{
    private boolean monday;
    private boolean tuesday;
    private boolean wednesday;
    private boolean thursday;
    private boolean friday;
    private boolean saturday;
    private boolean sunday;

    public boolean isMonday() {
        return monday;
    }


    public boolean isTuesday() {
        return tuesday;
    }


    public boolean isWednesday() {
        return wednesday;
    }


    public boolean isThursday() {
        return thursday;
    }



    public boolean isFriday() {
        return friday;
    }


    public boolean isSaturday() {
        return saturday;
    }


    public boolean isSunday() {
        return sunday;
    }
}

     public enum WeekDay {

        MONDAY("Monday", 1),
        TUESDAY("Tuesday", 2),
        WEDNESDAY("Wednesday", 3),
        THURSDAY("Thursday", 4),
        FRIDAY("Friday", 5),
        SATURDAY("Saturday", 6),
        SUNDAY("Sunday", 7);
        }

    private Set<WeekDay> getWeekPattern(ShiftPatternDTO shiftPattern) {
            Set<WeekDay> weekPatterns = new LinkedHashSet<>();
            if (shiftPattern.isMonday()) {
                weekPatterns.add(WeekDay.MONDAY);
            }
            if (shiftPattern.isTuesday()) {
                weekPatterns.add(WeekDay.TUESDAY);
            }
            if (shiftPattern.isWednesday()) {
                weekPatterns.add(WeekDay.WEDNESDAY);
            }
            if (shiftPattern.isThursday()) {
                weekPatterns.add(WeekDay.THURSDAY);
            }
            if (shiftPattern.isFriday()) {
                weekPatterns.add(WeekDay.FRIDAY);
            }
            if (shiftPattern.isSaturday()) {
                weekPatterns.add(WeekDay.SATURDAY);
            }
            if (shiftPattern.isSunday()) {
                weekPatterns.add(WeekDay.SUNDAY);
            }
            return weekPatterns;
        }

The above code is written using Java 7. I am trying to refactor this to Java 8. Had spent sometime using Optional utility but not able to derive a solution to this problem, Please can someone help? I dont want to see series of If conditions my code which is what i am trying to remove.

I have given basic skeleton structure of my code. Note that question is very specific to Java 8, So a simple answer saying No, you need to follow the other options suggested in this forum would be accepted

Upvotes: 1

Views: 486

Answers (2)

mtj
mtj

Reputation: 3554

If the ShiftPatternDTO shall stay unmodified, use the enum to hold a predicate:

public enum WeekDay {
    MONDAY("Monday", 1, ShiftPatternDTO::isMonday),
    TUESDAY("Tuesday", 2, ShiftPatternDTO::isTuesday),
    WEDNESDAY("Wednesday", 3, ShiftPatternDTO::isWednesday),
    THURSDAY("Thursday", 4, ShiftPatternDTO::isThursday),
    FRIDAY("Friday", 5, ShiftPatternDTO::isFriday),
    SATURDAY("Saturday", 6, ShiftPatternDTO::isSaturday),
    SUNDAY("Sunday", 7, ShiftPatternDTO::isSunday);

    public final String name;
    public final int num;
    public final Predicate<ShiftPatternDTO> pred;

    private WeekDay(String name, int num, Predicate<ShiftPatternDTO> pred) {
        this.name = name;
        this.num = num;
        this.pred = pred;
    }
}

private Set<WeekDay> getWeekPatternNew(ShiftPatternDTO shiftPattern) {
    return
        Arrays.stream(WeekDay.values())
        .filter(wd -> wd.pred.test(shiftPattern))
        .collect(Collectors.toSet());
}

Upvotes: 5

SamTebbs33
SamTebbs33

Reputation: 5647

You would be much better of redesigning the way that the booleans are represented. I suggest using a Set<WeekDay>.

public class ShiftPatternDTO {

    Set<WeekDay> days = new HashSet<>();

    public boolean isDay(WeekDay day) {
        return days.contains(day);
    }

    public void addDay(WeekDay day) {
        days.add(day);
    }

}

You could then just copy the set to your new set instead of doing all of those if-statements.

Set<WeekDay> weekPatterns = days.stream().collect(Collectors.toSet());

Upvotes: 0

Related Questions