]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
Elimination of compiler warnings.
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
index f123855dfc47e32be31d2268a05e6d3401ca33f4..ef000d75218c1dbe788acacbc3bb46c6a880bb50 100644 (file)
@@ -11,6 +11,8 @@ 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.Function;
 
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVParser;
@@ -46,13 +48,10 @@ public class DistrictImportUtils {
         Map<String, Character> delimiters = new HashMap<>();
         delimiters.put("Comma", ',');
         delimiters.put("Semicolon", ';');
+        delimiters.put("Tabulator", '\t');
         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();
@@ -69,25 +68,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;
     }