private static CsvFile parseAll( List<String> lines, int lineIndex, char separator, ImmutableList<String> headers, ImmutableMap<String, Integer> searchHeaders) { ImmutableList.Builder<CsvRow> rows = ImmutableList.builder(); for (int i = lineIndex; i < lines.size(); i++) { ImmutableList<String> fields = parseLine(lines.get(i), i + 1, separator); if (!fields.isEmpty()) { rows.add(new CsvRow(headers, searchHeaders, i + 1, fields)); } } return new CsvFile(headers, searchHeaders, rows.build()); }
private static CsvFile create(List<String> lines, boolean headerRow, char separator) { if (headerRow) { for (int i = 0; i < lines.size(); i++) { ImmutableList<String> headers = parseLine(lines.get(i), i + 1, separator); if (!headers.isEmpty()) { ImmutableMap<String, Integer> searchHeaders = buildSearchHeaders(headers); return parseAll(lines, i + 1, separator, headers, searchHeaders); } } throw new IllegalArgumentException("Could not read header row from empty CSV file"); } return parseAll(lines, 0, separator, ImmutableList.of(), ImmutableMap.of()); }
/** * Checks whether there is another row in the CSV file. * * @return true if there is another row, false if not * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ @Override public boolean hasNext() { if (nextRow != null) { return true; } else { String line = null; while ((line = Unchecked.wrap(() -> reader.readLine())) != null) { currentLineNumber++; ImmutableList<String> fields = CsvFile.parseLine(line, currentLineNumber, separator); if (!fields.isEmpty()) { nextRow = new CsvRow(headers, searchHeaders, currentLineNumber, fields); return true; } } return false; } }
int lineNumber = 1; while (line != null) { ImmutableList<String> headers = CsvFile.parseLine(line, lineNumber, separator); if (!headers.isEmpty()) { return new CsvIterator(breader, separator, headers, CsvFile.buildSearchHeaders(headers), lineNumber);