-
Notifications
You must be signed in to change notification settings - Fork 11k
added lines() in LineReader #6308
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,29 @@ | |
|
||
package com.google.common.io; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
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. Did you chance the import order accidentally? Google Java Styleguide 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. yes, fixed. |
||
import static com.google.common.io.CharStreams.createBuffer; | ||
|
||
import com.google.common.annotations.Beta; | ||
import com.google.common.annotations.GwtIncompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.CharBuffer; | ||
import java.util.ArrayDeque; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
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; | ||
|
||
import com.google.common.annotations.Beta; | ||
import com.google.common.annotations.GwtIncompatible; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.io.CharStreams.createBuffer; | ||
|
||
/** | ||
* A class for reading lines of text. Provides the same functionality as {@link | ||
* java.io.BufferedReader#readLine()} but for all {@link Readable} objects, not just instances of | ||
|
@@ -84,4 +94,43 @@ public String readLine() throws IOException { | |
} | ||
return lines.poll(); | ||
} | ||
|
||
/** | ||
* Returns a {@code Stream}, the elements of which are lines read from | ||
* this {@code LineReader}. | ||
* | ||
* @return a {@code Stream<String>} providing the lines of text | ||
* described by this {@code LineReader} | ||
*/ | ||
public Stream<String> lines() | ||
{ | ||
Iterator<String> iterator = new Iterator<String>() { | ||
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. Perhaps using the AbstractIterator would make the logic quite a bit simpler |
||
String nextLine = ""; | ||
@Override | ||
public boolean hasNext() { | ||
if (!nextLine.equals("")) { | ||
return true; | ||
} else { | ||
try { | ||
nextLine = Optional.ofNullable(readLine()).orElse(""); | ||
return (!nextLine.equals("")); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
@Override | ||
public String next() { | ||
if (!nextLine.equals("") || hasNext()) { | ||
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 |
||
String line = nextLine; | ||
nextLine = ""; | ||
return line; | ||
} else { | ||
throw new NoSuchElementException(); | ||
} | ||
} | ||
}; | ||
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( | ||
iterator, Spliterator.ORDERED | Spliterator.NONNULL), false); | ||
} | ||
} |
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
.