Skip to content
Snippets Groups Projects
Commit 7739c622 authored by R0slyn's avatar R0slyn
Browse files

removing log writer debug checks

parent bbd4df4d
No related branches found
No related tags found
No related merge requests found
package ac.ed.lurg.country;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import ac.ed.lurg.ModelConfig;
import ac.ed.lurg.types.CropType;
import ac.ed.lurg.utils.LogWriter;
import ac.ed.lurg.types.CropToDoubleMap;
public class TradeManager {
private static final int COUNTRY_COL = 0;
private static final int ITEM_COL = 1;
private static final int TRADE_BARRIER_COL = 2;
private CompositeCountryManager compositeCountryManager;
private Map<CompositeCountry, CropToDoubleMap> tradeMap = new HashMap<CompositeCountry, CropToDoubleMap>();
private boolean activeBarriers = ModelConfig.ACTIVE_TRADE_BARRIERS;
public TradeManager(CompositeCountryManager compositeCountryManager) {
this.compositeCountryManager = compositeCountryManager;
if(activeBarriers)
read();
}
public CropToDoubleMap getTradeBarriers(CompositeCountry cc){
if (tradeMap.get(cc) == null){
CropToDoubleMap tradeBarriers = new CropToDoubleMap();
for (CropType c : CropType.getImportedTypes()) {
tradeBarriers.put(c, ModelConfig.TRADE_BARRIER_FACTOR_DEFAULT);
}
tradeMap.put(cc, tradeBarriers);
}
return tradeMap.get(cc);
}
public void read() {
String filename = ModelConfig.TRADE_BARRIERS_FILE;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line, countryName, itemName;
double barrierValue;
reader.readLine(); // read header
while ((line=reader.readLine()) != null) {
String[] tokens = line.split(",");
if (tokens.length < 3)
LogWriter.printlnError("Too few columns in " + filename + ", " + line);
try {
countryName = tokens[COUNTRY_COL];
itemName = tokens[ITEM_COL];
barrierValue = Double.parseDouble(tokens[TRADE_BARRIER_COL]);
SingleCountry country = CountryManager.getForName(countryName);
CropType crop = CropType.getForFaoName(itemName);
CompositeCountry cc = compositeCountryManager.getForSingleCountry(country);
CropToDoubleMap countryData = tradeMap.get(cc);
if (countryData == null) {
countryData = new CropToDoubleMap();
tradeMap.put(cc, countryData);
}
countryData.incrementValue(crop, barrierValue);
} catch (Exception e) {
LogWriter.printlnError("Failed in processing trade barriers line " + line);
LogWriter.print(e);
}
}
reader.close();
} catch (Exception e) {
LogWriter.printlnError("Failed in reading trade barriers data file");
LogWriter.print(e);
}
LogWriter.println("Processed " + filename + ", create " + tradeMap.size() + " barriers entries");
for (Entry<CompositeCountry, CropToDoubleMap> entry : tradeMap.entrySet()) {
// LogWriter.println("country name " + entry.getKey());
// LogWriter.println("crops " + entry.getValue());
entry.getValue().divideBy(compositeCountryManager.getAllForCompositeCountry(entry.getKey()).size());
// LogWriter.println("number of countries " + compositeCountryManager.getAllForCompositeCountry(entry.getKey()).size());
// LogWriter.println("crops " + entry.getValue());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment