package speiger.src.coreengine.utils.collections.iterators;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ReaderIterator implements Iterator<String> {
	BufferedReader reader;
	String value = null;
	
	public ReaderIterator(Reader reader) {
		this(new BufferedReader(reader));
	}
	
	public ReaderIterator(InputStream stream) {
		this(new InputStreamReader(stream));
	}
	
	public ReaderIterator(BufferedReader reader) {
		this.reader = reader;
	}
	
	@Override
	public boolean hasNext() {
		if(value != null) return true;
		try { value = reader.readLine(); }
		catch(IOException e) {
			e.printStackTrace();
			return false;
		}
		return value != null;
	}
	
	@Override
	public String next() {
		if(!hasNext()) throw new NoSuchElementException();
		String result = value;
		value = null;
		return result;
	}
	
}
