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

import com.google.common.base.Preconditions;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author Tony Wardell
 * Date: 29/09/2016
 * Time: 13:48
 * Created with IntelliJ IDEA.
 */
@Component
public class CoTermRepositorySimpleMap  implements CoTermRepository{

    private static final Logger logger = LoggerFactory.getLogger(CoTermRepositorySimpleMap.class);
    private Map<String, List<CoTerm>> coTermsAll;
    private Map<String, List<CoTerm>> coTermsManual;

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

    /**
     * Get all co-occurring terms for the requested term up to the supplied limit
     * @param id
     * @param limit
     * @return
     */
    public List<CoTerm> findCoTerms(String id, CoTermType type, int limit, int similarityThreshold) {
        return type==CoTermType.MANUAL?findCoTermsFromMap(coTermsManual, id, limit, similarityThreshold)
                :findCoTermsFromMap(coTermsAll, id, limit, similarityThreshold);
    }


    /**
     * Get all co-occurring terms for the requested term up to the supplied limit
     * @param id
     * @param limit
     * @return
     */
    private List<CoTerm> findCoTermsFromMap(Map<String, List<CoTerm>> map, String id, int limit, int
            similarityThreshold) {

        List<CoTerm> results = map.get(id);

        if(results==null){
            return new ArrayList<>();
        }

        if(similarityThreshold >0){
            results = results.stream()
                    .filter(t -> t.getSignificance() >= similarityThreshold)    //todo requires sorting too?
                    .collect(Collectors.toList());
        }

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

        return results.subList(0, limit);
    }

    /**
     * Show some statistics for the Costats
     * @param out
     */
    public void report(PrintStream out) {

        out.println("Manual Report");
        out.println("==============");
        out.println("Number of Terms: " + coTermsManual.keySet().size());
        out.println("");
        out.println("ALL Report");
        out.println("==========");
        out.println("Number of Terms: " + coTermsAll.keySet().size());
        out.println("");

    }
}

@@ -55,100 +55,29 @@
 ort 
-com.google.common.base.Preconditions;%0Aimport java.io.PrintStream;%0Aimport java.util.ArrayList
+java.util.Collections
 ;%0Aim
@@ -171,105 +171,132 @@
 rg.s
-lf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.stereotype.Component;%0A%0A/*
+pringframework.stereotype.Component;%0A%0A/**%0A * Retrieve the co-occurring terms for the selected term from the in-memory map.%0A 
 *%0A *
@@ -472,107 +472,8 @@
 y%7B%0A%0A
-    private static final Logger logger = LoggerFactory.getLogger(CoTermRepositorySimpleMap.class);%0A
     
@@ -637,20 +637,20 @@
 erm%3E%3E co
-stat
+Term
 sAll, Ma
@@ -675,20 +675,20 @@
 erm%3E%3E co
-stat
+Term
 sManual)
@@ -718,20 +718,20 @@
 All = co
-stat
+Term
 sAll;%0A  
@@ -759,20 +759,20 @@
 ual = co
-stat
+Term
 sManual;
@@ -889,43 +889,352 @@
 m id
-%0A     * @param limit%0A     * @return
+ the GO Term for which we will lookup co-occurring terms.%0A     * @param limit Limit the number of co-occurring terms return to the limit specified.%0A     * @return a list of objects, each one of which represent a GO Term that is used to annotate the same gene%0A     * product as the id. Each object holds statistics related to that co-occurrence.
 %0A   
@@ -1295,16 +1295,20 @@
 Term
-Type typ
+Source sourc
 e, i
@@ -1363,23 +1363,29 @@
 urn 
-type
+source 
 ==
+ 
 CoTerm
-Typ
+Sourc
 e.MA
@@ -1654,43 +1654,352 @@
 m id
-%0A     * @param limit%0A     * @return
+ the GO Term for which we will lookup co-occurring terms.%0A     * @param limit Limit the number of co-occurring terms return to the limit specified.%0A     * @return a list of objects, each one of which represent a GO Term that is used to annotate the same gene%0A     * product as the id. Each object holds statistics related to that co-occurrence.
 %0A   
@@ -2135,25 +2135,24 @@
 hreshold) %7B%0A
-%0A
         List
@@ -2183,17 +2183,16 @@
 et(id);%0A
-%0A
         
@@ -2233,23 +2233,29 @@
 urn 
-new Arra
+Collections.empt
 yList
-%3C%3E
 ();%0A
@@ -2257,32 +2257,111 @@
 t();%0A        %7D%0A%0A
+        //use similarity threshold to filter results if it has been specified.%0A
         if(simil
@@ -2374,17 +2374,16 @@
 hreshold
- 
 %3E0)%7B%0A   
@@ -2580,33 +2580,32 @@
 t());%0A        %7D%0A
-%0A
         if(resul
@@ -2681,33 +2681,32 @@
 )-1);%0A        %7D%0A
-%0A
         return r
@@ -2742,476 +2742,6 @@
  %7D%0A%0A
-    /**%0A     * Show some statistics for the Costats%0A     * @param out%0A     */%0A    public void report(PrintStream out) %7B%0A%0A        out.println(%22Manual Report%22);%0A        out.println(%22==============%22);%0A        out.println(%22Number of Terms: %22 + coTermsManual.keySet().size());%0A        out.println(%22%22);%0A        out.println(%22ALL Report%22);%0A        out.println(%22==========%22);%0A        out.println(%22Number of Terms: %22 + coTermsAll.keySet().size());%0A        out.println(%22%22);%0A%0A    %7D%0A
 %7D%0A

Add javadoc, change return code a little.