package uk.ac.ebi.atlas.experiments;

import com.atlassian.util.concurrent.LazyReference;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import uk.ac.ebi.atlas.model.experiment.Experiment;
import uk.ac.ebi.atlas.model.experiment.ExperimentType;
import uk.ac.ebi.atlas.trader.ExperimentTrader;
import uk.ac.ebi.atlas.utils.ExperimentInfo;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class LatestExperimentsService {

    private final LatestExperimentsDao latestExperimentsDao;
    private final ExperimentTrader experimentTrader;
    private final ImmutableSet<ExperimentType> experimentTypes;

    private final LazyReference<ImmutableMap<String, Object>> latestExperimentsAttributes = new LazyReference<ImmutableMap<String, Object>>() {
        @Override
        protected ImmutableMap<String, Object> create() throws Exception {

            long experimentCount = latestExperimentsDao.fetchPublicExperimentsCount(experimentTypes);

            List<ExperimentInfo> latestExperimentInfo =
                    FluentIterable.from(latestExperimentsDao.fetchLatestExperimentAccessions(experimentTypes))
                            .transform(new Function<String, Experiment>() {
                                @Override
                                public Experiment apply(String experimentAccession) {
                                    return experimentTrader.getPublicExperiment(experimentAccession);
                                }})
                            .transform(Experiment::buildExperimentInfo).toList();

            return ImmutableMap.of(
                    "experimentCount", experimentCount,
                    "formattedExperimentCount", NumberFormat.getNumberInstance(Locale.US).format(experimentCount),
                    "latestExperiments", latestExperimentInfo);
        }
    };

    public LatestExperimentsService(LatestExperimentsDao latestExperimentsDao,
                                    ExperimentTrader experimentTrader,
                                    Set<ExperimentType> experimentTypes) {
        this.latestExperimentsDao = latestExperimentsDao;
        this.experimentTrader = experimentTrader;
        this.experimentTypes = ImmutableSet.copyOf(experimentTypes);
    }

    public ImmutableMap<String, Object> fetchLatestExperimentsAttributes() {
        return latestExperimentsAttributes.get();
    }
}

@@ -87,97 +87,8 @@
 ce;%0A
-import com.google.common.base.Function;%0Aimport com.google.common.collect.FluentIterable;%0A
 impo
@@ -130,16 +130,16 @@
 bleMap;%0A
+
 import c
@@ -479,16 +479,52 @@
 til.Set;
+%0Aimport java.util.stream.Collectors;
 %0A%0Apublic
@@ -1158,28 +1158,8 @@
     
-FluentIterable.from(
 late
@@ -1223,16 +1223,24 @@
 ntTypes)
+.stream(
 )%0A      
@@ -1266,136 +1266,39 @@
    .
-transform(new Function%3CString, Experiment%3E() %7B%0A                                @Override%0A                                p
+map(experimentTrader::getP
 ublic
- 
 Expe
@@ -1307,254 +1307,123 @@
 ment
- apply(String experimentAccession) %7B%0A                                    return experimentTrader.getPublicExperiment(experimentAccession);%0A                                %7D%7D)%0A                            .transform(Experiment::buildExperimentInfo)
+)%0A                            .map(Experiment::buildExperimentInfo)%0A                            .collect(Collectors
 .toL
@@ -1427,16 +1427,17 @@
 toList()
+)
 ;%0A%0A     

Replace Guava’s functional primitives with Java’s native ones