lambda - Create a separate predicate -


i have following filter in list. need people living in specified timeframe validto in both lists optional. can see, it's little bit complicated , since there other filters need make simple moving predicate variable.

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

i tried create bipredicate , use anymatch expects single predicate. person class extends time class.

any help, please?

from i'm understanding, have :

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 { } 

(well, i'm leaving implementation now).

if create following class :

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

you can shorten filter line :

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) ))... } 

is wanted ?


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -