package uk.ac.ebi.quickgo.index.annotation.coterms;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Aggregation class for co-occurring terms.
 * Add a succession of terms that annotate the same gene product(s) as a single (unspecified in this class) term.
 *
 * @author Tony Wardell
 * Date: 13/11/2015
 * Time: 14:24
 * Created with IntelliJ IDEA.
 */
class CoTermsForSelectedTerm {

    private final List<CoTerm> sortedView;

    /**
     * Create an instance of this class, initializing it with the list of co-occurring terms for a term.
     * @param sortedView The list of co-occurring terms for a term, sorted by similarity ratio
     */
    private CoTermsForSelectedTerm(List<CoTerm> sortedView) {
        this.sortedView = sortedView;
    }

    /**
     * @return an immutable list of co-occurring terms, in descending order of similarity.
     */
    List<CoTerm> highestSimilarity() {
        return sortedView;
    }

    public static class Builder {
        private final List<CoTerm> coTerms = new ArrayList<>();
        private long totalNumberGeneProducts;
        private long selected;

        /**
         * @return an empty instance.
         */
        static CoTermsForSelectedTerm empty() {
            return new CoTermsForSelectedTerm(Collections.emptyList());
        }

        /**
         *
         * @param totalNumberGeneProducts The total count of unique gene products for all terms encountered during
         * processing.
         */
        Builder setTotalNumberOfGeneProducts(long totalNumberGeneProducts) {
            Preconditions
                    .checkArgument(totalNumberGeneProducts > 0, "totalNumberGeneProducts" +
                            " should not be zero");
            this.totalNumberGeneProducts = totalNumberGeneProducts;
            return this;
        }

        /**
         *
         * @param selected The count of unique gene products annotated to selected term.
         */
        Builder setSelected(long selected) {
            Preconditions.checkArgument(selected != 0, "'setSelected' should not be zero");
            this.selected = selected;
            return this;
        }

        /**
         * Add this term to the list of terms that annotate the same gene products as the target term.
         * In this method the probability and similarity ratios are calculated on the passed in CoTerm, using
         * information
         * held by this class.
         * @param coTerm has all the required information to all the co-occurrence statistics to be calculated.
         */
        Builder addCoTerm(CoTerm coTerm) {
            Preconditions.checkArgument(coTerm != null, "addCoTerm was passed a coTerm which was null");
            coTerms.add(coTerm);
            return this;
        }

        /**
         * @return an immutable list of co-occurring terms, in descending order of similarity.
         */
        CoTermsForSelectedTerm build() {
            Preconditions
                    .checkState(totalNumberGeneProducts != 0, "totalNumberGeneProducts" +
                            " should not be zero");
            Preconditions.checkArgument(selected != 0, "selected should not be zero");
            coTerms.sort(new SignificanceSorter());
            return new CoTermsForSelectedTerm(Collections.unmodifiableList(coTerms));
        }

        private class SignificanceSorter implements Comparator<CoTerm> {

            @Override
            public int compare(CoTerm o1, CoTerm o2) {
                return Float.compare(o1.getSimilarityRatio(), o2.getSimilarityRatio());
            }
        }
    }
}

@@ -170,27 +170,58 @@
 til.
-Comparator;
+List;%0Aimport java.util.stream.Collectors;%0A
 %0Aimport 
 java
@@ -208,32 +208,39 @@
 ectors;%0A%0Aimport 
+static 
 java.util.List;%0A
@@ -233,20 +233,36 @@
 va.util.
-List
+Comparator.comparing
 ;%0A%0A/**%0A 
@@ -2970,91 +2970,120 @@
   * 
-@return an immutable list of co-occurring terms, in descending order of similarity.
+Create CoTermsForSelectedTerm instance populated with CoTerms%0A         * @return CoTermsForSelectedTerm instance
 %0A   
@@ -3391,60 +3391,8 @@
 %22);%0A
-            coTerms.sort(new SignificanceSorter());%0A
     
@@ -3466,177 +3466,240 @@
 ist(
-c
+sortC
 oTerms
-));%0A        %7D%0A%0A        private class SignificanceSorter implements Comparator%3CCoTerm%3E %7B%0A%0A            @Override%0A            public int compare(CoTerm o1, CoTerm o2
+BySimilarityDescending()));%0A        %7D%0A%0A        /**%0A         * @return an immutable list of co-occurring terms, in descending order of similarity.%0A         */%0A        private List%3CCoTerm%3E sortCoTermsBySimilarityDescending(
 ) %7B%0A
@@ -3714,97 +3714,172 @@
     
-    
 return 
-Float.compare(o1.getSimilarityRatio(), o2.getSimilarityRatio());%0A            %7D
+coTerms.stream()%0A                          .sorted(comparing(CoTerm::getSimilarityRatio).reversed())%0A                          .collect(Collectors.toList());
 %0A   

Replace Comparator instance for CoTerm sorting with java 8 sorted method.