Just to be clear I am not a java developer.
I have java code written for version 1.8. However, the java.util.function is not available in version 1.7 hence I am not able to compile the code. How can I compile\\convert the code to make it compatible to version 1.7 compiler?
below is the 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: Where to get the srccode as per below??
//pPredicate.setSrcCodeToTest("CRDSCNCTP");
//searchResults = searchResults.stream().filter(pPredicate).collect(Collectors.toList());
//TODO: what if there is no result with that src code searchResults will 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 be 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 you want to rewrite the java 8 expression in java 7 compatible form / syntax you can do it the 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 is something like
private static List<PersonSearchResult> filter(List<PersonSearchResult> sourceList, Predicate<PersonSearchResult> predicate) {
List<PersonSearchResult> result = new ArrayList<PersonSearchResult>();
if (sourceList != null) {
for (PersonSearchResult p: sourceList ) {
if ( p != null && predicate.test(p) ){
result.add(p);
}
}
return result;
}
we still need to define the Predicate
Interface. By having a look at the Java 8 API and the knowledge that we only need the test
method we can define it as:
interface Predicate<T> {
public boolean test(T t);
}
the PersonComparator
is that what you've defined as anoymous inner class.
Okay, you asked for code, so here you go:
import java.util.*;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.Iterables;
public class YourClassName {
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) {
List<PersonSearchResult> results = Lists.new ArrayList()
PersonPredicate pPredicate = new PersonPredicate();
pPredicate.setSrcCodeToTest("CRDSCNCTP");
Iterable<PersonSearchResult> result = Iterables.filter(searchResults, pPredicate);
List result = Lists.newArrayList(result);
if(result.size() > 0){
Collections.sort(result, 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 apply(PersonSearchResult person) {
return srcCodeToTest.equalsIgnoreCase(person.getPerson().getPersonId().getSrcCode());
}
public void setSrcCodeToTest(String srcCodeToTest){
this.srcCodeToTest = srcCodeToTest;
}
}
Please add guava library to your project as well. If there are some errors, do tell. I did it in a hurry.
The easiest way of doing it without adding any external dependencies is to just filter the collection yourself:
//TODO: Where to get the srccode as per below??
srcCode = "CRDSCNCTP";
searchResults = new LinkedList<>(searchResults);
Iterator<PersonSearchResult> it = searchResults.iterator();
while(it.hasNext()) {
PersonSearchResult p = it.next();
String pSrcCode = p.getPerson().getPersonId().getSrcCode();
if(!srcCode.equalsIgnoreCase(pSrcCode)) {
it.remove();
}
}
if(searchResults != null && searchResults.size() > 1){
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.