X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2FDistrictImportUtils.java;h=ef000d75218c1dbe788acacbc3bb46c6a880bb50;hb=440c2b11fd49be2feb9edd39b931306b824b8aba;hp=1ad5b9a9487c0667d91a553cd6ecc461b1851156;hpb=1bc60c2213f9b3fc7b4d935ba9afda2b767290e5;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 1ad5b9a9..ef000d75 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 @@ -4,10 +4,15 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; 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; @@ -17,8 +22,7 @@ import org.simantics.db.Resource; public class DistrictImportUtils { private DistrictImportUtils() { } - - + public static Resource importCSVAsLayer(Path csvFile) throws IOException { try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) { @@ -42,15 +46,12 @@ public class DistrictImportUtils { public static Map getSupportedCSVDelimiterFormats() { Map delimiters = new HashMap<>(); - delimiters.put("COMMA", ','); - delimiters.put("SEMICOMMA", ';'); + delimiters.put("Comma", ','); + delimiters.put("Semicolon", ';'); + delimiters.put("Tabulator", '\t'); 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(); @@ -67,25 +68,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; } @@ -172,4 +179,20 @@ public class DistrictImportUtils { } } + public static Collection readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException { + Set results = new HashSet<>(); + CSVFormat format = CSVFormat.newFormat(delim); + try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + Iterator records = parser.iterator(); + if (records.hasNext()) + records.next(); + while (records.hasNext()) { + CSVRecord row = records.next(); + String value = row.get(mappingIndex); + results.add(value); + } + } + return results; + } + }