From 56d9c8d48f4934ade89db8d9f8eb231860dff3a4 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 23 Jun 2021 15:25:41 -0400 Subject: [PATCH] Add CSVRecord#toStream(). --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/csv/CSVRecord.java | 10 ++++++++++ .../java/org/apache/commons/csv/CSVRecordTest.java | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 388fa7df..cb6f25b1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -59,6 +59,7 @@ Add possibility to use ResultSet header meta data as CSV header #11. Make CSVRecord#toList() public. + Add CSVRecord#toStream(). Update org.junit.jupiter:junit-jupiter from 5.6.0 to 5.7.0, #84 #109 Update tests from Apache Commons Lang 3.9 to 3.12.0. diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index a8c86de4..ced1a9a2 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -308,6 +308,16 @@ public final class CSVRecord implements Serializable, Iterable { return putIn(new LinkedHashMap(values.length)); } + /** + * Returns a sequential ordered stream whose elements are the values. + * + * @return the new stream. + * @since 1.9.0 + */ + public Stream toStream() { + return Arrays.stream(values); + } + /** * Returns a string representation of the contents of this record. The result is constructed by comment, mapping, * recordNumber and by passing the internal values array to {@link Arrays#toString(Object[])}. diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 4bd1a40b..4f655f22 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -34,6 +34,7 @@ import java.util.Collections; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; @@ -296,6 +297,15 @@ public class CSVRecordTest { } } + @Test + public void testToStream() { + final AtomicInteger i = new AtomicInteger(); + record.toStream().forEach(value -> { + assertEquals(values[i.get()], value); + i.incrementAndGet(); + }); + } + @Test public void testToString() { assertNotNull(recordWithHeader.toString());