import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.function.Function;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
return delimiters;
}
- public static List<Map<String, String>> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
- return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount);
- }
-
public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
if (firstAsHeader)
format = format.withFirstRecordAsHeader();
}
}
- public static List<CSVRecord> readRows(Path source, char delim, int rowAmount) throws IOException {
+ public static List<CSVRecord> readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException {
List<CSVRecord> results = new ArrayList<>();
+ AtomicInteger count = new AtomicInteger(0);
+ consumeCSV(source, delim, firstAsHeader, t -> {
+ results.add(t);
+ int current = count.incrementAndGet();
+ return current < rowAmount;
+ });
+ return results;
+ }
+
+ public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
CSVFormat format = CSVFormat.newFormat(delim);
+ if (firstAsHeader) {
+ format = format.withFirstRecordAsHeader();
+ }
try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
Iterator<CSVRecord> records = parser.iterator();
- int rows = 0;
- if (rowAmount == -1) {
- while (records.hasNext()) {
- results.add(records.next());
- rows++;
- }
- } else {
- while (rows < rowAmount && records.hasNext()) {
- results.add(records.next());
- rows++;
+ while (records.hasNext()) {
+ Boolean cont = consumer.apply(records.next());
+ if (!cont) {
+ break;
}
}
}
- return results;
}