1 19 20 package org.openidex.search; 21 22 import java.util.Iterator ; 23 import java.util.NoSuchElementException ; 24 25 29 class CompoundSearchIterator implements Iterator { 30 31 32 private final SearchInfo[] elements; 33 34 private int elementIndex; 35 36 private Iterator elementIterator; 37 38 private Object nextObject; 39 40 private boolean upToDate; 41 42 49 CompoundSearchIterator(SearchInfo[] elements) { 50 if (elements == null) { 51 throw new IllegalArgumentException (); 52 } 53 54 if (elements.length == 0) { 55 this.elements = null; 56 elementIndex = 0; 57 upToDate = true; } else { 59 this.elements = elements; 60 elementIterator = elements[elementIndex = 0].objectsToSearch(); 61 upToDate = false; 62 } 63 } 64 65 67 public boolean hasNext() { 68 if (!upToDate) { 69 update(); 70 } 71 return elementIndex < elements.length; 72 } 73 74 76 public Object next() { 77 if (!hasNext()) { 78 throw new NoSuchElementException (); 79 } 80 81 upToDate = false; 82 return nextObject; 83 } 84 85 87 private void update() { 88 assert upToDate == false; 89 90 while (!elementIterator.hasNext()) { 91 elements[elementIndex] = null; 92 93 if (++elementIndex == elements.length) { 94 break; 95 } 96 97 elementIterator = elements[elementIndex].objectsToSearch(); 98 } 99 100 if (elementIndex < elements.length) { 101 nextObject = elementIterator.next(); 102 } else { 103 elementIterator = null; 104 nextObject = null; 105 } 106 107 upToDate = true; 108 } 109 110 114 public void remove() { 115 throw new UnsupportedOperationException (); 116 } 117 118 } 119 | Popular Tags |