Skip to content

Commit

Permalink
implement writeRecord(Stream<String>)
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Dec 22, 2024
1 parent c5253d9 commit 847934e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add `writeRecord(Stream<String>)` to `CsvWriter` to write records from a stream of fields

## [3.4.0] - 2024-11-04
### Added
- Add `skipLines(int)` and `skipLines(Predicate<String>, int)` to `CsvReader` to skip lines before the actual CSV data starts
Expand Down Expand Up @@ -132,6 +136,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial release

[Unreleased]: https://github.com/osiegmar/FastCSV/compare/v3.4.0...HEAD
[3.4.0]: https://github.com/osiegmar/FastCSV/compare/v3.3.1...v3.4.0
[3.3.1]: https://github.com/osiegmar/FastCSV/compare/v3.3.0...v3.3.1
[3.3.0]: https://github.com/osiegmar/FastCSV/compare/v3.2.0...v3.3.0
Expand Down
5 changes: 5 additions & 0 deletions lib/src/intTest/java/blackbox/writer/CsvWriterRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -72,6 +73,10 @@ void openRecordWriteComment() {
assertThatThrownBy(() -> csv.writeRecord(List.of("foo")))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Record already started, call end() on CsvWriterRecord first");

assertThatThrownBy(() -> csv.writeRecord(Stream.of("foo")))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Record already started, call end() on CsvWriterRecord first");
}

}
22 changes: 17 additions & 5 deletions lib/src/intTest/java/blackbox/writer/CsvWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand Down Expand Up @@ -90,11 +89,14 @@ void oneLineTwoValues() {
}

@Test
void twoLinesTwoValues() {
final List<String> cols = new ArrayList<>();
cols.add("foo");
cols.add("bar");
void oneLineTwoValuesStream() {
assertThat(write(w -> w.writeRecord(Stream.of("foo", "bar"))))
.isEqualTo("foo,bar\n");
}

@Test
void twoLinesTwoValuesList() {
final List<String> cols = List.of("foo", "bar");
assertThat(write(w -> w.writeRecord(cols).writeRecord(cols)))
.isEqualTo("foo,bar\nfoo,bar\n");
}
Expand Down Expand Up @@ -241,7 +243,17 @@ void unwritableIterable() {
assertThatThrownBy(() -> crw.build(new UnwritableWriter()).writeRecord(List.of("foo")))
.isInstanceOf(UncheckedIOException.class)
.hasMessage("java.io.IOException: Cannot write");
}

@Test
void unwritableStream() {
assertThatThrownBy(() -> crw.build(new UnwritableWriter()).writeRecord(Stream.of("foo")))
.isInstanceOf(UncheckedIOException.class)
.hasMessage("java.io.IOException: Cannot write");
}

@Test
void unwritableComment() {
assertThatThrownBy(() -> crw.build(new UnwritableWriter()).writeComment("foo"))
.isInstanceOf(UncheckedIOException.class)
.hasMessage("java.io.IOException: Cannot write");
Expand Down
33 changes: 32 additions & 1 deletion lib/src/main/java/de/siegmar/fastcsv/writer/CsvWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.file.Path;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import de.siegmar.fastcsv.util.Preconditions;
import de.siegmar.fastcsv.util.Util;
Expand Down Expand Up @@ -85,11 +87,12 @@ public static CsvWriterBuilder builder() {
* @throws UncheckedIOException if a write-error occurs
* @throws IllegalStateException if a record is already started (by calling {@link #writeRecord()}) and not ended
* @see #writeRecord(String...)
* @see #writeRecord(Stream)
*/
public CsvWriter writeRecord(final Iterable<String> values) {
validateNoOpenRecord();
int fieldIdx = 0;
try {
int fieldIdx = 0;
for (final String value : values) {
writeInternal(value, fieldIdx++);
}
Expand All @@ -99,6 +102,34 @@ public CsvWriter writeRecord(final Iterable<String> values) {
}
}

/**
* Writes a complete line - one or more fields and new line character(s) at the end.
*
* @param values the fields to write ({@code null} values are handled as empty strings, if
* not configured otherwise ({@link QuoteStrategies#EMPTY}))
* @return This CsvWriter.
* @throws UncheckedIOException if a write-error occurs
* @throws IllegalStateException if a record is already started (by calling {@link #writeRecord()}) and not ended
* @see #writeRecord(String...)
* @see #writeRecord(Iterable)
*/
public CsvWriter writeRecord(final Stream<String> values) {
validateNoOpenRecord();
final AtomicInteger fieldIdx = new AtomicInteger();
try {
values.forEachOrdered(value -> {
try {
writeInternal(value, fieldIdx.getAndIncrement());
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
});
return endRecord();
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Writes a complete line - one or more fields and new line character(s) at the end.
*
Expand Down

0 comments on commit 847934e

Please sign in to comment.