package uk.co.automatictester.lightning.core.state.data;

import com.univocity.parsers.common.processor.ConcurrentRowProcessor;
import com.univocity.parsers.common.processor.RowListProcessor;
import com.univocity.parsers.csv.CsvParserSettings;
import uk.co.automatictester.lightning.core.exceptions.CSVFileNonexistentHostAndMetricException;
import uk.co.automatictester.lightning.core.s3client.factory.S3ClientFlyweightFactory;
import uk.co.automatictester.lightning.core.state.data.base.AbstractCsvEntries;

import java.io.File;
import java.util.List;

import static java.util.stream.Collectors.toList;
import static uk.co.automatictester.lightning.core.enums.PerfMonColumns.*;

public class PerfMonEntries extends AbstractCsvEntries {

    private PerfMonEntries(File perfMonCsvFile) {
        loadFromFile(perfMonCsvFile);
        throwExceptionIfEmpty();
    }

    private PerfMonEntries(List<String[]> perfMonEntries) {
        super(perfMonEntries);
    }

    private PerfMonEntries(String region, String bucket, String key) {
        s3Client = S3ClientFlyweightFactory.getInstance(region).setBucket(bucket);
        loadFromS3Object(key);
        throwExceptionIfEmpty();
    }

    public static PerfMonEntries fromFile(File perfMonCvsFile) {
        return new PerfMonEntries(perfMonCvsFile);
    }

    public static PerfMonEntries fromList(List<String[]> perfMonEntries) {
        return new PerfMonEntries(perfMonEntries);
    }

    public static PerfMonEntries fromS3Object(String region, String bucket, String key) {
        return new PerfMonEntries(region, bucket, key);
    }

    public PerfMonEntries getEntriesWith(String hostAndMetric) {
        List<String[]> list = entries.stream()
                .filter(e -> e[HOST_AND_METRIC.getColumn()].equals(hostAndMetric))
                .collect(toList());
        PerfMonEntries filteredDataEntries = PerfMonEntries.fromList(list);
        if (filteredDataEntries.size() == 0) {
            throw new CSVFileNonexistentHostAndMetricException(hostAndMetric);
        }
        return filteredDataEntries;
    }

    @Override
    protected CsvParserSettings getCsvParserSettings() {
        CsvParserSettings parserSettings = new CsvParserSettings();
        parserSettings.setLineSeparatorDetectionEnabled(true);
        parserSettings.setHeaderExtractionEnabled(false);
        parserSettings.selectIndexes(TIMESTAMP.getColumn(), VALUE.getColumn(), HOST_AND_METRIC.getColumn());
        RowListProcessor rowProcessor = new RowListProcessor();
        ConcurrentRowProcessor concurrentRowProcessor = new ConcurrentRowProcessor(rowProcessor);
        parserSettings.setProcessor(concurrentRowProcessor);
        return parserSettings;
    }
}

@@ -547,16 +547,77 @@
 .List;%0A%0A
+import static java.util.stream.Collectors.collectingAndThen;%0A
 import s
@@ -1876,205 +1876,100 @@
 ect(
-toList());%0A        PerfMonEntries filteredDataEntries = PerfMonEntries.fromList(list);%0A        if (filteredDataEntries.size() == 0) %7B%0A            throw new CSVFileNonexistentHostAndMetricException(
+collectingAndThen(toList(), filteredList -%3E returnListOrThrowExceptionIfEmpty(filteredList, 
 host
@@ -1982,19 +1982,11 @@
 ric)
-;%0A        %7D
+));
 %0A   
@@ -2001,27 +2001,37 @@
 urn 
-filteredDataEntries
+PerfMonEntries.fromList(list)
 ;%0A  
@@ -2663,11 +2663,266 @@
 s;%0A    %7D
+%0A%0A    private List%3CString%5B%5D%3E returnListOrThrowExceptionIfEmpty(List%3CString%5B%5D%3E list, String hostAndMetric) %7B%0A        if (list.size() == 0) %7B%0A            throw new CSVFileNonexistentHostAndMetricException(hostAndMetric);%0A        %7D%0A        return list;%0A    %7D
 %0A%7D%0A

Utilise collectingAndThen