source: MotivateDuringTherapy/src/main/java/tudelft/mentalhealth/motivatepersisting/MeanNrStatements.java

Last change on this file was 5, checked in by Bart Vastenhouw, 5 years ago

Intermediate update

File size: 1.8 KB
Line 
1package tudelft.mentalhealth.motivatepersisting;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.util.Collections;
8import java.util.HashMap;
9import java.util.Map;
10
11/**
12 * Table 3: Mean nr. of statements in a given answer per situation, defined by
13 * PCL score and Trust. This is a singleton as we have only 1 fixed table
14 * backing this, to avoid repeated loading of the table.
15 */
16public class MeanNrStatements {
17
18 private Map<Situation, Double> meanNrStatements = new HashMap<>();
19 private static MeanNrStatements instance;
20
21 private MeanNrStatements() {
22 readTable();
23 }
24
25 /**
26 * @return the whole table, immutable
27 */
28 public Map<Situation, Double> getTable() {
29 return Collections.unmodifiableMap(meanNrStatements);
30 }
31
32 /**
33 * @return the instance of the MeanNrStatements.
34 * @throws IOException
35 */
36 public static MeanNrStatements instance() {
37 if (instance == null) {
38 instance = new MeanNrStatements();
39 }
40 return instance;
41 }
42
43 @Override
44 public String toString() {
45 return meanNrStatements.toString();
46 }
47
48 /**
49 *
50 * @param situation the {@link Situation} of the patient.
51 * @return the average (mean) number of statements in the situation
52 */
53 public Double getMean(Situation situation) {
54 return meanNrStatements.get(situation);
55 }
56
57 private void readTable() {
58 InputStream is = MeanNrStatements.class
59 .getResourceAsStream("MeanNrStatements.csv");
60
61 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
62 reader.lines().forEach(line -> addLine(line));
63
64 }
65
66 private void addLine(String line) {
67 String[] values = line.split(",");
68 PclTrend pcl = PclTrend.valueOf(values[0]);
69 Trust trust = Trust.valueOf(values[1]);
70 Double mean = Double.valueOf(values[2]);
71 meanNrStatements.put(new Situation(pcl, trust), mean);
72 }
73}
Note: See TracBrowser for help on using the repository browser.