如何使用Java 8简化链接条件

时间:2016-09-01 11:45:26

标签: java lambda 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;
        }

上面的代码是使用Java 7编写的。我试图将其重构为Java 8.曾经花了一些时间使用 可选 实用程序,但无法获得解决方案这个问题,有人可以帮忙吗?我不想看到一系列If条件我的代码是我想要删除的。

我已经给出了代码的基本骨架结构。请注意,问题非常具体到Java 8,所以一个简单的答案说不,你需要按照本论坛中建议的其他选项进行接受

2 个答案:

答案 0 :(得分:5)

如果ShiftPatternDTO保持不变,请使用枚举来保存谓词:

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());
}

答案 1 :(得分:0)

你会更好地重新设计布尔代表的方式。我建议使用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);
    }

}

然后,您可以将该集合复制到新集合,而不是执行所有这些if语句。

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