commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1410045 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java test/java/org/apache/commons/csv/CSVFormatBuilderTest.java
Date Thu, 15 Nov 2012 21:56:37 GMT
Author: ggregory
Date: Thu Nov 15 21:56:36 2012
New Revision: 1410045

URL: http://svn.apache.org/viewvc?rev=1410045&view=rev
Log:
[CSV-68] Use the Builder pattern for CSVFormat.

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java

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=1410045&r1=1410044&r2=1410045&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 Nov 15
21:56:36 2012
@@ -84,7 +84,7 @@ public class CSVFormat implements Serial
      * For example for parsing or generating a CSV file on a French system the following
format will be used:
      *
      * <pre>
-     * CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
+     * CSVFormat fmt = CSVFormat.newBuilder(EXCEL).withDelimiter(';').build();
      * </pre>
      * Settings are:
      * <ul>
@@ -134,6 +134,10 @@ public class CSVFormat implements Serial
         return new CSVFormatBuilder(delimiter);
     }
 
+    public static CSVFormatBuilder newBuilder(final CSVFormat format) {
+        return new CSVFormatBuilder(format);
+    }
+    
     /**
      * Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
      * <ul>
@@ -422,6 +426,20 @@ public class CSVFormat implements Serial
             this.recordSeparator = lineSeparator;
             this.header = header;
         }
+        
+        /**
+         * 
+         * Creates a CSVFormatBuilder, using the values of the given CSVFormat.
+         * 
+         * @param format
+         *            The format to use values from
+         */
+        private CSVFormatBuilder(CSVFormat format) {
+            this(format.delimiter, format.quoteChar, format.quotePolicy,
+                    format.commentStart, format.escape,
+                    format.ignoreSurroundingSpaces, format.ignoreEmptyLines,
+                    format.recordSeparator, format.header);
+        }
 
         /**
          * Creates a basic CSVFormatBuilder.

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java?rev=1410045&r1=1410044&r2=1410045&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java
(original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java
Thu Nov 15 21:56:36 2012
@@ -17,11 +17,14 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.CSVFormat.RFC4180;
 import static org.apache.commons.csv.Constants.CR;
 import static org.apache.commons.csv.Constants.CRLF;
 import static org.apache.commons.csv.Constants.LF;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
 import org.junit.Before;
@@ -152,4 +155,29 @@ public class CSVFormatBuilderTest {
     public void testIgnoreEmptyLines() {
         assertFalse(builder.withIgnoreEmptyLines(false).build().getIgnoreEmptyLines());
     }
+    
+    @Test
+    public void testCopiedFormatIsEqualToOriginal() {
+        CSVFormat copyOfRCF4180 = CSVFormat.newBuilder(RFC4180).build();
+        assertEqualFormats(RFC4180, copyOfRCF4180);
+    }
+
+    @Test
+    public void testCopiedFormatWithChanges() {
+        CSVFormat newFormat = CSVFormat.newBuilder(RFC4180).withDelimiter('!').build();
+        assertTrue(newFormat.getDelimiter() != RFC4180.getDelimiter());
+    }
+    
+    // FIXME implement equals on CSVFormat to allow use of Assert.assertEquals()
+    private static void assertEqualFormats(CSVFormat expected, CSVFormat acutal) {
+        assertEquals(expected.getCommentStart(), acutal.getCommentStart());
+        assertEquals(expected.getDelimiter(), acutal.getDelimiter());
+        assertEquals(expected.getEscape(), acutal.getEscape());
+        assertArrayEquals(expected.getHeader(), acutal.getHeader());
+        assertEquals(expected.getIgnoreEmptyLines(), acutal.getIgnoreEmptyLines());
+        assertEquals(expected.getIgnoreSurroundingSpaces(), acutal.getIgnoreSurroundingSpaces());
+        assertEquals(expected.getQuoteChar(), acutal.getQuoteChar());
+        assertEquals(expected.getQuotePolicy(), acutal.getQuotePolicy());
+        assertEquals(expected.getRecordSeparator(), acutal.getRecordSeparator());
+    }
 }



Mime
View raw message