1 package com.calipso.reportgenerator.reportcalculator; 2 3 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 4 5 import java.util.*; 6 import java.io.Serializable ; 7 8 11 12 public class DataTreeNode implements Serializable { 13 private String value; 14 private List subItems; 15 private SharedFloat[] metrics; 16 private Map subNodes; 18 private DataTreeNode parent; 19 private int dimensionIndex; 20 21 22 29 public DataTreeNode(DataTreeNode parent, String value, int metricCount, int dimensionIndex) { 30 this.parent = parent; 31 this.value = value; 32 this.subItems = new ArrayList(); 33 this.metrics = new SharedFloat[metricCount]; 34 this.dimensionIndex = dimensionIndex; 36 initialize(); 37 } 38 39 42 private void initialize() { 43 for (int i = 0; i < metrics.length; i++){ 44 metrics[i] = SharedFloat.newFrom(0); 45 } 46 47 50 51 } 52 53 57 public int getDimensionIndex() { 58 return dimensionIndex; 59 } 60 61 65 public void setDimensionIndex(int dimensionIndex) { 66 this.dimensionIndex = dimensionIndex; 67 } 68 69 73 public String getValue() { 74 return value; 75 } 76 77 82 public List getSubItems() { 83 return subItems; 84 } 85 86 90 public SharedFloat[] getMetrics() { 91 return metrics; 92 } 93 94 97 98 102 public Map getSubNodes() { 103 if (subNodes == null) { 104 subNodes = new TreeMap(); 105 } 106 return subNodes; 107 } 108 109 117 public DataTreeNode getNodeFrom(Object key, int dimensionIndex) { 118 DataTreeNode node; 119 if (getSubNodes().containsKey(key)) { 120 node = (DataTreeNode) getSubNodes().get(key); 121 } 122 else { 123 node = new DataTreeNode(this, key.toString(), metrics.length, dimensionIndex ); 124 getSubNodes().put(key, node); 125 } 126 return node; 127 } 128 129 137 public void updateMetricValue(int index, SharedFloat value) { 138 metrics[index] = (metrics[index]).add(value); 139 145 } 146 147 151 public DataTreeNode getParent() { 152 return parent; 153 } 154 155 159 public void setValue(String value) { 160 this.value = value; 161 } 162 163 168 public void addSubItem(DataTreeSubItem subItem) { 169 getSubItems().add(subItem); 170 } 171 172 177 public void calculateAccumulable(int[] adjAccumulableMetrics) { 178 Iterator subNodesIter = getSubNodes().values().iterator(); 179 while (subNodesIter.hasNext()) { 180 DataTreeNode dataTreeNode = (DataTreeNode) subNodesIter.next(); 181 dataTreeNode.calculateAccumulable(adjAccumulableMetrics); 182 } 183 184 SharedFloat[] accValues = new SharedFloat[adjAccumulableMetrics.length]; 185 Iterator subItemsIter = getSubItems().iterator(); 186 while (subItemsIter.hasNext()) { 187 DataTreeSubItem subItem = (DataTreeSubItem) subItemsIter.next(); 188 for (int i = 0; i < accValues.length; i++) { 189 if (accValues[i] == null) { 190 accValues[i] = SharedFloat.newFrom(0); 191 } 192 accValues[i].add(subItem.getMetricValues()[adjAccumulableMetrics[i]]); 193 } 194 subItem.setAccumulableMetricValues(copyOf(accValues)); 195 subItem.setAdjMetricIndexes(adjAccumulableMetrics); 196 } 197 } 198 199 205 private SharedFloat[] copyOf(SharedFloat[] source) { 206 SharedFloat[] result = new SharedFloat[source.length]; 207 for (int i = 0; i < source.length; i++) { 208 result[i] = SharedFloat.newFrom(source[i].floatValue()); 209 } 210 return result; 211 } 212 213 public DataTreeSubItem getSubItem(String [] noGroupDimValues) { 214 Iterator iterator = getSubItems().iterator(); 215 while (iterator.hasNext()) { 216 DataTreeSubItem dataTreeSubItem = (DataTreeSubItem) iterator.next(); 217 if(dataTreeSubItem.matches(noGroupDimValues)){ 218 return dataTreeSubItem; 219 } 220 } 221 DataTreeSubItem subItem = new DataTreeSubItem(noGroupDimValues, getMetrics().length); 222 getSubItems().add(subItem); 223 return subItem; 224 } 225 } 226 | Popular Tags |