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

import uk.ac.ebi.quickgo.geneproduct.common.document.GeneProductDocument;
import uk.ac.ebi.quickgo.geneproduct.model.GeneProduct;
import uk.ac.ebi.quickgo.geneproduct.service.converter.GeneProductDocConverter;
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.List;
import java.util.Map;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Converts the Solr results into {@link GeneProduct} instances.
 *
 * Created 06/04/16
 * @author Edd
 */
public class GeneProductSolrQueryResultConverter extends AbstractSolrQueryResultConverter<GeneProduct> {
    private static final Logger LOGGER = LoggerFactory.getLogger(GeneProductSolrQueryResultConverter.class);

    private final DocumentObjectBinder documentObjectBinder;
    private final GeneProductDocConverter geneProductDocConverter;

    public GeneProductSolrQueryResultConverter(DocumentObjectBinder documentObjectBinder,
            GeneProductDocConverter geneProductDocConverter,
            Map<String, String> fieldNameMap){
        super(new SolrQueryResultHighlightingConverter(fieldNameMap));

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

        this.documentObjectBinder = documentObjectBinder;
        this.geneProductDocConverter = geneProductDocConverter;
    }

    @Override protected List<GeneProduct> convertResults(SolrDocumentList results) {
        assert results != null : "Results list cannot be null";

        List<GeneProductDocument> solrTermDocs = documentObjectBinder.getBeans(GeneProductDocument.class, results);

        List<GeneProduct> domainTerms = new ArrayList<>(solrTermDocs.size());

        solrTermDocs.stream()
                .map(geneProductDocConverter::convert)
                .forEach(domainTerms::add);

        assert domainTerms.size() == results.size();

        return domainTerms;
    }
}

@@ -477,21 +477,16 @@
 va.util.
-Array
 List;%0Aim
@@ -492,36 +492,35 @@
 mport java.util.
-List
+Map
 ;%0Aimport java.ut
@@ -514,35 +514,49 @@
 mport java.util.
-Map
+stream.Collectors
 ;%0Aimport org.apa
@@ -2073,86 +2073,14 @@
     
-List%3CGeneProduct%3E domainTerms = new ArrayList%3C%3E(solrTermDocs.size());%0A%0A       
+return
  sol
@@ -2174,116 +2174,36 @@
    .
-forEach(domainTerms::add);%0A%0A        assert domainTerms.size() == results.size();%0A%0A        return domainTerms
+collect(Collectors.toList())
 ;%0A  

simplify return of results: no assertion done because functional interface guarantees each object is converted.