1 17 package org.eclipse.emf.common.util; 18 19 20 import java.util.Iterator ; 21 22 23 29 public abstract class AbstractTreeIterator extends BasicEList implements TreeIterator 30 { 31 35 protected boolean includeRoot; 36 37 40 protected Object object; 41 42 45 protected Iterator nextPruneIterator; 46 47 50 protected Iterator nextRemoveIterator; 51 52 56 public AbstractTreeIterator(Object object) 57 { 58 this.object = object; 59 this.includeRoot = true; 60 } 61 62 66 public AbstractTreeIterator(Object object, boolean includeRoot) 67 { 68 this.object = object; 69 this.includeRoot = includeRoot; 70 } 71 72 77 protected abstract Iterator getChildren(Object object); 78 79 83 public boolean hasNext() 84 { 85 if (data == null && !includeRoot) 86 { 87 return hasAnyChildren(); 88 } 89 else 90 { 91 return hasMoreChildren(); 92 } 93 } 94 95 private boolean hasAnyChildren() 96 { 97 Iterator nextPruneIterator = this.nextPruneIterator; 98 99 nextPruneIterator = getChildren(object); 100 add(nextPruneIterator); 101 return nextPruneIterator.hasNext(); 102 } 103 104 private boolean hasMoreChildren() 105 { 106 return data == null || !isEmpty() && ((Iterator )data[size - 1]).hasNext(); 109 } 110 111 115 public Object next() 116 { 117 Object result; 118 119 if (data == null) 122 { 123 result = object; 126 nextPruneIterator = getChildren(object); 127 add(nextPruneIterator); 128 if (includeRoot) 129 { 130 return result; 131 } 132 } 133 134 Iterator currentIterator = (Iterator )data[size - 1]; 137 result = currentIterator.next(); 138 nextRemoveIterator = currentIterator; 139 140 Iterator iterator = getChildren(result); 143 if (iterator.hasNext()) 144 { 145 nextPruneIterator = iterator; 148 add(iterator); 149 } 150 else 151 { 152 nextPruneIterator = null; 155 156 while (!currentIterator.hasNext()) 159 { 160 data[--size] = null; 163 164 if (isEmpty()) 167 { 168 break; 169 } 170 171 currentIterator = (Iterator )data[size - 1]; 174 } 175 } 176 177 return result; 178 } 179 180 188 public void remove() 189 { 190 if (nextRemoveIterator == null) 191 { 192 throw new IllegalStateException ("There is no valid object to remove."); 193 } 194 nextRemoveIterator.remove(); 195 } 196 197 200 public void prune() 201 { 202 if (nextPruneIterator != null) 205 { 206 if (!isEmpty() && data[size - 1] == nextPruneIterator) 209 { 210 data[--size] = null; 213 214 while (!isEmpty() && !((Iterator )data[size - 1]).hasNext()) 217 { 218 data[--size] = null; 219 } 220 } 221 222 nextPruneIterator = null; 225 } 226 } 227 } 228 | Popular Tags |