maidh91 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Have LineReader implement Iterator<String> #405

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be handy if LineReader implemented Iterator<String>, so you could 
directly use the various guava collections methods to search and filter the 
lines of the file.

IOExceptions that occur in the next() or hasNext() method could be rethrown as 
NoSuchElementExceptions; the original IOException could be accessed by a 
getException() method.

The Files.readLines(File, Charset, LineProcessor) method provides some of this 
ability, but making LineReader a String iterator would allow constructions like:

Predicate<String> LINE_MATCHES = new Predicate<String>(){..}
List<String> summary = Iterators.filter(new LineReader(file), LINE_MATCHES);

Combine this with a Matcher -> Predicate adapter and you could schlep out any 
lines of interest very easily.  (see: 
http://code.google.com/p/hamcrest/issues/detail?id=94)

Thanks,
Ed

Original issue reported on code.google.com by edbe...@gmail.com on 24 Aug 2010 at 4:21

GoogleCodeExporter commented 9 years ago
To know whether you have a next element you'd need to read the line in 
hasNext(), but hasNext() should never throw an exception. Wrapping the 
IOException in an unchecked exception seems like a bad idea. (Although, if you 
did, it could set as the 'cause' and accessed via getCause(), not a new special 
getException() method.)

I think one of two concessions would have to be made:
- Have a new sub-interface of Iterator that can throw a checked exception on 
hasNext().
- Or, have a static method somewhere that returns an Iterable<String> from a 
Readable. That method throws IOException.

Original comment by ray.j.gr...@gmail.com on 26 Aug 2010 at 6:38

GoogleCodeExporter commented 9 years ago
And... what was I thinking: you can't have a sub-interface that adds a checked 
Exception to a method.

So, number 2 is the winner. I realize this blows your idea of being able to 
stop iteration prior to the entire file being read in.

Or: and perhaps this is what you were suggesting all along: LineReader is an 
Iterator, if an IOException occurs then hasNext() returns false. At the end of 
iteration you must call a special getException() method on your LineReader to 
see if there was an error. I think that's un-Java-like. Exceptions should be 
thrown, not manually checked for.

Original comment by ray.j.gr...@gmail.com on 26 Aug 2010 at 6:59

GoogleCodeExporter commented 9 years ago
>Or: and perhaps this is what you were suggesting all along: LineReader is an 
Iterator, if an IOException occurs then hasNext() returns false.
It was.  I'd forgotten that hasNext() doesn't throw an exception.  
>I think that's un-Java-like. Exceptions should be thrown, not manually checked 
for.
You're probably right.  I'd thought there was some advantage to not having to 
read the entire file as a list (which guava has a method to do already), but 
the user would always have to take steps to determine why hasNext() returned 
false: because the iteration ended without error, or something bad happened.
Thanks for thinking about this.

Original comment by edbe...@gmail.com on 28 Aug 2010 at 11:57

GoogleCodeExporter commented 9 years ago
Thanks for the suggestion and discussion, guys.  Iterators that might throw 
exceptions, and are backed by a stream that will need to be closed, are highly 
problematic, and we don't have a good solution for this.

Original comment by kevinb@google.com on 8 Sep 2010 at 6:06

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09