@Override public Address next() { try { if (line == null) line = reader.readLine(); } catch (IOException e) { throw new UncheckedIOException(e); } if (line == null) throw new NoSuchElementException(); final String[] fields = CsvUtils.parseCsvLine(SEP, QUOTE, line); line = null; final Address address = new Address(); address.setId(readField(fields, idIndex)); address.setStreet(readField(fields, streetIndex)); address.setNumber(readField(fields, numberIndex)); address.setZipcode(readField(fields, zipIndex)); address.setMunicipality(readField(fields, municipalityIndex)); final String country = readField(fields, countryIndex); address.setCountry(country != null ? country : "CH"); return address; }
public CsvParser(final String file) { this.reader = IOUtils.getBufferedReader(file); try { final String[] firstLine = CsvUtils.parseCsvLine(SEP, QUOTE, reader.readLine()); for (int i = 0; i < firstLine.length; i++) { if (STREET.equals(firstLine[i])) { if (streetIndex >= 0) throw new RuntimeException(); streetIndex = i; } if (NUMBER.equals(firstLine[i])) { if (numberIndex >= 0) throw new RuntimeException(); numberIndex = i; } if (ZIP_CODE.equals(firstLine[i])) { if (zipIndex >= 0) throw new RuntimeException(); zipIndex = i; } if (CITY.equals(firstLine[i])) { if (municipalityIndex >= 0) throw new RuntimeException(); municipalityIndex = i; } if (COUNTRY.equals(firstLine[i])) { if (countryIndex >= 0) throw new RuntimeException(); countryIndex = i; } if (LOC_ID.equals(firstLine[i])) { if (idIndex >= 0) throw new RuntimeException(); idIndex = i; } } } catch (IOException e) { throw new UncheckedIOException(e); } }