package archimulator.util.csv;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.QuoteMode;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * CSV helper.
 *
 * @author Min Cai
 */
public class CSVHelper {
    public static <ExperimentT> void toCsv(String outputCSVFileName, List<ExperimentT> results, List<CSVField<ExperimentT>> fields) {
        File resultDirFile = new File(outputCSVFileName).getParentFile();

        if (!resultDirFile.exists()) {
            if (!resultDirFile.mkdirs()) {
                throw new RuntimeException();
            }
        }

        CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',').withQuoteMode(QuoteMode.ALL).withQuote('"');

        try {
            FileWriter writer = new FileWriter(outputCSVFileName);
            CSVPrinter printer = new CSVPrinter(writer, format);
            printer.printRecord(fields);

            for (ExperimentT experiment : results) {
                List<String> experimentData = new ArrayList<>();

                for (CSVField<ExperimentT> field : fields) {
                    experimentData.add(field.getFunc().apply(experiment));
                }

                printer.printRecord(experimentData);
            }

            printer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -367,25 +367,21 @@
 static %3C
-Experimen
+Resul
 tT%3E void
@@ -418,25 +418,21 @@
 e, List%3C
-Experimen
+Resul
 tT%3E resu
@@ -450,25 +450,21 @@
 SVField%3C
-Experimen
+Resul
 tT%3E%3E fie
@@ -1030,29 +1030,21 @@
 or (
-ExperimentT experimen
+ResultT resul
 t : 
@@ -1083,30 +1083,22 @@
 String%3E 
-experimentData
+record
  = new A
@@ -1146,17 +1146,13 @@
 eld%3C
-Experimen
+Resul
 tT%3E 
@@ -1189,30 +1189,22 @@
         
-experimentData
+record
 .add(fie
@@ -1222,25 +1222,21 @@
 ).apply(
-experimen
+resul
 t));%0A   
@@ -1291,22 +1291,14 @@
 ord(
-experimentData
+record
 );%0A 

Update CSVHelper.