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
This commit is contained in:
Emmanuel Bourg 2012-03-05 12:12:04 +00:00
parent c7f90f93c6
commit 312f5b033e
2 changed files with 17 additions and 7 deletions

View File

@ -17,6 +17,7 @@
package org.apache.commons.csv; package org.apache.commons.csv;
import java.io.Reader;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -198,6 +199,15 @@ public class CSVFormat implements Cloneable, Serializable {
return format; return format;
} }
/**
* Parses the specified content.
*
* @param in
*/
public Iterable<String[]> parse(Reader in) {
return new CSVParser(in, this);
}
protected CSVFormat clone() { protected CSVFormat clone() {
try { try {
return (CSVFormat) super.clone(); return (CSVFormat) super.clone();

View File

@ -573,22 +573,22 @@ public class CSVParserTest extends TestCase {
public void testForEach() { public void testForEach() {
List<String[]> records = new ArrayList<String[]>(); List<String[]> records = new ArrayList<String[]>();
String code = "a,b,c\n1,2,3\nx,y,z"; Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Reader in = new StringReader(code);
for (String[] record : new CSVParser(in)) { for (String[] record : CSVFormat.DEFAULT.parse(in)) {
records.add(record); records.add(record);
} }
assertEquals(3, records.size()); assertEquals(3, records.size());
assertTrue(Arrays.equals(new String[] {"a", "b", "c"}, records.get(0))); assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1))); assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
assertTrue(Arrays.equals(new String[] {"x", "y", "z"}, records.get(2))); assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
} }
public void testIterator() { public void testIterator() {
String code = "a,b,c\n1,2,3\nx,y,z"; Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<String[]> iterator = new CSVParser(new StringReader(code)).iterator();
Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext());
iterator.remove(); iterator.remove();