1 package com.calipso.reportgenerator.reportcalculator; 2 3 import java.util.*; 4 5 17 public class CubeIterator { 18 LinkedList totals; 19 Iterator[] iterators; 20 Map.Entry[] entries; 21 int[] dimensions; 22 int[] metrics; 23 Cube cube; 24 int metricsSize; 25 int dimensionsSize; 26 27 28 33 public static CubeIterator on(Cube cube) { 34 return new CubeIterator(cube); 35 } 36 37 41 public CubeIterator(Cube cube) { 42 this.cube = cube; 43 initialize(); 44 } 45 46 50 private void initialize() { 51 dimensionsSize = cube.getDimensionsSize(); 52 metricsSize = cube.getMetricsSize(); 53 dimensions = cube.getIntegerArray(0, dimensionsSize); 54 metrics = cube.getIntegerArray(dimensionsSize, metricsSize); 55 entries = new Map.Entry[dimensionsSize + 1]; 56 iterators = new Iterator[dimensionsSize]; 57 entryAtPut(-1, new Entry("root", cube.getRoot())); 59 } 60 61 66 public Map.Entry entryAt(int index) { 67 return entries[index + 1]; 68 } 69 70 75 public void entryAtPut(int index, Map.Entry value) { 76 entries[index + 1] = value; 77 fillEntryWithParentsValues(index); 78 } 79 80 87 private void fillEntryWithParentsValues(int index) { 88 int i; 89 Map.Entry entry; 90 for (i = 0; i <= index; i++) { 91 entry = entries[i + 1]; 92 if (entry != null) { 93 ((Object []) entries[index + 1].getValue())[dimensions[i]] = entry.getKey(); 94 } 95 } 96 } 97 98 102 public void initializeIterator(int dimensionIndex) { 103 iterators[dimensionIndex] = iteratorFor(dimensionIndex); 104 if (iterators[dimensionIndex].hasNext()) { 105 updateEntry(iterators[dimensionIndex], dimensionIndex); 106 } 107 } 108 109 114 private Iterator iteratorFor(int dimensionIndex) { 115 HashMap table; 116 Object [] node; 117 118 node = (Object []) entryAt(dimensionIndex - 1).getValue(); 119 int cubeDimIndex = dimensions[dimensionIndex]; 120 table = (HashMap) node[cubeDimIndex]; 121 if (table != null) { 122 return cube.sortedIteratorFor(table, cubeDimIndex); 123 } 124 else { 125 return null; 126 } 127 } 128 129 134 private void updateEntry(Iterator iterator, int dimensionIndex) { 135 entryAtPut(dimensionIndex, (Map.Entry) iterator.next()); 136 } 137 138 141 public void advance() { 142 totals = new LinkedList(); 143 advanceAt(dimensionsSize - 1); 144 } 145 146 152 private void advanceAt(int dimensionIndex) { 153 154 if (dimensionIndex >= 0) { 155 if (iterators[dimensionIndex] != null) { 156 if (iterators[dimensionIndex].hasNext()) { 157 updateEntry(iterators[dimensionIndex], dimensionIndex); 158 return; 159 } 160 else { 161 iterators[dimensionIndex] = null; 162 totals.add(totalFrom(dimensionIndex - 1)); 163 } 164 } 165 advanceAt(dimensionIndex - 1); 166 initializeIterator(dimensionIndex); 167 } 168 } 169 170 178 private Object totalFrom(int dimensionIndex) { 179 Object [] result; 180 int i; 181 Object [] node; 182 183 node = (Object []) entryAt(dimensionIndex).getValue(); 184 result = new Object [node.length]; 185 for (i = 0; i < dimensionsSize; i++) { 186 if (node[i] instanceof HashMap) { 187 result[i] = null; 188 } 189 else { 190 result[i] = node[i]; 191 } 192 } 193 for (i = 0; i < metricsSize; i++) { 194 result[dimensionsSize + i] = node[metrics[i]]; 195 } 196 return result; 197 } 198 199 203 public boolean hasNext() { 204 Iterator iterator; 205 if (iterators.length == 0){ 206 return false; 207 } 208 if (iterators[0] == null) { 209 iterator = iteratorFor(0); 210 if (iterator != null) { 211 return iterator.hasNext(); 212 } 213 else { 214 return false; 215 } 216 } 217 for (int i = 0; i < dimensionsSize; i++) { 218 iterator = iterators[i]; 219 if (iterator.hasNext()) { 220 return true; 221 } 222 } 223 return false; 224 } 225 226 230 public Object [] current() { 231 Object [] result; 232 int i; 233 Object [] node; 234 235 result = new Object [dimensionsSize + metricsSize]; 236 for (i = 0; i < dimensionsSize; i++) { 237 result[i] = entryAt(i).getKey(); 238 } 239 node = (Object []) entryAt(dimensionsSize - 1).getValue(); 240 for (i = 0; i < metricsSize; i++) { 241 result[dimensionsSize + i] = node[metrics[i]]; 242 } 243 return result; 244 } 245 246 250 public Collection currentTotals() { 251 return totals; 252 } 253 } | Popular Tags |