00001 00002 // Copyright 2010 by Carlo Jaeger, Antoine Mandel, Steffen Fuerst and European Climate Forum 00003 // Licensed under the Open Software License version 3.0 00004 // See the file "License-OSL-3.0.txt" in the distribution for more information 00005 // The License text can be also found under http://www.opensource.org/licenses/osl-3.0.php 00007 00008 package de.pik.lagom.generic; 00009 00010 import java.util.Arrays; 00011 00012 // TradeTrace is used for retrieving statistical values based on the trading of goods. 00018 public class TradeTrace { 00019 private final Foundation foundation; 00020 00022 private double overallTradeValue; 00024 private double overallTradeValueWithoutImportExport; 00027 private double overallSectorTradeValue[]; 00030 private double overallSectorTradeValueWithoutImportExport[]; 00032 private double overallQuantity; 00034 private double overallQuantityWithoutImportExport; 00037 private double overallSectorQuantity[]; 00040 private double overallSectorQuantityWithoutImportExport[]; 00043 private double overallSectorQuantityBoughtByHouseholds[]; 00047 private double overallSectorQuantityBoughtByFirmsOfSector[][]; 00048 00049 TradeTrace(Foundation pFoundation) { 00050 foundation = pFoundation; 00051 } 00052 00053 public void init() { 00054 final int lNumSectors = foundation.getNumSectors(); 00055 overallSectorTradeValue = new double[lNumSectors]; 00056 overallSectorTradeValueWithoutImportExport = new double[lNumSectors]; 00057 overallSectorQuantity = new double[lNumSectors]; 00058 overallSectorQuantityWithoutImportExport = new double[lNumSectors]; 00059 overallSectorQuantityBoughtByHouseholds = new double[lNumSectors]; 00060 overallSectorQuantityBoughtByFirmsOfSector = new double[lNumSectors][lNumSectors]; 00061 } 00062 00063 public void clear() { 00064 overallTradeValue = 0.d; 00065 overallTradeValueWithoutImportExport = 0.d; 00066 Arrays.fill(overallSectorTradeValue, 0.d); 00067 Arrays.fill(overallSectorTradeValueWithoutImportExport, 0.d); 00068 overallQuantity = 0.d; 00069 overallQuantityWithoutImportExport = 0.d; 00070 Arrays.fill(overallSectorQuantity, 0.d); 00071 Arrays.fill(overallSectorQuantityWithoutImportExport, 0.d); 00072 Arrays.fill(overallSectorQuantityBoughtByHouseholds, 0.d); 00073 for (int i = 0; i < overallSectorQuantityBoughtByFirmsOfSector.length; i++) { 00074 Arrays.fill(overallSectorQuantityBoughtByFirmsOfSector[i], 0.d); 00075 } 00076 } 00077 00078 public void reportTrade(double pQuantity, IBuyer pBuyer, ISeller pSeller) { 00079 final double lPrice = pSeller.getPrice(); 00080 final double lTradeValue = pQuantity * lPrice; 00081 final int lProductionSectorIndex = pSeller.getProductionSector().getArrayIndex(); 00082 // assert(lTradeValue>0); 00083 00084 overallTradeValue += lTradeValue; 00085 overallSectorTradeValue[lProductionSectorIndex] += lTradeValue; 00086 overallQuantity += pQuantity; 00087 overallSectorQuantity[lProductionSectorIndex] += pQuantity; 00088 00089 if (pBuyer instanceof Household) { 00090 overallSectorQuantityBoughtByHouseholds[lProductionSectorIndex] += pQuantity; 00091 } else if (pBuyer instanceof Firm) { 00092 final Firm lFirm = (Firm) pBuyer; 00093 overallSectorQuantityBoughtByFirmsOfSector[lProductionSectorIndex] 00094 [lFirm.getProductionSector().getArrayIndex()] += 00095 pQuantity; 00096 } 00097 00098 if (!(pBuyer instanceof ImportExport) && !(pSeller instanceof ImportExport)) { 00099 overallTradeValueWithoutImportExport += lTradeValue; 00100 overallSectorTradeValueWithoutImportExport[lProductionSectorIndex] += lTradeValue; 00101 overallQuantityWithoutImportExport += pQuantity; 00102 overallSectorQuantityWithoutImportExport[lProductionSectorIndex] += pQuantity; 00103 } 00104 } 00105 00106 public double getOverallTradeValue() { 00107 return overallTradeValue; 00108 } 00109 00110 public double getOverallTradeValueWithoutImportExport() { 00111 return overallTradeValueWithoutImportExport; 00112 } 00113 00114 public double getOverallQuantity() { 00115 return overallQuantity; 00116 } 00117 00118 public double getOverallQuantityWithoutImportExport() { 00119 return overallQuantityWithoutImportExport; 00120 } 00121 00122 public double getAverageTradePrice() { 00123 return getOverallTradeValue() / getOverallQuantity(); 00124 } 00125 00126 public double getAverageTradePriceWithoutImportExport() { 00127 return getOverallTradeValueWithoutImportExport() / getOverallQuantityWithoutImportExport(); 00128 } 00129 00130 public double getOverallTradeValueSelledInSector(Sector pSector) { 00131 return overallSectorTradeValue[pSector.getArrayIndex()]; 00132 } 00133 00134 public double getOverallTradeValueSelledInSectorWithoutImportExport(Sector pSector) { 00135 return overallSectorTradeValueWithoutImportExport[pSector.getArrayIndex()]; 00136 } 00137 00138 00139 public double getOverallQuantitySelledInSector(Sector pSector) { 00140 return overallSectorQuantity[pSector.getArrayIndex()]; 00141 } 00142 00143 public double getOverallQuantitySelledInSectorWithoutImportExport(Sector pSector) { 00144 return overallSectorQuantityWithoutImportExport[pSector.getArrayIndex()]; 00145 } 00146 00147 00148 public double getAverageTradePriceSelledInSector(Sector pSector) { 00149 return getOverallTradeValueSelledInSector(pSector) / 00150 getOverallQuantitySelledInSector(pSector); 00151 } 00152 00153 public double getAverageTradePriceSelledInSectorWithoutImportExport(Sector pSector) { 00154 return getOverallTradeValueSelledInSectorWithoutImportExport(pSector) / 00155 getOverallQuantitySelledInSectorWithoutImportExport(pSector); 00156 } 00157 00158 public double getOverallQuantityBoughtByHouseholds(Sector pSector) { 00159 return overallSectorQuantityBoughtByHouseholds[pSector.getArrayIndex()]; 00160 } 00161 00162 public double getOverallCO2BoughtByFirmsInSector(Sector pSector) { 00163 double lOverallCO2 = 0.d; 00164 final int lSectorIndex = pSector.getArrayIndex(); 00165 final int lNumSectors = overallSectorQuantityBoughtByFirmsOfSector.length; 00166 00167 for (int iSector = 0; iSector < lNumSectors; iSector++) { 00168 lOverallCO2 += overallSectorQuantityBoughtByFirmsOfSector[iSector][lSectorIndex] 00169 * foundation.getSectorWithIndex(iSector).getInitUnitInPetaJoule() 00170 * foundation.getSectorWithIndex(iSector).calcCiCoef( 00171 foundation.getPeriodAsYear()); 00172 } 00173 return lOverallCO2; 00174 } 00175 } 00176 00178 // EOF