X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2FDistrictImportUtils.java;fp=org.simantics.district.imports%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2FDistrictImportUtils.java;h=ca3f4be59aef4a6c055ed1e77522d693242b7e6a;hb=83f7b8f97f2ded1e48ed58d404f76d61625ae17b;hp=6b22746c4f5d3fc082f8cebe693d685e489c7001;hpb=650d50b78c7d021898c6616e9c826ee7dfa4f39b;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java index 6b22746c..ca3f4be5 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java @@ -11,6 +11,9 @@ import java.util.Iterator; 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; @@ -50,10 +53,6 @@ public class DistrictImportUtils { return delimiters; } - public static List> readRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException { - return readRows(source, CSVFormat.newFormat(delimiter), firstAsHeader, amount); - } - public static List> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException { if (firstAsHeader) format = format.withFirstRecordAsHeader(); @@ -70,25 +69,31 @@ public class DistrictImportUtils { } } - public static List readRows(Path source, char delim, int rowAmount) throws IOException { + public static List readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException { List 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 consumer) throws IOException { CSVFormat format = CSVFormat.newFormat(delim); + if (firstAsHeader) { + format = format.withFirstRecordAsHeader(); + } try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { Iterator 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; }