Skip to content

Commit 312f5b0

Browse files
committed
Added a convenient parse() method to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297022 13f79535-47bb-0310-9956-ffa450edef68
1 parent c7f90f9 commit 312f5b0

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import java.io.Reader;
2021
import java.io.Serializable;
2122

2223
/**
@@ -198,6 +199,15 @@ public CSVFormat withLineSeparator(String lineSeparator) {
198199
return format;
199200
}
200201

202+
/**
203+
* Parses the specified content.
204+
*
205+
* @param in
206+
*/
207+
public Iterable<String[]> parse(Reader in) {
208+
return new CSVParser(in, this);
209+
}
210+
201211
protected CSVFormat clone() {
202212
try {
203213
return (CSVFormat) super.clone();

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,22 +573,22 @@ public void testDelimiterIsWhitespace() throws IOException {
573573
public void testForEach() {
574574
List<String[]> records = new ArrayList<String[]>();
575575

576-
String code = "a,b,c\n1,2,3\nx,y,z";
577-
Reader in = new StringReader(code);
576+
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
578577

579-
for (String[] record : new CSVParser(in)) {
578+
for (String[] record : CSVFormat.DEFAULT.parse(in)) {
580579
records.add(record);
581580
}
582581

583582
assertEquals(3, records.size());
584-
assertTrue(Arrays.equals(new String[] {"a", "b", "c"}, records.get(0)));
583+
assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
585584
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
586-
assertTrue(Arrays.equals(new String[] {"x", "y", "z"}, records.get(2)));
585+
assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
587586
}
588587

589588
public void testIterator() {
590-
String code = "a,b,c\n1,2,3\nx,y,z";
591-
Iterator<String[]> iterator = new CSVParser(new StringReader(code)).iterator();
589+
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
590+
591+
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
592592

593593
assertTrue(iterator.hasNext());
594594
iterator.remove();

0 commit comments

Comments
 (0)