package tudelft.mentalhealth.perfectfit; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.opencsv.CSVParser; import com.opencsv.CSVParserBuilder; import com.opencsv.CSVReader; import com.opencsv.CSVReaderBuilder; /** * The set of example goals. Reads in essentially from goals.csv database. * Singleton pattern to avoid re-reading this repeatedly which is useless as the * data is immutable. * */ public class Goals { private static Goals goals = null; // singleton cache private final List lines = new ArrayList<>(); protected Goals() throws IOException { for (String[] line : readAllLines()) { lines.add(new GoalLine(line)); } } private List readAllLines() throws IOException { InputStream stream = getClass().getClassLoader() .getResourceAsStream("examples_with_user_data.csv"); try (Reader in = new InputStreamReader(stream)) { CSVParser parser = new CSVParserBuilder().withSeparator(',') .build(); // skip the header line CSVReader reader = new CSVReaderBuilder(in).withSkipLines(1) .withCSVParser(parser).build(); return reader.readAll(); } } public static Goals instance() throws IOException { if (goals == null) { goals = new Goals(); } return goals; } @Override public String toString() { return lines.toString(); } public GoalLine get(int nr) { return lines.get(nr); } public List all() { return Collections.unmodifiableList(lines); } }