1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.DimensionValueNode; 4 5 import java.util.Iterator ; 6 import java.util.ArrayList ; 7 8 10 public class DimensionNodeIterator implements Iterator { 11 12 private DimensionValueNode dimensionValueNode; 13 private int dimensionCount; 14 private Iterator [] iterators; 15 private Object [] currentValues; 16 private DimensionValueNode[] currentNodes; 17 private boolean withTotals; 18 private ArrayList currentTotals; 19 20 public DimensionNodeIterator(DimensionValueNode dimensionValueNode, boolean withTotals) { 21 this.dimensionValueNode = dimensionValueNode; 22 this.withTotals = withTotals; 23 this.totalsPending = withTotals; 24 initialize(); 25 } 26 27 public boolean withTotals() { 28 return this.withTotals; 29 } 30 31 private void initialize() { 32 dimensionCount = Math.max(1, getDimensionValueNode().getDimensionCount()); 33 iterators = new Iterator [dimensionCount]; 34 currentValues = new Object [dimensionCount]; 35 currentNodes = new DimensionValueNode[dimensionCount]; 36 iterators[0] = getDimensionValueNode().getSubNodesList().iterator(); 37 } 38 39 public DimensionValueNode getDimensionValueNode() { 40 return dimensionValueNode; 41 } 42 43 public int getDimensionCount() { 44 return dimensionCount; 45 } 46 47 private ArrayList getCurrentTotals() { 48 if (currentTotals == null) { 49 currentTotals = new ArrayList (); 50 } 51 return currentTotals; 52 } 53 54 private boolean totalsPending; 55 56 public boolean hasNext() { 57 Iterator iterator; 58 for (int i = 0; i < dimensionCount; i++) { 59 iterator = iterators[i]; 60 if (iterator != null && iterator.hasNext()) { 61 return true; 62 } 63 } 64 if (withTotals) { 66 boolean xhasNext = totalsPending || currentValues[0] != null || getCurrentTotals().size() > 0; if (totalsPending) totalsPending = false; 68 return xhasNext; 69 } 70 else { 71 return false; 72 } 73 } 74 75 public Object next() { 76 Object [] values = new Object [getDimensionCount()]; 77 advance(); 78 if (getCurrentTotals().size() > 0) { 79 values = (Object []) getCurrentTotals().get(0); 80 getCurrentTotals().remove(0); 81 } 82 else { 83 copyFromCurrent(values); 84 } 85 return values; 86 } 87 88 private void copyFromCurrent(Object [] values) { 89 System.arraycopy(currentValues, 0, values, 0, currentValues.length); 90 } 91 92 93 private boolean itemPending = false; 94 95 private void advance() { 96 if (!itemPending) { 97 if (getCurrentTotals().size() == 0) { 98 advanceAt(getDimensionCount() - 1); 99 } 100 } 101 if (itemPending && getCurrentTotals().size() == 0) { 102 itemPending = false; 103 } 104 } 105 106 private void advanceAt(int dimensionIndex) { 107 if (dimensionIndex >= 0) { 108 Iterator iterator = iterators[dimensionIndex]; 109 if (iterator != null) { 110 if (iterator.hasNext()) { 111 updateCurrentValue(dimensionIndex, (DimensionValueNode) iterator.next()); 112 return; 113 } 114 else { 115 iterators[dimensionIndex] = null; 116 updateCurrentValue(dimensionIndex, null); 117 if (withTotals) { 118 Object [] values = new Object [getDimensionCount()]; 119 copyFromCurrent(values); 120 getCurrentTotals().add(values); 121 itemPending = true; 122 } 123 } 124 } 125 advanceAt(dimensionIndex - 1); 126 initializeIterator(dimensionIndex); 127 } 128 } 129 130 private void initializeIterator(int dimensionIndex) { 131 if (dimensionIndex > 0) { 132 Iterator iterator = iteratorFor(dimensionIndex); 133 if (iterator == null) { 134 initializeIterator(dimensionIndex - 1); 135 iterator = iteratorFor(dimensionIndex); 136 } 137 138 if (iterator != null && iterator.hasNext()) { 139 iterators[dimensionIndex] = iterator; 140 DimensionValueNode node = (DimensionValueNode) iterator.next(); 141 updateCurrentValue(dimensionIndex, node); 142 } 143 else { 144 iterators[dimensionIndex] = null; 145 } 146 } 147 else { 148 iterators[0] = null; 149 } 150 } 151 152 private Iterator iteratorFor(int dimensionIndex) { 153 DimensionValueNode node = currentNodes[dimensionIndex - 1]; 154 if (node != null) { 155 return node.iterator(); 156 } 157 return null; 158 } 159 160 private void updateCurrentValue(int dimensionIndex, DimensionValueNode node) { 161 Object value = null; 162 if (node != null) { 163 value = node.getValue(); 164 } 165 else { 166 value = null; 167 } 168 currentValues[dimensionIndex] = value; 169 currentNodes[dimensionIndex] = node; 170 if (dimensionIndex < currentValues.length - 1) { 171 for (int i = dimensionIndex + 1; i < currentValues.length; i++) { 172 currentValues[i] = null; 173 currentNodes[i] = null; 174 } 175 } 176 } 177 178 public void remove() { 179 } 180 } 181 | Popular Tags |