package uk.ac.ebi.quickgo.annotation.service.search;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationDocument;
import uk.ac.ebi.quickgo.annotation.model.Annotation;
import uk.ac.ebi.quickgo.annotation.service.converter.AnnotationDocConverter;
import uk.ac.ebi.quickgo.rest.search.solr.AbstractSolrQueryResultConverter;
import uk.ac.ebi.quickgo.rest.search.solr.SolrQueryResultHighlightingConverter;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocumentList;

/**
 * Turns SolrDocuments into instances of the Annotation DTO.
 *
 * @author Tony Wardell
 * Date: 26/04/2016
 * Time: 16:44
 * Created with IntelliJ IDEA.
 */
class SolrQueryResultConverter extends AbstractSolrQueryResultConverter<Annotation> {

    private final DocumentObjectBinder documentObjectBinder;
    private final AnnotationDocConverter annotationDocConverter;

    private static final Map<String,String> emptyMap = new HashMap<>();

    public SolrQueryResultConverter(DocumentObjectBinder documentObjectBinder,
            AnnotationDocConverter annotationDocConverter){
        super();

        Preconditions.checkArgument(documentObjectBinder != null, "Document Object Binder cannot be null");
        Preconditions.checkArgument(annotationDocConverter != null, "Gene product document converter cannot be null");

        this.documentObjectBinder = documentObjectBinder;
        this.annotationDocConverter = annotationDocConverter;
    }

    /**
     * Turns SolrDocuments into instances of the Annotation DTO.
     * @param results
     * @return
     */
    @Override protected List<Annotation> convertResults(SolrDocumentList results) {
        assert results != null : "Results list cannot be null";

        List<AnnotationDocument> solrTermDocs = documentObjectBinder.getBeans(AnnotationDocument.class, results);
        List<Annotation> domainObjs = new ArrayList<>(solrTermDocs.size());

        solrTermDocs.stream()
                .map(annotationDocConverter::convert)
                .forEach(domainObjs::add);

        assert domainObjs.size() == results.size();
        return domainObjs;
    }
}

@@ -548,24 +548,60 @@
 a.util.Map;%0A
+import java.util.stream.Collectors;%0A
 import org.a
@@ -1760,23 +1760,172 @@
 ults
-%0A     * @return
+ a list of SolrDocuments that meet search criteria (if applicable)%0A     * @return a corresponding list of Annotations converted from the Solr Document list argument
 %0A   
@@ -2192,16 +2192,17 @@
 sults);%0A
+%0A
         
@@ -2234,55 +2234,8 @@
 js =
- new ArrayList%3C%3E(solrTermDocs.size());%0A%0A       
  sol
@@ -2328,31 +2328,35 @@
    .
-forEach(domainObjs::add
+collect(Collectors.toList()
 );%0A%0A

Optimize the creation of the list of Annotation objects. Improve javadoc.