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

fr.profi.mzdb.util.iterator.BufferedIterator Maven / Gradle / Ivy

There is a newer version: 0.0.27
Show newest version
package fr.profi.mzdb.util.iterator;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class BufferedIterator implements Iterator {

	private Iterator source;
	private int max;
	private LinkedList queue;
	private E nextReturn;
	private Object done = new Object();

	public BufferedIterator(Iterator src, int m) {

		max = m;
		source = src;
		queue = new LinkedList();

		(new Thread("BufferedIterator Filler") {

			public void run() {

				while (source.hasNext()) {

					E next = source.next();

					synchronized (queue) {

						while (queue.size() >= max) {

							try {

								queue.wait();
							} catch (InterruptedException doh) {

								doh.printStackTrace();

								return; // something went wrong

							}

						}

						queue.add(next);

						queue.notify();

					}

				}

				synchronized (queue) {

					queue.add(done);

					queue.notify();

				}

			}

		}).start();

	}

	@SuppressWarnings("unchecked")
	public synchronized boolean hasNext() {

		while (nextReturn == null) {

			synchronized (queue) {

				while (queue.isEmpty()) {

					try {

						queue.wait();

					} catch (InterruptedException doh) {

						doh.printStackTrace();

						return false; // something went wrong

					}

				}

				nextReturn = (E) queue.removeFirst();

				queue.notify();

				if (nextReturn == done) {

					return false;

				}

			}

		}

		return true;

	}

	public synchronized E next() {

		if (!hasNext()) {

			throw new NoSuchElementException();

		}

		E retVal = nextReturn;

		nextReturn = null;

		return retVal;

	}

	public void remove() {

		throw new UnsupportedOperationException("Unsupported operation.");

	}

}