stub - Convert java version 1.8 to 1.7 compatible -
just clear not java developer.
i have java code written version 1.8. however, java.util.function not available in version 1.7 hence not able compile code. how can compile\convert code make compatible version 1.7 compiler?
below code
import java.util.*; import java.util.function.predicate; public list<personsearchresult> searchpersonlistasperson( person bean, short minscore, int maxresults, string cvwname, person.personsource[] sources, person.personattribute[] attributes) throws masterdataserviceexception { logservicebegin(log, "searchpersonlistasperson"); personmapper mapper = new personmapper(); list<personsearchresult> searchresults = searchforrecordlist( mapper, bean, personentityid.getstaticenttype(), minscore, maxresults, cvwname, sources, attributes); if(searchresults != null && searchresults.size() > 1) { personpredicate ppredicate = new personpredicate(); //todo: srccode per below?? //ppredicate.setsrccodetotest("crdscnctp"); //searchresults = searchresults.stream().filter(ppredicate).collect(collectors.tolist()); //todo: if there no result src code searchresults become empty if(searchresults != null && searchresults.size() > 1){ collections.sort(searchresults, new comparator<personsearchresult>(){ public int compare(personsearchresult a, personsearchresult b) { if(isnullorempty(a.getperson().getperattributeslist()) && isnullorempty(b.getperson().getperattributeslist())){ return 0; }else if(isnullorempty(b.getperson().getperattributeslist())){ return -1; }else if(isnullorempty(a.getperson().getperattributeslist())){ return 1; }else{ int comparison = a.getperson().getperattributeslist().get(0).getstatus().comparetoignorecase(b.getperson().getperattributeslist().get(0).getstatus()); return comparison == 0 ? b.getperson().getperattributeslist().get(0).getupdatedate().compareto(a.getperson().getperattributeslist().get(0).getupdatedate()) : comparison; } } }); } } logserviceend(log, "searchpersonlistasperson"); cleanusercredentials(); return searchresults; } private boolean isnullorempty(list<memperson> list){ return list == null || list.isempty(); } class personpredicate implements predicate<personsearchresult>{ string srccodetotest; public boolean test(personsearchresult person) { return srccodetotest.equalsignorecase(person.getperson().getpersonid().getsrccode()); } public void setsrccodetotest(string srccodetotest){ this.srccodetotest = srccodetotest; }
the code can written using java 8 streams as:
return searchresults.stream() .filter((personsearchresult p) -> "crdscnctp".equalsignorecase(p.getperson().getpersonid().getsrccode())) .sorted(new personcomparator()) .collect(collectors.tolist());
if want rewrite java 8 expression in java 7 compatible form / syntax can following way:
personpredicate predicate = new personpredicate("crdscnctp"); list<personsearchresult> searchresults = ...; list<personsearchresult> filteredresult = filter(searchresult, predicate); list<personsearchresult> result = collections.sort(filteredresult , new personcomparator());
where filter
private static list<personsearchresult> filter(list<personsearchresult> sourcelist, predicate<personsearchresult> predicate) { list<personsearchresult> result = new arraylist<personsearchresult>(); if (sourcelist != null) { (personsearchresult p: sourcelist ) { if ( p != null && predicate.test(p) ){ result.add(p); } } return result; }
we still need define predicate
interface. having @ java 8 api , knowledge need test
method can define as:
interface predicate<t> { public boolean test(t t); }
the personcomparator
you've defined anoymous inner class.
Comments
Post a Comment