-1

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;
        }
Waseem Ahmed
  • 57
  • 10
  • Sorry but once you move forward there is no going back. But if you still want to go back you have to make necessary changes in your code that can tackle the code that you have implemented in java 8 style to java 7 style. And as Java 8 is lot different its previous version, trying this is next to impossible. – Sumit Surana May 15 '15 at 06:31
  • 1
    You can use library like guava which has functions and predicates and easily modify the code. – Heisenberg May 15 '15 at 06:32
  • I dont understand much of this code. However, I am happy to learn and translate this code myself. Can you please be kind enough to let me know the flow of the code and which documentation I should be referring to get help? -- Many Thanks!! – Waseem Ahmed May 15 '15 at 06:53
  • Why do you write Java 8 Code and afterwards want to compile it using the Java 7 SDK. If you want to run Java 8 Code on a Java 7 JVM you can use retrolamda https://github.com/orfjackal/retrolambda which is a backport of Java 8 Features to Java 5,6,7. – andih May 15 '15 at 07:50

3 Answers3

1

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.

Sevle
  • 3,109
  • 2
  • 19
  • 31
andih
  • 5,570
  • 3
  • 26
  • 36
0

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.

Heisenberg
  • 3,153
  • 3
  • 27
  • 55
0

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){
    ...
Raniz
  • 10,882
  • 1
  • 32
  • 64