source: PerfectFit/Dialog/src/main/java/tudelft/mentalhealth/perfectfit/Goals.java@ 7

Last change on this file since 7 was 7, checked in by Wouter Pasman, 9 months ago

#124 release PerfectFit sources

File size: 1.6 KB
Line 
1package tudelft.mentalhealth.perfectfit;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.InputStreamReader;
6import java.io.Reader;
7import java.util.ArrayList;
8import java.util.Collections;
9import java.util.List;
10
11import com.opencsv.CSVParser;
12import com.opencsv.CSVParserBuilder;
13import com.opencsv.CSVReader;
14import 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 */
22public 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}
Note: See TracBrowser for help on using the repository browser.