1 25 38 package org.jgrapht.util; 39 40 import java.util.*; 41 42 43 89 public class PrefetchIterator<E> 90 implements Iterator<E>, Enumeration<E> 91 { 92 93 95 private NextElementFunctor<E> innerEnum; 96 private E getNextLastResult; 97 private boolean isGetNextLastResultUpToDate = false; 98 private boolean endOfEnumerationReached = false; 99 private boolean flagIsEnumerationStartedEmpty = true; 100 private int innerFunctorUsageCounter = 0; 101 102 104 public PrefetchIterator(NextElementFunctor<E> aEnum) 105 { 106 innerEnum = aEnum; 107 } 108 109 111 115 private E getNextElementFromInnerFunctor() 116 { 117 innerFunctorUsageCounter++; 118 E result = this.innerEnum.nextElement(); 119 120 flagIsEnumerationStartedEmpty = false; 123 return result; 124 } 125 126 131 public E nextElement() 132 { 133 E result = null; 134 if (this.isGetNextLastResultUpToDate) { 135 result = this.getNextLastResult; 136 } else { 137 result = getNextElementFromInnerFunctor(); 138 } 139 140 this.isGetNextLastResultUpToDate = false; 141 return result; 142 } 143 144 148 public boolean hasMoreElements() 149 { 150 if (endOfEnumerationReached) { 151 return false; 152 } 153 154 if (isGetNextLastResultUpToDate) { 155 return true; 156 } else { 157 try { 158 this.getNextLastResult = getNextElementFromInnerFunctor(); 159 this.isGetNextLastResultUpToDate = true; 160 return true; 161 } catch (NoSuchElementException noSuchE) { 162 endOfEnumerationReached = true; 163 return false; 164 } 165 } } 168 174 public boolean isEnumerationStartedEmpty() 175 { 176 if (this.innerFunctorUsageCounter == 0) { 177 if (hasMoreElements()) { 178 return false; 179 } else { 180 return true; 181 } 182 } else { 186 return flagIsEnumerationStartedEmpty; 187 } 188 } 189 190 public boolean hasNext() 191 { 192 return this.hasMoreElements(); 193 } 194 195 public E next() 196 { 197 return this.nextElement(); 198 } 199 200 203 public void remove() 204 throws UnsupportedOperationException  205 { 206 throw new UnsupportedOperationException (); 207 } 208 209 211 public interface NextElementFunctor<EE> 212 { 213 217 public EE nextElement() 218 throws NoSuchElementException; 219 } 220 } 221 | Popular Tags |