Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-68

Use the Builder pattern for CSVFormat

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.0
    • None
    • None

    Description

      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:

      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();
      

      Compare this with the current syntax:

      // 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);
      

      As a proof of concept I've written skeleton code which compiles (but needs completing).

      Attachments

        1. CSV-68.patch
          3 kB
          Sebb
        2. CSVFormat2.java
          14 kB
          Sebb
        3. CVSFormat2Main.java
          0.9 kB
          Sebb
        4. CSV-68_20121111.patch
          56 kB
          Benedikt Ritter
        5. CSV-68_20121114.patch
          57 kB
          Benedikt Ritter
        6. CSV-68_20121115.patch
          4 kB
          Benedikt Ritter
        7. CSV-68_20121117.patch
          12 kB
          Benedikt Ritter

        Issue Links

          Activity

            People

              Unassigned Unassigned
              sebb Sebb
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: