package ciir.jfoley.chai.io;

import ciir.jfoley.chai.collections.iters.UntilNullGenerator;
import ciir.jfoley.chai.collections.iters.UntilNullIterator;

import java.io.*;
import java.util.Iterator;

/**
 * @author jfoley.
 */
public class LinesIterable implements Iterable<String>, AutoCloseable {

	private final BufferedReader reader;

	public LinesIterable(BufferedReader reader) {
		this.reader = reader;
	}

	@Override
	public Iterator<String> iterator() {
		return new UntilNullIterator<>(new UntilNullGenerator<String>() {
			@Override
			public String next() {
				try {
					return reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		});
	}

	@Override
	public void close() throws Exception {
		reader.close();
	}

	public static LinesIterable of(BufferedReader reader) {
		return new LinesIterable(reader);
	}
	public static LinesIterable of(Reader reader) {
		return new LinesIterable(new BufferedReader(reader));
	}
	public static LinesIterable of(String data) {
		return of(new StringReader(data));
	}

	public static LinesIterable fromFile(File path) throws FileNotFoundException {
		return of(new FileReader(path));
	}

	public static LinesIterable fromFile(String path) throws FileNotFoundException {
		return of(new FileReader(path));
	}
}

@@ -1108,36 +1108,26 @@
 ath) throws 
-FileNotFound
+IO
 Exception %7B%0A
@@ -1130,39 +1130,49 @@
 n %7B%0A
-%09%09
+    
 return 
-of(new FileReader(p
+fromFile(path.getAbsoluteP
 ath
+(
 ));%0A
@@ -1237,20 +1237,10 @@
 ows 
-FileNotFound
+IO
 Exce
@@ -1263,16 +1263,15 @@
  of(
-new File
+IO.open
 Read

Update LinesIterable to handle compressed streams