You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2014/08/28 02:28:03 UTC

svn commit: r1621004 - in /commons/proper/csv/trunk/src: changes/ main/java/org/apache/commons/csv/ test/java/org/apache/commons/csv/

Author: ggregory
Date: Thu Aug 28 00:28:02 2014
New Revision: 1621004

URL: http://svn.apache.org/r1621004
Log:
[CSV-129] Add CSVFormat#with 0-arg methods matching boolean arg methods

Modified:
    commons/proper/csv/trunk/src/changes/changes.xml
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java

Modified: commons/proper/csv/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1621004&r1=1621003&r2=1621004&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Thu Aug 28 00:28:02 2014
@@ -39,7 +39,8 @@
   </properties>
   <body>
     <release version="1.1" date="2014-mm-dd" description="Feature and bug fix release">
-      <action issue="CSV-128" type="fix" dev="britter">CSVFormat.EXCEL should ignore empty header names</action>
+      <action issue="CSV-128" type="fix" dev="ggregory">CSVFormat.EXCEL should ignore empty header names</action>
+      <action issue="CSV-129" type="add" dev="ggregory">Add CSVFormat#with 0-arg methods matching boolean arg methods</action>
     </release>
     <release version="1.0" date="2014-08-14" description="First release">
       <action issue="CSV-125" type="fix" dev="britter">No longer works with Java 6</action>

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1621004&r1=1621003&r2=1621004&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Thu Aug 28 00:28:02 2014
@@ -216,7 +216,7 @@ public final class CSVFormat implements 
      * Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}.
      * </p>
      */
-    public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(true);
+    public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames();
 
     /**
      * Tab-delimited format.
@@ -234,7 +234,7 @@ public final class CSVFormat implements 
     public static final CSVFormat TDF =
             DEFAULT
             .withDelimiter(TAB)
-            .withIgnoreSurroundingSpaces(true);
+            .withIgnoreSurroundingSpaces();
 
     /**
      * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations.
@@ -852,6 +852,17 @@ public final class CSVFormat implements 
     }
 
     /**
+     * Sets the missing column names behavior of the format to {@code true}
+     *
+     * @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
+     * @see #withAllowMissingColumnNames(boolean)
+     * @since 1.1
+     */
+    public CSVFormat withAllowMissingColumnNames() {
+        return this.withAllowMissingColumnNames(true);
+    }
+
+    /**
      * Sets the missing column names behavior of the format.
      *
      * @param allowMissingColumnNames
@@ -866,6 +877,17 @@ public final class CSVFormat implements 
     }
 
     /**
+     * Sets the empty line skipping behavior of the format to {@code true}.
+     *
+     * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
+     * @since {@link #withIgnoreEmptyLines(boolean)}
+     * @since 1.1
+     */
+    public CSVFormat withIgnoreEmptyLines() {
+        return this.withIgnoreEmptyLines(true);
+    }
+
+    /**
      * Sets the empty line skipping behavior of the format.
      *
      * @param ignoreEmptyLines
@@ -880,6 +902,17 @@ public final class CSVFormat implements 
     }
 
     /**
+     * Sets the trimming behavior of the format to {@code true}.
+     *
+     * @return A new CSVFormat that is equal to this but with the specified trimming behavior.
+     * @see #withIgnoreSurroundingSpaces(boolean)
+     * @since 1.1
+     */
+    public CSVFormat withIgnoreSurroundingSpaces() {
+        return this.withIgnoreSurroundingSpaces(true);
+    }
+
+    /**
      * Sets the trimming behavior of the format.
      *
      * @param ignoreSurroundingSpaces
@@ -994,6 +1027,21 @@ public final class CSVFormat implements 
     }
 
     /**
+     * Sets skipping the header record to {@code true}.
+     *
+     * @param skipHeaderRecord
+     *            whether to skip the header record.
+     *
+     * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
+     * @see #withSkipHeaderRecord(boolean)
+     * @see #withHeader(String...)
+     * @since 1.1
+     */
+    public CSVFormat withSkipHeaderRecord() {
+        return this.withSkipHeaderRecord(true);
+    }
+
+    /**
      * Sets whether to skip the header record.
      *
      * @param skipHeaderRecord

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1621004&r1=1621003&r2=1621004&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Thu Aug 28 00:28:02 2014
@@ -123,8 +123,8 @@ public class CSVFormatTest {
                 .withCommentMarker('#')
                 .withEscape('+')
                 .withHeader("One", "Two", "Three")
-                .withIgnoreEmptyLines(true)
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreEmptyLines()
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL);
         final CSVFormat left = right
@@ -138,8 +138,8 @@ public class CSVFormatTest {
         final CSVFormat right = CSVFormat.newFormat('\'')
                 .withCommentMarker('#')
                 .withEscape('+')
-                .withIgnoreEmptyLines(true)
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreEmptyLines()
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL);
         final CSVFormat left = right
@@ -153,7 +153,7 @@ public class CSVFormatTest {
         final CSVFormat right = CSVFormat.newFormat('\'')
                 .withCommentMarker('#')
                 .withEscape('+')
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL);
         final CSVFormat left = right
@@ -187,8 +187,8 @@ public class CSVFormatTest {
                 .withRecordSeparator(CR)
                 .withCommentMarker('#')
                 .withEscape('+')
-                .withIgnoreEmptyLines(true)
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreEmptyLines()
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL);
         final CSVFormat left = right
@@ -203,8 +203,8 @@ public class CSVFormatTest {
                 .withRecordSeparator(CR)
                 .withCommentMarker('#')
                 .withEscape('+')
-                .withIgnoreEmptyLines(true)
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreEmptyLines()
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL)
                 .withNullString("null");
@@ -220,12 +220,12 @@ public class CSVFormatTest {
                 .withRecordSeparator(CR)
                 .withCommentMarker('#')
                 .withEscape('+')
-                .withIgnoreEmptyLines(true)
-                .withIgnoreSurroundingSpaces(true)
+                .withIgnoreEmptyLines()
+                .withIgnoreSurroundingSpaces()
                 .withQuote('"')
                 .withQuoteMode(QuoteMode.ALL)
                 .withNullString("null")
-                .withSkipHeaderRecord(true);
+                .withSkipHeaderRecord();
         final CSVFormat left = right
                 .withSkipHeaderRecord(false);
 
@@ -267,7 +267,7 @@ public class CSVFormatTest {
 
     @Test
     public void testNullRecordSeparatorCsv106() {
-        final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord(true).withHeader("H1", "H2");
+        final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2");
         final String formatStr = format.format("A", "B");
         assertNotNull(formatStr);
         assertFalse(formatStr.endsWith("null"));
@@ -377,13 +377,13 @@ public class CSVFormatTest {
     @Test
     public void testWithIgnoreEmptyLines() throws Exception {
         assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
-        assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines());
+        assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
     }
 
     @Test
     public void testWithIgnoreSurround() throws Exception {
         assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
-        assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces());
+        assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
     }
 
     @Test

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1621004&r1=1621003&r2=1621004&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Thu Aug 28 00:28:02 2014
@@ -100,7 +100,7 @@ public class CSVParserTest {
                 { "   8   ", "   \"quoted \"\" /\" / string\"   " }, { "9", "   \n   " }, };
 
         final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
-                .withIgnoreEmptyLines(true);
+                .withIgnoreEmptyLines();
 
         final CSVParser parser = CSVParser.parse(code, format);
         final List<CSVRecord> records = parser.getRecords();
@@ -127,7 +127,7 @@ public class CSVParserTest {
         };
 
         final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
-                .withIgnoreEmptyLines(true);
+                .withIgnoreEmptyLines();
 
         final CSVParser parser = CSVParser.parse(code, format);
         final List<CSVRecord> records = parser.getRecords();
@@ -299,6 +299,23 @@ public class CSVParserTest {
         }
     }
 
+//    @Test
+//    public void testStartWithEmptyLinesThenHeaders() throws Exception {
+//        final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" };
+//        final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
+//                { "" } };
+//        for (final String code : codes) {
+//            final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
+//            final List<CSVRecord> records = parser.getRecords();
+//            assertEquals(res.length, records.size());
+//            assertTrue(records.size() > 0);
+//            for (int i = 0; i < res.length; i++) {
+//                assertArrayEquals(res[i], records.get(i).values());
+//            }
+//            parser.close();
+//        }
+//    }
+
     @Test
     public void testEndOfFileBehaviorCSV() throws Exception {
         final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n",
@@ -433,7 +450,7 @@ public class CSVParserTest {
 
     @Test
     public void testGetLine() throws IOException {
-        final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+        final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
         for (final String[] re : RESULT) {
             assertArrayEquals(re, parser.nextRecord().values());
         }
@@ -507,7 +524,7 @@ public class CSVParserTest {
 
     @Test
     public void testGetRecords() throws IOException {
-        final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+        final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
         final List<CSVRecord> records = parser.getRecords();
         assertEquals(RESULT.length, records.size());
         assertTrue(records.size() > 0);
@@ -584,13 +601,13 @@ public class CSVParserTest {
     @Test
     public void testHeadersMissing() throws Exception {
         final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
-        CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true).parse(in).iterator();
+        CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator();
     }
 
     @Test
     public void testHeaderMissingWithNull() throws Exception {
         final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
-        CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames(true).parse(in).iterator();
+        CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator();
     }
 
     @Test
@@ -668,7 +685,7 @@ public class CSVParserTest {
     @Test
     public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
         final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
-        final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord(true)
+        final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord()
                 .parse(in).iterator();
         CSVRecord record;
 
@@ -841,7 +858,7 @@ public class CSVParserTest {
     @Test
     public void testSkipSetHeader() throws Exception {
         final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
-        final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord(true)
+        final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord()
                 .parse(in).iterator();
         final CSVRecord record = records.next();
         assertEquals("1", record.get("a"));

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java?rev=1621004&r1=1621003&r2=1621004&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/LexerTest.java Thu Aug 28 00:28:02 2014
@@ -59,7 +59,7 @@ public class LexerTest {
     @Test
     public void testSurroundingSpacesAreDeleted() throws IOException {
         final String code = "noSpaces,  leadingSpaces,trailingSpaces  ,  surroundingSpaces  ,  ,,";
-        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
@@ -72,7 +72,7 @@ public class LexerTest {
     @Test
     public void testSurroundingTabsAreDeleted() throws IOException {
         final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
-        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
@@ -98,7 +98,7 @@ public class LexerTest {
                 "\n"+
                 "\n"+
                 "\n";
-        final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(true);
+        final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
         final Lexer parser = getLexer(code, format);
 
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
@@ -241,7 +241,7 @@ public class LexerTest {
         *        a,  " foo " ,b
         */
         final String code = "a,\"foo\",b\na,   \" foo\",b\na,\"foo \"  ,b\na,  \" foo \"  ,b";
-        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
+        final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
         assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
         assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));