创建一个单独的谓词

时间:2017-07-28 14:41:08

标签: lambda java-8 predicate

我的列表中有以下过滤器。我需要生活在指定时间范围内的人,其中两个列表中的validTo都是可选的。正如您所看到的,它有点复杂,因为有其他过滤器,我需要通过将谓词移动到变量来使其变得简单。

people.stream()
            .filter(person -> peopleTime.stream().anyMatch(time ->
                    (!person.getValidTo().isPresent() || time.getValidFrom().isBefore(person.getValidTo().get()) || time.getValidFrom().isEqual(person.getValidTo().get()))
                            && (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom()) || time.getValidTo().get().isEqual(person.getValidFrom()))))

我尝试创建一些BiPredicate并使用它,但anyMatch需要单个谓词。 Person类扩展了Time类。

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

根据我的理解,你基本上有:

public abstract static class MyDate {
    public abstract boolean isBefore(MyDate other);
    public abstract boolean isAfter(MyDate other);
    public abstract boolean isEqual(MyDate other);
}
public static abstract class Time {
    public abstract Optional<MyDate> getValidTo();
    public abstract Optional<MyDate> getValidFrom();
}

public static abstract class Person extends Time {
}

(好吧,我暂时离开了实施)。

如果您创建以下类:

public static class TimePersonPredicate implements Predicate<Time> {

    private final Person person;
    public TimePersonPredicate(Person person) {
        this.person = person;
    }
    @Override
    public boolean test(Time time) {
        return (!person.getValidTo().isPresent() || time.getValidFrom().get().isBefore(person.getValidTo().get()) || time.getValidFrom().get().isEqual(person.getValidTo().get()))
                && (!time.getValidTo().isPresent() || time.getValidTo().get().isAfter(person.getValidFrom().get()) || time.getValidTo().get().isEqual(person.getValidFrom().get()));
    }

}

您可以像这样缩短过滤行:

public static void main(String[] args) {
    List<Person> people = new ArrayList<>();
    List<Time> peopleTime = new ArrayList<>();
    people.stream()
        .filter(person -> peopleTime.stream().anyMatch(new TimePersonPredicate(person) ))...
}

这就是你想要的吗?

相关问题