
org.zoodb.jdo.internal.util.MergingIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of parent Show documentation
Show all versions of parent Show documentation
ZooDB Java JDO Object Database.
The newest version!
/*
* Copyright 2009-2013 Tilmann Zaeschke. All rights reserved.
*
* This file is part of ZooDB.
*
* ZooDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ZooDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ZooDB. If not, see .
*
* See the README and COPYING files for further information.
*/
package org.zoodb.jdo.internal.util;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
/**
* This merging iterator merges multiple iterators into a single one.
*
* TODO For queries across multiple nodes, merge asynchronously by running sub-iterators in
* different threads and merge the result as they arrive.
*
* @author Tilmann Zaeschke
*
* @param
*/
public class MergingIterator implements CloseableIterator {
private final List> iterators = new LinkedList>();
private CloseableIterator current;
private final IteratorRegistry registry;
public MergingIterator() {
this.registry = null;
}
public MergingIterator(IteratorRegistry registry) {
this.registry = registry;
registry.registerIterator(this);
}
@Override
public boolean hasNext() {
if (current == null) {
return false;
}
while (!current.hasNext()) {
if (iterators.isEmpty()) {
current = null;
return false;
}
current.close();
current = iterators.remove(0);
}
return true;
}
@Override
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return current.next();
}
@Override
public void remove() {
current.remove();
}
public void add(CloseableIterator it) {
iterators.add(it);
if (current == null) {
current = it;
}
}
@Override
public void close() {
if (current != null) {
current.close();
}
for (CloseableIterator i: iterators) {
i.close();
}
if (registry != null) {
registry.deregisterIterator(this);
}
}
@Override
public void refresh() {
for (CloseableIterator> it: iterators) {
it.refresh();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy