[CSV-157] Add enum CSVFormat.Predefined that contains the default CSVFormat values.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1695178 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2015-08-10 22:11:24 +00:00
parent 4ab1b69cc0
commit fd3a9862bd
3 changed files with 111 additions and 1 deletions

View File

@ -38,9 +38,10 @@
<title>Release Notes</title>
</properties>
<body>
<release version="1.1.1" date="20??-MM-DD" description="Feature and bug fix release">
<release version="1.2" date="2015-MM-DD" description="Feature and bug fix release">
<action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action>
<action issue="CSV-156" type="fix" dev="ggregory" due-to="Jason Steenstra-Pickens">Incorrect Javadoc on QuoteMode.NONE</action>
<action issue="CSV-157" type="add" dev="ggregory">Add enum CSVFormat.Predefined that contains the default CSVFormat values.</action>
</release>
<release version="1.1" date="2014-11-16" description="Feature and bug fix release">
<action issue="CSV-140" type="fix" dev="ggregory" due-to="Damjan Jovanovic">QuoteMode.NON_NUMERIC doesn't work with CSVPrinter.printRecords(ResultSet)</action>

View File

@ -147,6 +147,54 @@ import java.util.Set;
*/
public final class CSVFormat implements Serializable {
/**
* Predefines formats.
*
* @since 1.2
*/
public static enum Predefined {
/**
* @see CSVFormat#DEFAULT.
*/
Default(CSVFormat.DEFAULT),
/**
* @see CSVFormat#EXCEL.
*/
Excel(CSVFormat.EXCEL),
/**
* @see CSVFormat#MYSQL.
*/
MySQL(CSVFormat.MYSQL),
/**
* @see CSVFormat#RFC4180.
*/
RFC4180(CSVFormat.RFC4180),
/**
* @see CSVFormat#TDF.
*/
TDF(CSVFormat.TDF);
private final CSVFormat format;
private Predefined(CSVFormat format) {
this.format = format;
}
/**
* Gets the format.
*
* @return the format.
*/
public CSVFormat getFormat() {
return format;
}
};
private static final long serialVersionUID = 1L;
private final char delimiter;
@ -175,6 +223,7 @@ public final class CSVFormat implements Serializable {
* <li>withRecordSeparator("\r\n")</li>
* <li>withIgnoreEmptyLines(true)</li>
* </ul>
* @see Predefined#Default
*/
public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true,
CRLF, null, null, null, false, false);
@ -191,6 +240,7 @@ public final class CSVFormat implements Serializable {
* <li>withRecordSeparator("\r\n")</li>
* <li>withIgnoreEmptyLines(false)</li>
* </ul>
* @see Predefined#RFC4180
*/
public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);
@ -220,6 +270,7 @@ public final class CSVFormat implements Serializable {
* Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean)
* withAllowMissingColumnNames(true)}.
* </p>
* @see Predefined#Excel
*/
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames();
@ -235,6 +286,7 @@ public final class CSVFormat implements Serializable {
* <li>withRecordSeparator("\r\n")</li>
* <li>withIgnoreSurroundingSpaces(true)</li>
* </ul>
* @see Predefined#TDF
*/
public static final CSVFormat TDF = DEFAULT.withDelimiter(TAB).withIgnoreSurroundingSpaces();
@ -257,6 +309,7 @@ public final class CSVFormat implements Serializable {
* <li>withEscape('\\')</li>
* </ul>
*
* @see Predefined#MySQL
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">
* http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
*/

View File

@ -0,0 +1,56 @@
/*
* 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;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests {@link CSVFormat.Predefined}.
*/
public class CSVFormatPredefinedTest {
private void test(final CSVFormat format, final String enumName) {
Assert.assertEquals(format, CSVFormat.Predefined.valueOf(enumName).getFormat());
}
@Test
public void testDefault() {
test(CSVFormat.DEFAULT, "Default");
}
@Test
public void testExcel() {
test(CSVFormat.EXCEL, "Excel");
}
@Test
public void testMySQL() {
test(CSVFormat.MYSQL, "MySQL");
}
@Test
public void testRFC4180() {
test(CSVFormat.RFC4180, "RFC4180");
}
@Test
public void testTDF() {
test(CSVFormat.TDF, "TDF");
}
}