]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
Merge "Make vertices smaller on map UI & CSV import performance improvements"
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
index 6b22746c4f5d3fc082f8cebe693d685e489c7001..ca3f4be59aef4a6c055ed1e77522d693242b7e6a 100644 (file)
@@ -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<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();
@@ -70,25 +69,31 @@ public class DistrictImportUtils {
         }
     }
 
-    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;
     }