package uk.ac.ebi.quickgo.ontology.common.coterm;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * Retrieve the co-occurring terms for the selected term from the in-memory map.
 *
 * @author Tony Wardell
 * Date: 29/09/2016
 * Time: 13:48
 * Created with IntelliJ IDEA.
 */
@Component
public class CoTermRepositorySimpleMap  implements CoTermRepository{

    private Map<String, List<CoTerm>> coTermsAll;
    private Map<String, List<CoTerm>> coTermsManual;

    public CoTermRepositorySimpleMap(Map<String, List<CoTerm>> coTermsAll, Map<String, List<CoTerm>> coTermsManual) {
        this.coTermsAll = coTermsAll;
        this.coTermsManual = coTermsManual;
    }

    /**
     * Get all co-occurring terms for the requested term up to the supplied limit
     * @param id the GO Term for which we will lookup co-occurring terms.
     * @param limit Limit the number of co-occurring terms return to the limit specified.
     * @return a list of objects, each one of which represent a GO Term that is used to annotate the same gene
     * product as the id. Each object holds statistics related to that co-occurrence.
     */
    public List<CoTerm> findCoTerms(String id, CoTermSource source, int limit, Predicate filter) {
        return source == CoTermSource.MANUAL?findCoTermsFromMap(coTermsManual, id, limit, filter)
                :findCoTermsFromMap(coTermsAll, id, limit, filter);
    }


    /**
     * Get all co-occurring terms for the requested term up to the supplied limit. The data within the file is
     * ordered by GOTerm and then probability score.
     * @param id the GO Term for which we will lookup co-occurring terms.
     * @param limit Limit the number of co-occurring terms return to the limit specified.
     * @return a list of objects, each one of which represent a GO Term that is used to annotate the same gene
     * product as the id. Each object holds statistics related to that co-occurrence.
     */
    private List<CoTerm> findCoTermsFromMap(Map<String, List<CoTerm>> map, String id, int limit, Predicate filter) {
        List<CoTerm> results = map.get(id);
        if(results==null){
            return Collections.emptyList();
        }

        //If we have been passed a filtering predicate, use it. Could be extended to be a list of filters.
        if(filter !=null ){
            results = results.stream()
                    .filter(filter::test)
                    .collect(Collectors.toList());
        }

        if(results.size()<=limit){
            return results;
        }
        return results.subList(0, limit);
    }

}

@@ -474,17 +474,16 @@
 mpleMap 
- 
 implemen
@@ -501,16 +501,17 @@
 pository
+ 
 %7B%0A%0A    p
@@ -509,32 +509,38 @@
  %7B%0A%0A    private 
+final 
 Map%3CString, List
@@ -573,16 +573,22 @@
 private 
+final 
 Map%3CStri
@@ -1074,32 +1074,96 @@
 imit specified.%0A
+     * @param filter apply the predicate to filter the results.%0A
      * @return a
@@ -1431,32 +1431,40 @@
 limit, Predicate
+%3CCoTerm%3E
  filter) %7B%0A     
@@ -1506,9 +1506,11 @@
 NUAL
-?
+ ? 
 find
@@ -1575,16 +1575,17 @@
        :
+ 
 findCoTe
@@ -1634,17 +1634,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -1805,16 +1805,84 @@
 y score.
+ Apply the predicate passed to this class for filtering the results.
 %0A     * 
@@ -2030,32 +2030,96 @@
 imit specified.%0A
+     * @param filter apply the predicate to filter the results.%0A
      * @return a
@@ -2413,16 +2413,36 @@
 redicate
+%3CCoTerm%3E%0A           
  filter)
@@ -2498,24 +2498,25 @@
       if
+ 
 (results
 ==null)%7B
@@ -2511,15 +2511,18 @@
 ults
+ 
 ==
+ 
 null)
+ 
 %7B%0A  
@@ -2693,49 +2693,14 @@
     
-if(filter !=null )%7B%0A            results =
+return
  res
@@ -2713,20 +2713,16 @@
 tream()%0A
-    
         
@@ -2747,14 +2747,8 @@
 lter
-::test
 )%0A  
@@ -2765,42 +2765,21 @@
     
-    .collect(Collectors.toList());
+.limit(limit)
 %0A   
@@ -2783,27 +2783,24 @@
         
-%7D%0A%0A
         
 if(resul
@@ -2795,112 +2795,36 @@
     
-if(results.size()%3C=limit)%7B%0A            return results;%0A        %7D%0A        return results.subList(0, limit
+.collect(Collectors.toList()
 );%0A 

Change javadoc similarityThreshold -> Predicate. Change code after running analyzer.