Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions guava-tests/test/com/google/common/io/LineBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Unit tests for {@link LineBuffer} and {@link LineReader}.
Expand Down Expand Up @@ -74,6 +76,10 @@ public String apply(String value) {
assertEquals(expectRead, readUsingJava(input, chunk));
assertEquals(expectRead, readUsingReader(input, chunk, true));
assertEquals(expectRead, readUsingReader(input, chunk, false));
assertTrue(expectRead.containsAll(readUsingReaderGetLines(input, chunk, true)
.collect(Collectors.toList())));
assertTrue(expectRead.containsAll(readUsingReaderGetLines(input, chunk, false)
.collect(Collectors.toList())));
}
}

Expand Down Expand Up @@ -121,6 +127,13 @@ private static List<String> readUsingReader(String input, int chunk, boolean asR
return lines;
}

private static Stream<String> readUsingReaderGetLines(String input, int chunk, boolean asReader) {
Readable readable =
asReader ? getChunkedReader(input, chunk) : getChunkedReadable(input, chunk);

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

Suggested change
asReader ? getChunkedReader(input, chunk) : getChunkedReadable(input, chunk);
Readable readable = asReader
? getChunkedReader(input, chunk)
: getChunkedReadable(input, chunk);

Copy link
Contributor Author

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.

LineReader r = new LineReader(readable);
return r.lines();
}

// Returns a Readable that is *not* a Reader.
private static Readable getChunkedReadable(String input, int chunk) {
final Reader reader = getChunkedReader(input, chunk);
Expand Down
61 changes: 55 additions & 6 deletions guava/src/com/google/common/io/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,29 @@

package com.google.common.io;

import static com.google.common.base.Preconditions.checkNotNull;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you chance the import order accidentally? Google Java Styleguide

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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>() {

Choose a reason for hiding this comment

The 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()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !nextLine.equals("") condition should be already covered by the hasNext() right?

String line = nextLine;
nextLine = "";
return line;
} else {
throw new NoSuchElementException();
}
}
};
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
iterator, Spliterator.ORDERED | Spliterator.NONNULL), false);
}
}