package tudelft.mentalhealth.motivatepersisting; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * Table 3: Mean nr. of statements in a given answer per situation, defined by * PCL score and Trust. This is a singleton as we have only 1 fixed table * backing this, to avoid repeated loading of the table. */ public class MeanNrStatements { private Map meanNrStatements = new HashMap<>(); private static MeanNrStatements instance; private MeanNrStatements() { readTable(); } /** * @return the whole table, immutable */ public Map getTable() { return Collections.unmodifiableMap(meanNrStatements); } /** * @return the instance of the MeanNrStatements. * @throws IOException */ public static MeanNrStatements instance() { if (instance == null) { instance = new MeanNrStatements(); } return instance; } @Override public String toString() { return meanNrStatements.toString(); } /** * * @param situation the {@link Situation} of the patient. * @return the average (mean) number of statements in the situation */ public Double getMean(Situation situation) { return meanNrStatements.get(situation); } private void readTable() { InputStream is = MeanNrStatements.class .getResourceAsStream("MeanNrStatements.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); reader.lines().forEach(line -> addLine(line)); } private void addLine(String line) { String[] values = line.split(","); PclTrend pcl = PclTrend.valueOf(values[0]); Trust trust = Trust.valueOf(values[1]); Double mean = Double.valueOf(values[2]); meanNrStatements.put(new Situation(pcl, trust), mean); } }