commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benedikt Ritter (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CSV-68) Use Builder pattern for CSVFormat
Date Wed, 14 Nov 2012 16:40:12 GMT

    [ https://issues.apache.org/jira/browse/CSV-68?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497212#comment-13497212
] 

Benedikt Ritter commented on CSV-68:
------------------------------------

Hi Gary,

the problem is, that the user has no possibility to make sure that a CSVFormat is valid. Internally
we use the package private validate() method for this purpose. Using a builder makes sure
that only valid formats can be created and there is no need to validate a format after construction.
Another advantage of the builder is, that CSVFormat's interface is smaller and does only contain
methods that are needs when working with CSVFormats. OTH you can not simply call one of the
"withXXX" methods to create a copy of a format.

Regards and thanks for review,
Benedikt
                
> Use Builder pattern for CSVFormat
> ---------------------------------
>
>                 Key: CSV-68
>                 URL: https://issues.apache.org/jira/browse/CSV-68
>             Project: Commons CSV
>          Issue Type: Improvement
>            Reporter: Sebb
>         Attachments: CSV-68_20121111.patch, CSV-68.patch, CSVFormat2.java, CVSFormat2Main.java
>
>
> Using a builder pattern to create CSVFormat instances would allow the settings to be
validated at creation time and would eliminate the need to keep creating new CSVFormat instances
whilst still allowing the class to be immutable.
> A possible API is as follows:
> {code}
> CSVFormat DEFAULT = CSVFormat.init(',') // delimiter is required
>         .withEncapsulator('"')
>         .withLeadingSpacesIgnored(true)
>         .withTrailingSpacesIgnored(true)
>         .withEmptyLinesIgnored(true)
>         .withLineSeparator("\r\n") // optional, as it would be the default
>         .build();
> CSVFormat format = CSVFormat.init(CSVFormat.DEFAULT) // alternatively start with pre-defined
format
>         .withSurroundingSpacesIgnored(false)
>         .build();
> {code}
> Compare this with the current syntax:
> {code}
> // internal syntax; not easy to determine what all the parameters do
> CSVFormat DEFAULT1 = new CSVFormat(',', '"', DISABLED, DISABLED, true, true, false, true,
CRLF);
> // external syntax
> CSVFormat format = CSVFormat.DEFAULT.withSurroundingSpacesIgnored(false);
> {code}
> As a proof of concept I've written skeleton code which compiles (but needs completing).

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message