Skip to content
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

feat: add FixedLength#parse(Reader) #37

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 22 additions & 7 deletions src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import name.velikodniy.vitaliy.fixedlength.annotation.SplitLineAfter;
import name.velikodniy.vitaliy.fixedlength.formatters.Formatter;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -290,21 +292,34 @@ public List<T> parse(InputStream stream) throws FixedLengthException {
return this.parseAsStream(stream).collect(Collectors.toList());
}

public List<T> parse(Reader reader) throws FixedLengthException {
return parseAsStream(reader).collect(Collectors.toList());
}

public Stream<T> parseAsStream(InputStream inputStream)
throws FixedLengthException {
Stream<String> lines = StreamSupport.stream(
Spliterators.spliterator(
new Scanner(inputStream, this.charset.name()).useDelimiter(this.delimiter),
Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.NONNULL
), false);

return parseAsStream(lines);
}

public Stream<T> parseAsStream(Reader reader) throws FixedLengthException {
return parseAsStream(new BufferedReader(reader).lines());
}

private Stream<T> parseAsStream(Stream<String> lines) throws FixedLengthException {
if (lineTypes.isEmpty()) {
throw new FixedLengthException(
"Specify at least one line type"
);
}

return StreamSupport.stream(
Spliterators.spliterator(
new Scanner(inputStream, this.charset.name()).useDelimiter(this.delimiter),
Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.NONNULL
), false)
.map(this::fixedFormatLine)
return lines.map(this::fixedFormatLine)
.filter(Objects::nonNull)
.flatMap(fixedFormatRecord -> lineToObjects(fixedFormatRecord).stream());
}
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -58,7 +59,6 @@ void testParseInheritedOneLineType() throws FixedLengthException {
assertNotNull(((InheritedEmployee)e).lastName);
});
}


@Test
@DisplayName("Parse as input stream with default charset and one line type")
Expand Down Expand Up @@ -178,4 +178,14 @@ void testParseMixedLineTypeCustomDelimiter() throws FixedLengthException {

assertEquals(3, parse.size());
}

@Test
@DisplayName("Parse as reader with default charset and one line type")
void testParseReaderWithDefaultCharset() throws FixedLengthException {
List<Row> parse = new FixedLength<Row>()
.registerLineType(Employee.class)
.parse(new StringReader(singleTypeExample));

assertEquals(2, parse.size());
}
}
Loading