Hi all,

I'm trying to figure out how to design the CSV API to make it simple and elegant. I'd like to get some feedback on the ideas I'm considering.

The focus is on the base package, the writer package contains an alternative implementation that is likely to be merged or removed.

Currently the package contains the following public classes:

CSVFormat (formerly CSVStrategy)
CSVParser
CSVPrinter
CSVUtils


The CSVFormat class contains no logic, and all the others classes operate on a CSVFormat. That make me think that the parse/print methods could be direct members of the CSVFormat class. That would be similar to the DateFormat and the NumberFormat classes having a set of parse() and format() methods.

Thus the API could be reduced to a single public class, CSVFormat, that would be used like this:

    CSVFormat format = new CSVFormat().withDelimiter(';');

    for (String[] values : format.parse(in)) {
        ....
    }

and for printing:

    PrintWriter out = ...
    for (String[] values : source) {
        out.print(format.format(values));
    }


The other idea relates to the bean mapping feature. CSVFormat could be generified and work on annotated classes. I imagine something like this:

    public class Person {
        @CSVField(trim = true)
        private String firstname;

        @CSVField(header="NAME", width=12)
        private String lastname;

        @CSVField(header="DATE", format="yyyy-MM-dd")
        private Date birthdate;
    }

then:

    CSVFormat<Person> format = new CSVFormat().withType(Person.class);

    for (Person person : format.parse(in)) {
        ....
    }


What do you think?


Emmanuel Bourg



Attachment: smime.p7s
Description: Signature cryptographique S/MIME

Reply via email to