-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added lines() in LineReader #6308
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,22 @@ | |
|
||
import com.google.common.annotations.Beta; | ||
import com.google.common.annotations.GwtIncompatible; | ||
import com.google.common.collect.AbstractIterator; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import com.google.errorprone.annotations.MustBeClosed; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.CharBuffer; | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import javax.annotation.CheckForNull; | ||
|
||
/** | ||
|
@@ -84,4 +94,50 @@ public String readLine() throws IOException { | |
} | ||
return lines.poll(); | ||
} | ||
|
||
/** | ||
* Returns a {@code Stream}, the elements of which are lines read from this {@code LineReader}. | ||
* | ||
* <p>The returned stream is lazy and only reads from the source in the terminal operation. If an | ||
* I/O error occurs while the stream is reading from the source or when the stream is closed, an | ||
* {@link UncheckedIOException} is thrown. | ||
* | ||
* <p>Like {@link LineReader#readLine()}, this method reads a line of text. A line is | ||
* considered to be terminated by any one of a line feed ({@code '\n'}), a carriage return | ||
* ({@code '\r'}), or a carriage return followed immediately by a linefeed ({@code "\r\n"}). | ||
* | ||
* <p>The caller is responsible for ensuring that the returned stream is closed. For example: | ||
* | ||
* <pre>{@code | ||
* try (Stream<String> lines = source.lines()) { | ||
* lines.map(...) | ||
* .filter(...) | ||
* .forEach(...); | ||
* } | ||
* }</pre> | ||
* | ||
* @return a {@code Stream<String>} providing the lines of text | ||
* described by this {@code LineReader} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the JavaDoc should:
|
||
* @throws UncheckedIOException if an I/O error occurs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method itself does not throw |
||
*/ | ||
@MustBeClosed | ||
public Stream<String> lines() { | ||
AbstractIterator<String> iterator = new AbstractIterator<String>() { | ||
@CheckForNull | ||
@Override | ||
protected String computeNext() { | ||
try { | ||
String next = readLine(); | ||
if (next != null) { | ||
return next; | ||
} | ||
return endOfData(); | ||
} catch (IOException exception) { | ||
throw new UncheckedIOException(exception); | ||
} | ||
} | ||
}; | ||
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( | ||
iterator, Spliterator.ORDERED | Spliterator.NONNULL), false); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When looking through the code base, this is the way they split the ternary operator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referred this function
readUsingReader
.