All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.util.iterator.CloseableCompositeIterator Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package org.archive.util.iterator;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

public class CloseableCompositeIterator implements CloseableIterator {

	protected LinkedList> iters;
	protected Iterator> iterPtr;
	protected CloseableIterator currIter;
	
    public CloseableCompositeIterator()
	{
		iters = new LinkedList>();
	}
	
	public void addFirst(CloseableIterator e)
	{
		iters.addFirst(e);
	}
	
	public void addLast(CloseableIterator e)
	{
		iters.addLast(e);
	}
	
	@Override
    public boolean hasNext() {
		
		if (iterPtr == null) {
			iterPtr = iters.iterator();
			currIter = iterPtr.next();
		}
		
		if (currIter == null) {
			return false;
		}
		
		while (currIter != null) {
			if (currIter.hasNext()) {
				return true;
			}
			
			currIter = (iterPtr.hasNext() ? iterPtr.next() : null);
		}
		
		return false;
    }

	@Override
    public E next() {
		return currIter.next();
    }

	@Override
    public void remove() {
		currIter.remove();
    }

	@Override
    public void close() throws IOException {
		for (CloseableIterator e : iters) {
			if (e != null) {
				try {
					e.close();
				} catch (IOException io) {
					
				}
			}
		}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy