1 package org.apache.ojb.broker.accesslayer; 2 3 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.ojb.broker.PersistenceBrokerException; 23 24 41 public class ChainingIterator implements OJBIterator 42 { 43 private List m_rsIterators = new ArrayList (); 44 private OJBIterator m_activeIterator = null; 45 46 53 private int m_activeIteratorIndex = 0; 54 private int m_fullSize = -1; 55 private int m_currentCursorPosition = 0; 56 57 private boolean disableLifeCycleEvents = false; 58 59 62 public ChainingIterator() 63 { 64 super(); 65 } 66 67 70 public ChainingIterator(List iterators) 71 { 72 Iterator checkIterator = iterators.iterator(); 73 OJBIterator temp; 74 75 79 while (checkIterator.hasNext()) 80 { 81 temp = (OJBIterator) checkIterator.next(); 82 addIterator(temp); 83 } 84 } 85 86 90 public void addIterator(OJBIterator iterator) 91 { 92 95 if (iterator != null) 96 { 97 if (iterator.hasNext()) 98 { 99 setNextIterator(); 100 m_rsIterators.add(iterator); 101 } 102 } 103 } 104 105 111 public int size() throws PersistenceBrokerException 112 { 113 if (m_fullSize == -1) 114 { 115 int size = 0; 116 Iterator it = m_rsIterators.iterator(); 117 while (it.hasNext()) 118 { 119 size += ((OJBIterator) it.next()).size(); 120 } 121 m_fullSize = size; 122 } 123 return m_fullSize; 124 } 125 126 129 public int fullSize() throws PersistenceBrokerException 130 { 131 return size(); 132 } 133 134 144 public boolean absolute(int row) throws PersistenceBrokerException 145 { 146 if (row == 0) 148 { 149 return true; 150 } 151 152 if (row == 1) 153 { 154 m_activeIteratorIndex = 0; 155 m_activeIterator = (OJBIterator) m_rsIterators.get(m_activeIteratorIndex); 156 m_activeIterator.absolute(1); 157 return true; 158 } 159 if (row == -1) 160 { 161 m_activeIteratorIndex = m_rsIterators.size(); 162 m_activeIterator = (OJBIterator) m_rsIterators.get(m_activeIteratorIndex); 163 m_activeIterator.absolute(-1); 164 return true; 165 } 166 167 boolean movedToAbsolute = false; 169 boolean retval = false; 170 setNextIterator(); 171 172 if (row > 0) 174 { 175 int sizeCount = 0; 176 Iterator it = m_rsIterators.iterator(); 177 OJBIterator temp = null; 178 while (it.hasNext() && !movedToAbsolute) 179 { 180 temp = (OJBIterator) it.next(); 181 if (temp.size() < row) 182 { 183 sizeCount += temp.size(); 184 } 185 else 186 { 187 m_currentCursorPosition = row - sizeCount; 189 retval = temp.absolute(m_currentCursorPosition); 190 movedToAbsolute = true; 191 } 192 } 193 194 } 195 196 else if (row < 0) 198 { 199 int sizeCount = 0; 200 OJBIterator temp = null; 201 for (int i = m_rsIterators.size(); ((i >= 0) && !movedToAbsolute); i--) 202 { 203 temp = (OJBIterator) m_rsIterators.get(i); 204 if (temp.size() < row) 205 { 206 sizeCount += temp.size(); 207 } 208 else 209 { 210 m_currentCursorPosition = row + sizeCount; 212 retval = temp.absolute(m_currentCursorPosition); 213 movedToAbsolute = true; 214 } 215 } 216 } 217 218 return retval; 219 } 220 221 236 public boolean relative(int row) throws PersistenceBrokerException 237 { 238 if (row == 0) 239 { 240 return true; 241 } 242 243 boolean movedToRelative = false; 244 boolean retval = false; 245 setNextIterator(); 246 247 if (row > 0) 248 { 249 if (row > (m_activeIterator.size() - m_currentCursorPosition)) 252 { 253 256 int positionCounter = m_activeIterator.size() - m_currentCursorPosition; 259 for (int i = m_activeIteratorIndex + 1; ((i < m_rsIterators.size()) && !movedToRelative); i++) 260 { 261 m_activeIteratorIndex = i; 262 m_currentCursorPosition = 0; 263 m_activeIterator = (OJBIterator) m_rsIterators.get(m_activeIteratorIndex); 264 if (!((row - positionCounter) > m_activeIterator.size())) 265 { 266 m_currentCursorPosition = row - positionCounter; 268 retval = m_activeIterator.relative(m_currentCursorPosition); 269 movedToRelative = true; 270 } 271 } 272 } 273 else 274 { 275 retval = m_activeIterator.relative(row); 277 movedToRelative = true; 278 } 279 } 280 281 return retval; 282 } 283 284 288 public void releaseDbResources() 289 { 290 Iterator it = m_rsIterators.iterator(); 291 while (it.hasNext()) 292 { 293 ((OJBIterator) it.next()).releaseDbResources(); 294 } 295 } 296 297 302 public boolean hasNext() 303 { 304 setNextIterator(); 305 if (m_activeIterator == null) 306 { 307 return false; 308 } 309 else 310 { 311 return m_activeIterator.hasNext(); 312 } 313 } 314 315 321 public Object next() 322 { 323 setNextIterator(); 324 m_currentCursorPosition++; 325 return m_activeIterator.next(); 326 } 327 328 public void remove() 329 { 330 setNextIterator(); 331 m_activeIterator.remove(); 332 } 333 334 338 private boolean setNextIterator() 339 { 340 boolean retval = false; 341 if (m_activeIterator == null) 343 { 344 if (m_rsIterators.size() > 0) 345 { 346 m_activeIteratorIndex = 0; 347 m_currentCursorPosition = 0; 348 m_activeIterator = (OJBIterator) m_rsIterators.get(m_activeIteratorIndex); 349 } 350 } 351 else if (!m_activeIterator.hasNext()) 352 { 353 if (m_rsIterators.size() > (m_activeIteratorIndex + 1)) 354 { 355 m_activeIteratorIndex++; 359 m_currentCursorPosition = 0; 360 m_activeIterator = (OJBIterator) m_rsIterators.get(m_activeIteratorIndex); 361 retval = true; 362 } 363 } 364 365 return retval; 366 } 367 368 373 public boolean containsIteratorForTable(String aTable) 374 { 375 boolean result = false; 376 377 if (m_rsIterators != null) 378 { 379 for (int i = 0; i < m_rsIterators.size(); i++) 380 { 381 OJBIterator it = (OJBIterator) m_rsIterators.get(i); 382 if (it instanceof RsIterator) 383 { 384 if (((RsIterator) it).getClassDescriptor().getFullTableName().equals(aTable)) 385 { 386 result = true; 387 break; 388 } 389 } 390 else if (it instanceof ChainingIterator) 391 { 392 result = ((ChainingIterator) it).containsIteratorForTable(aTable); 393 } 394 } 395 } 396 397 return result; 398 } 399 400 403 public void disableLifeCycleEvents() 404 { 405 Iterator iterators = m_rsIterators.iterator(); 406 while (iterators.hasNext()) 407 { 408 OJBIterator iter = (OJBIterator) iterators.next(); 409 iter.disableLifeCycleEvents(); 410 } 411 } 412 413 } 414 | Popular Tags |