Am 15. März 2012 01:58 schrieb Emmanuel Bourg <ebo...@apache.org>: > There is another alternative, we might replace the records returned as a > String[] by a CSVRecord class able to access the fields by id or by name. > This would be similar to a JDBC resultset (except for the looping logic) >
sounds good. This discussion showed, that a record is more than a String array. So having a specialized class is a good idea. > This avoids the duplication of the parser, which might still be generified > later to support custom beans. > > The example becomes: > > CSVFormat format = CSVFormat.DEFAULT.withHeader(); > > for (CSVRecord record : format.parse(in)) { > > Person person = new Person(); > person.setName(record.get("name")); > person.setEmail(record.get("email")); > person.setPhone(record.get("phone")); > persons.add(person); > } > > The record is not a Map to keep it simple, it only exposes 3 methods: > get(int), get(String) and size() > I'm not sure if I understand the approach completely. The Header can not be accessed as a CSVRecord, right? CSVRecords know the header values through get(string). What happens if the format does not support a header? UnsupportedOperationException? If I got you right, we could use getHeaders() to know, which header values are available. Maybe it would be useful to have the record implement iterable as well. Benedikt > Emmanuel Bourg > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org