public OrderedMergeIterator(IteratorRegistry registry, CloseableIterator<LLEntry>[] iterators) { this.registry = registry; if (registry != null) { registry.registerIterator(this); } // init values currentValues = new ArrayList<LLEntry>(); for (int i = 0; i < iterators.length; i++) { CloseableIterator<LLEntry> iter = iterators[i]; if (iter.hasNext()) { this.iterators.add(iter); currentValues.add(iter.next()); } } // find smallest value if (!currentValues.isEmpty()) { int currentPos = 0; current = currentValues.get(0); for (int i = 1; i < currentValues.size(); i++) { if (current.getKey() > currentValues.get(i).getKey()) { currentPos = i; current = currentValues.get(i); } } // refill getNext(currentPos); } }
@Override public void close() { for (CloseableIterator<?> i : iterators) { i.close(); } if (registry != null) { registry.deregisterIterator(this); } }