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 2012/10/12 14:35:47 UTC

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

Author: ggregory
Date: Fri Oct 12 12:35:46 2012
New Revision: 1397541

URL: http://svn.apache.org/viewvc?rev=1397541&view=rev
Log:
Refactor constants from various classes into a package private Constants class and use static imports.

Added:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java   (with props)
Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Lexer.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer1.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer3.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.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=1397541&r1=1397540&r2=1397541&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 Fri Oct 12 12:35:46 2012
@@ -17,6 +17,13 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.COMMA;
+import static org.apache.commons.csv.Constants.CR;
+import static org.apache.commons.csv.Constants.DOUBLE_QUOTE;
+import static org.apache.commons.csv.Constants.ESCAPE;
+import static org.apache.commons.csv.Constants.LF;
+import static org.apache.commons.csv.Constants.TAB;
+
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Serializable;
@@ -29,20 +36,12 @@ import java.io.StringWriter;
  */
 public class CSVFormat implements Serializable {
 
-    private static final String LF = "\n";
-
-    private static final char ESCAPE = '\\';
-
-    private static final char TAB = '\t';
-
-    private static final char DOUBLE_QUOTE = '"';
-
-    private static final char COMMA = ',';
+    private static final String LF_STR = "" + LF;
 
     private static final long serialVersionUID = 1L;
 
     /** According to RFC 4180, line breaks are delimited by CRLF */
-    public static final String CRLF = "\r\n";
+    public static final String CRLF = "" + CR + LF;
 
     private final char delimiter;
     private final char encapsulator;
@@ -127,7 +126,7 @@ public class CSVFormat implements Serial
 
     /**
      * Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and <tt>LOAD DATA INFILE</tt> operations. This is
-     * a tab-delimited format with a LF character as the line separator. Values are not quoted and special characters
+     * a tab-delimited format with a LF_STR character as the line separator. Values are not quoted and special characters
      * are escaped with '\'.
      *
      * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">
@@ -137,7 +136,7 @@ public class CSVFormat implements Serial
             PRISTINE
             .withDelimiter(TAB)
             .withEscape(ESCAPE)
-            .withLineSeparator(LF);
+            .withLineSeparator(LF_STR);
 
     /**
      * Creates a customized CSV format.

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,12 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.COMMENT;
+import static org.apache.commons.csv.Constants.CR;
+import static org.apache.commons.csv.Constants.EMPTY;
+import static org.apache.commons.csv.Constants.LF;
+import static org.apache.commons.csv.Constants.SP;
+
 import java.io.Flushable;
 import java.io.IOException;
 
@@ -24,12 +30,6 @@ import java.io.IOException;
  * Print values as a comma separated list.
  */
 public class CSVPrinter {
-
-    private static final char COMMENT = '#';
-    private static final String EMPTY = "";
-    private static final char SP = ' ';
-    private static final char CR = '\r';
-    private static final char LF = '\n';
     
     /** The place that the values get written. */
     private final Appendable out;

Added: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java?rev=1397541&view=auto
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java (added)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java Fri Oct 12 12:35:46 2012
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv;
+
+/**
+ * Constants for this package.
+ */
+class Constants {
+    
+    static final char BELL = '\b';
+    static final char COMMA = ',';
+    static final char COMMENT = '#';
+    static final char CR = '\r';
+    static final char DOUBLE_QUOTE = '"';
+    static final char ESCAPE = '\\';
+    static final char FF = '\f';
+    static final char LF = '\n';
+    static final char SP = ' ';
+    static final char TAB = '\t';
+    static final String EMPTY = "";
+    
+    /** The end of stream symbol */
+    static final int END_OF_STREAM = -1;
+
+    /** Undefined state for the lookahead char */
+    static final int UNDEFINED = -2;
+
+
+}

Propchange: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Constants.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,11 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.CR;
+import static org.apache.commons.csv.Constants.END_OF_STREAM;
+import static org.apache.commons.csv.Constants.LF;
+import static org.apache.commons.csv.Constants.UNDEFINED;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
@@ -29,16 +34,6 @@ import java.io.Reader;
  */
 class ExtendedBufferedReader extends BufferedReader {
 
-    private static final char CR = '\r';
-
-    private static final char LF = '\n';
-
-    /** The end of stream symbol */
-    static final int END_OF_STREAM = -1;
-
-    /** Undefined state for the lookahead char */
-    static final int UNDEFINED = -2;
-
     /** The last char returned */
     private int lastChar = UNDEFINED;
 

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Lexer.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Lexer.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Lexer.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/Lexer.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,14 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.BELL;
+import static org.apache.commons.csv.Constants.CR;
+import static org.apache.commons.csv.Constants.END_OF_STREAM;
+import static org.apache.commons.csv.Constants.FF;
+import static org.apache.commons.csv.Constants.LF;
+import static org.apache.commons.csv.Constants.TAB;
+import static org.apache.commons.csv.Constants.UNDEFINED;
+
 import java.io.IOException;
 
 /**
@@ -24,12 +32,6 @@ import java.io.IOException;
  */
 abstract class Lexer {
 
-    private static final char FF = '\f';
-    private static final char BELL = '\b';
-    private static final char TAB = '\t';
-    private static final char LF = '\n';
-    private static final char CR = '\r';
-    
     private final boolean isEncapsulating;
     private final boolean isEscaping;
     private final boolean isCommentEnabled;
@@ -80,7 +82,7 @@ abstract class Lexer {
             return BELL;
         case 'f':
             return FF;
-        case ExtendedBufferedReader.END_OF_STREAM:
+        case END_OF_STREAM:
             throw new IOException("EOF whilst processing escape sequence");
         default:
             return c;
@@ -125,14 +127,14 @@ abstract class Lexer {
      * @return true if the character is at the start of a line.
      */
     boolean isStartOfLine(final int c) {
-        return c == LF || c == CR || c == ExtendedBufferedReader.UNDEFINED;
+        return c == LF || c == CR || c == UNDEFINED;
     }
 
     /**
      * @return true if the given character indicates end of file
      */
     boolean isEndOfFile(final int c) {
-        return c == ExtendedBufferedReader.END_OF_STREAM;
+        return c == END_OF_STREAM;
     }
 
     abstract Token nextToken(Token reusableToken) throws IOException;

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer1.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer1.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer1.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer1.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,7 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.UNDEFINED;
 import static org.apache.commons.csv.Token.Type.EOF;
 import static org.apache.commons.csv.Token.Type.EORECORD;
 import static org.apache.commons.csv.Token.Type.TOKEN;
@@ -60,7 +61,7 @@ class CSVLexer1 extends Lexer {
         //  empty line detection: eol AND (last char was EOL or beginning)
         if (format.getIgnoreEmptyLines()) {
             while (eol
-                    && (lastChar == '\n' || lastChar == '\r' || lastChar == ExtendedBufferedReader.UNDEFINED)
+                    && (lastChar == '\n' || lastChar == '\r' || lastChar == UNDEFINED)
                     && !isEndOfFile(lastChar)) {
                 // go on char ahead ...
                 lastChar = c;

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer3.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer3.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer3.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexer3.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,7 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.END_OF_STREAM;
 import static org.apache.commons.csv.Token.Type.COMMENT;
 import static org.apache.commons.csv.Token.Type.EOF;
 import static org.apache.commons.csv.Token.Type.EORECORD;
@@ -74,7 +75,7 @@ class CSVLexer3 extends Lexer {
         if (isWhitespace(intch)) { // Must be after EOL check
             return CharType.WHITESPACE;
         }
-        if (intch == ExtendedBufferedReader.END_OF_STREAM) {
+        if (intch == END_OF_STREAM) {
             return CharType.EOFCHAR;
         }
         return CharType.OTHER;

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java?rev=1397541&r1=1397540&r2=1397541&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java Fri Oct 12 12:35:46 2012
@@ -17,6 +17,8 @@
 
 package org.apache.commons.csv;
 
+import static org.apache.commons.csv.Constants.END_OF_STREAM;
+import static org.apache.commons.csv.Constants.UNDEFINED;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
@@ -30,9 +32,9 @@ public class ExtendedBufferedReaderTest 
     @Test
     public void testEmptyInput() throws Exception {
         final ExtendedBufferedReader br = getBufferedReader("");
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
+        assertEquals(END_OF_STREAM, br.read());
+        assertEquals(END_OF_STREAM, br.lookAhead());
+        assertEquals(END_OF_STREAM, br.readAgain());
         assertNull(br.readLine());
         assertEquals(0, br.read(new char[10], 0, 0));
     }
@@ -41,7 +43,7 @@ public class ExtendedBufferedReaderTest 
     public void testReadLookahead1() throws Exception {
         final ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
         assertEquals('1', br.lookAhead());
-        assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
+        assertEquals(UNDEFINED, br.readAgain());
         assertEquals('1', br.read());
         assertEquals('1', br.readAgain());
 
@@ -79,12 +81,12 @@ public class ExtendedBufferedReaderTest 
         assertEquals('\n', br.readAgain());
         assertEquals(3, br.getLineNumber());
 
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
+        assertEquals(END_OF_STREAM, br.lookAhead());
         assertEquals('\n', br.readAgain());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
-        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
+        assertEquals(END_OF_STREAM, br.read());
+        assertEquals(END_OF_STREAM, br.readAgain());
+        assertEquals(END_OF_STREAM, br.read());
+        assertEquals(END_OF_STREAM, br.lookAhead());
 
     }