1 | package tudelft.mentalhealth.perfectfit;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.io.InputStreamReader;
|
---|
6 | import java.io.Reader;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import com.opencsv.CSVParser;
|
---|
12 | import com.opencsv.CSVParserBuilder;
|
---|
13 | import com.opencsv.CSVReader;
|
---|
14 | import com.opencsv.CSVReaderBuilder;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * The set of example goals. Reads in essentially from goals.csv database.
|
---|
18 | * Singleton pattern to avoid re-reading this repeatedly which is useless as the
|
---|
19 | * data is immutable.
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class Goals {
|
---|
23 | private static Goals goals = null; // singleton cache
|
---|
24 |
|
---|
25 | private final List<GoalLine> lines = new ArrayList<>();
|
---|
26 |
|
---|
27 | protected Goals() throws IOException {
|
---|
28 | for (String[] line : readAllLines()) {
|
---|
29 | lines.add(new GoalLine(line));
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private List<String[]> readAllLines() throws IOException {
|
---|
34 | InputStream stream = getClass().getClassLoader()
|
---|
35 | .getResourceAsStream("examples_with_user_data.csv");
|
---|
36 |
|
---|
37 | try (Reader in = new InputStreamReader(stream)) {
|
---|
38 |
|
---|
39 | CSVParser parser = new CSVParserBuilder().withSeparator(',')
|
---|
40 | .build();
|
---|
41 | // skip the header line
|
---|
42 | CSVReader reader = new CSVReaderBuilder(in).withSkipLines(1)
|
---|
43 | .withCSVParser(parser).build();
|
---|
44 |
|
---|
45 | return reader.readAll();
|
---|
46 |
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public static Goals instance() throws IOException {
|
---|
51 | if (goals == null) {
|
---|
52 | goals = new Goals();
|
---|
53 | }
|
---|
54 | return goals;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String toString() {
|
---|
59 | return lines.toString();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public GoalLine get(int nr) {
|
---|
63 | return lines.get(nr);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public List<GoalLine> all() {
|
---|
67 | return Collections.unmodifiableList(lines);
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|