Skip to content

Commit fd3a986

Browse files
committed
[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
1 parent 4ab1b69 commit fd3a986

3 files changed

Lines changed: 111 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
<title>Release Notes</title>
3939
</properties>
4040
<body>
41-
<release version="1.1.1" date="20??-MM-DD" description="Feature and bug fix release">
41+
<release version="1.2" date="2015-MM-DD" description="Feature and bug fix release">
4242
<action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action>
4343
<action issue="CSV-156" type="fix" dev="ggregory" due-to="Jason Steenstra-Pickens">Incorrect Javadoc on QuoteMode.NONE</action>
44+
<action issue="CSV-157" type="add" dev="ggregory">Add enum CSVFormat.Predefined that contains the default CSVFormat values.</action>
4445
</release>
4546
<release version="1.1" date="2014-11-16" description="Feature and bug fix release">
4647
<action issue="CSV-140" type="fix" dev="ggregory" due-to="Damjan Jovanovic">QuoteMode.NON_NUMERIC doesn't work with CSVPrinter.printRecords(ResultSet)</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,54 @@
147147
*/
148148
public final class CSVFormat implements Serializable {
149149

150+
/**
151+
* Predefines formats.
152+
*
153+
* @since 1.2
154+
*/
155+
public static enum Predefined {
156+
157+
/**
158+
* @see CSVFormat#DEFAULT.
159+
*/
160+
Default(CSVFormat.DEFAULT),
161+
162+
/**
163+
* @see CSVFormat#EXCEL.
164+
*/
165+
Excel(CSVFormat.EXCEL),
166+
167+
/**
168+
* @see CSVFormat#MYSQL.
169+
*/
170+
MySQL(CSVFormat.MYSQL),
171+
172+
/**
173+
* @see CSVFormat#RFC4180.
174+
*/
175+
RFC4180(CSVFormat.RFC4180),
176+
177+
/**
178+
* @see CSVFormat#TDF.
179+
*/
180+
TDF(CSVFormat.TDF);
181+
182+
private final CSVFormat format;
183+
184+
private Predefined(CSVFormat format) {
185+
this.format = format;
186+
}
187+
188+
/**
189+
* Gets the format.
190+
*
191+
* @return the format.
192+
*/
193+
public CSVFormat getFormat() {
194+
return format;
195+
}
196+
};
197+
150198
private static final long serialVersionUID = 1L;
151199

152200
private final char delimiter;
@@ -175,6 +223,7 @@ public final class CSVFormat implements Serializable {
175223
* <li>withRecordSeparator("\r\n")</li>
176224
* <li>withIgnoreEmptyLines(true)</li>
177225
* </ul>
226+
* @see Predefined#Default
178227
*/
179228
public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true,
180229
CRLF, null, null, null, false, false);
@@ -191,6 +240,7 @@ public final class CSVFormat implements Serializable {
191240
* <li>withRecordSeparator("\r\n")</li>
192241
* <li>withIgnoreEmptyLines(false)</li>
193242
* </ul>
243+
* @see Predefined#RFC4180
194244
*/
195245
public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);
196246

@@ -220,6 +270,7 @@ public final class CSVFormat implements Serializable {
220270
* Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean)
221271
* withAllowMissingColumnNames(true)}.
222272
* </p>
273+
* @see Predefined#Excel
223274
*/
224275
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames();
225276

@@ -235,6 +286,7 @@ public final class CSVFormat implements Serializable {
235286
* <li>withRecordSeparator("\r\n")</li>
236287
* <li>withIgnoreSurroundingSpaces(true)</li>
237288
* </ul>
289+
* @see Predefined#TDF
238290
*/
239291
public static final CSVFormat TDF = DEFAULT.withDelimiter(TAB).withIgnoreSurroundingSpaces();
240292

@@ -257,6 +309,7 @@ public final class CSVFormat implements Serializable {
257309
* <li>withEscape('\\')</li>
258310
* </ul>
259311
*
312+
* @see Predefined#MySQL
260313
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">
261314
* http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
262315
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests {@link CSVFormat.Predefined}.
25+
*/
26+
public class CSVFormatPredefinedTest {
27+
28+
private void test(final CSVFormat format, final String enumName) {
29+
Assert.assertEquals(format, CSVFormat.Predefined.valueOf(enumName).getFormat());
30+
}
31+
32+
@Test
33+
public void testDefault() {
34+
test(CSVFormat.DEFAULT, "Default");
35+
}
36+
37+
@Test
38+
public void testExcel() {
39+
test(CSVFormat.EXCEL, "Excel");
40+
}
41+
42+
@Test
43+
public void testMySQL() {
44+
test(CSVFormat.MYSQL, "MySQL");
45+
}
46+
47+
@Test
48+
public void testRFC4180() {
49+
test(CSVFormat.RFC4180, "RFC4180");
50+
}
51+
52+
@Test
53+
public void testTDF() {
54+
test(CSVFormat.TDF, "TDF");
55+
}
56+
}

0 commit comments

Comments
 (0)