Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package ac.ed.lurg.demand;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import ac.ed.lurg.ModelConfig;
import ac.ed.lurg.country.CountryManager;
import ac.ed.lurg.country.SingleCountry;
import ac.ed.lurg.types.CommodityType;
import ac.ed.lurg.utils.LogWriter;
public class CalorieManager {
private static final int COUNTRY_COL = 0;
private static final int COMMODITY_COL = 1;
private static final int CALORIE_PER_G_COL = 2;
private Map<SingleCountry, Map<CommodityType, Double>> calorieMap = new HashMap<SingleCountry, Map<CommodityType, Double>>();
public CalorieManager() {
super();
read();
}
public void read() {
String filename = ModelConfig.CALORIE_PER_T_FILE;
try {
BufferedReader fitReader = new BufferedReader(new FileReader(filename));
String line, countryName, commodityName;
double kcalPerT;
fitReader.readLine(); // read header
while ((line=fitReader.readLine()) != null) {
String[] tokens = line.split(",");
if (tokens.length < 3)
LogWriter.printlnError("Too few columns in " + filename + ", " + line);
countryName = tokens[COUNTRY_COL];
commodityName = tokens[COMMODITY_COL];
kcalPerT = Double.parseDouble(tokens[CALORIE_PER_G_COL]);
SingleCountry country = CountryManager.getForName(countryName);
if (country == null)
LogWriter.printlnError("Null country for" + countryName);
else {
CommodityType commodity = CommodityType.getForFaoName(commodityName);
Map<CommodityType, Double> commodityMap = calorieMap.get(country);
if (commodityMap == null) {
commodityMap = new HashMap<CommodityType, Double>();
calorieMap.put(country, commodityMap);
}
commodityMap.put(commodity, kcalPerT);
}
}
fitReader.close();
} catch (Exception e) {
LogWriter.printlnError("Failed in reading country specific calorie per t values");
LogWriter.print(e);
}
LogWriter.println("Processed " + filename + ", create " + calorieMap.size() + " calorie per t values");
}
public Map<CommodityType, Double> get(SingleCountry country) {
Map<CommodityType, Double> commodityMap = calorieMap.get(country);
if (commodityMap == null) {
LogWriter.printlnError("CalorieManager: can't calorie get values for " + country);
return null;
}
return commodityMap;
}
}