]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.district.imports;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.function.Function;
16
17 import org.apache.commons.csv.CSVFormat;
18 import org.apache.commons.csv.CSVParser;
19 import org.apache.commons.csv.CSVRecord;
20 import org.simantics.db.Resource;
21
22 public class DistrictImportUtils {
23
24     private DistrictImportUtils() { }
25
26     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
27         
28         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
29             Map<String, Integer> header = parser.getHeaderMap();
30             System.out.println(header);
31         }
32         return null;
33     }
34     
35     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
36         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
37     }
38     
39     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
40         if (firstAsHeader)
41             format = format.withFirstRecordAsHeader();
42         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
43             return parser.getHeaderMap();
44         }
45     }
46
47     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
48         Map<String, Character> delimiters = new HashMap<>();
49         delimiters.put("Comma", ',');
50         delimiters.put("Semicolon", ';');
51         delimiters.put("Tabulator", '\t');
52         return delimiters;
53     }
54
55     public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
56         if (firstAsHeader)
57             format = format.withFirstRecordAsHeader();
58         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
59             int start = 0;
60             List<Map<String, String>> results = new ArrayList<>(amount);
61             Iterator<CSVRecord> iter = parser.iterator();
62             while (start < amount && iter.hasNext()) {
63                 CSVRecord record = iter.next();
64                 results.add(record.toMap());
65                 start++;
66             }
67             return results;
68         }
69     }
70
71     public static List<CSVRecord> readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException {
72         List<CSVRecord> results = new ArrayList<>();
73         AtomicInteger count = new AtomicInteger(0);
74         consumeCSV(source, delim, firstAsHeader, t -> {
75             results.add(t);
76             int current = count.incrementAndGet();
77             return current < rowAmount;
78         });
79         return results;
80     }
81     
82     public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
83         CSVFormat format = CSVFormat.newFormat(delim);
84         if (firstAsHeader) {
85             format = format.withFirstRecordAsHeader();
86         }
87         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
88             Iterator<CSVRecord> records = parser.iterator();
89             while (records.hasNext()) {
90                 Boolean cont = consumer.apply(records.next());
91                 if (!cont) {
92                     break;
93                 }
94             }
95         }
96     }
97
98     
99     public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
100         Map<CSVHeader, List<String>> results = new HashMap<>();
101         CSVFormat format = CSVFormat.newFormat(delimiter);
102         if (firstAsHeader)
103             format = format.withFirstRecordAsHeader();
104         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
105             Map<String, Integer> headers = parser.getHeaderMap();
106             if (headers != null && !headers.isEmpty()) {
107                 for (int index = 0; index < headers.size(); index++) {
108                     for (String head : headers.keySet()) {
109                         results.put(new CSVHeader(head, index), new ArrayList<>());
110                     }
111                 }
112             }
113             
114             Iterator<CSVRecord> records = parser.iterator();
115             int rows = 0;
116             while (rows < amount && records.hasNext()) {
117                 CSVRecord record = records.next();
118                 for (int j = 0; j < record.size(); j++) {
119                     String value = record.get(j);
120                     String header = Integer.toString(j);
121                     CSVHeader csvHeader = new CSVHeader(header, j);
122                     List<String> vals = results.get(csvHeader);
123                     if (vals == null) {
124                         vals = new ArrayList<>();
125                         results.put(csvHeader, vals);
126                     }
127                     vals.add(value);
128                 }
129                 rows++;
130             }
131         }
132         return results;
133     }
134     
135     public static class CSVHeader {
136
137         private final String header;
138         private final int index;
139         
140         public CSVHeader(String header, int index) {
141             this.header = header;
142             this.index = index;
143         }
144
145         public String getHeader() {
146             return header;
147         }
148
149         public int getIndex() {
150             return index;
151         }
152         
153         @Override
154         public int hashCode() {
155             final int prime = 31;
156             int result = 1;
157             result = prime * result + ((header == null) ? 0 : header.hashCode());
158             result = prime * result + index;
159             return result;
160         }
161
162         @Override
163         public boolean equals(Object obj) {
164             if (this == obj)
165                 return true;
166             if (obj == null)
167                 return false;
168             if (getClass() != obj.getClass())
169                 return false;
170             CSVHeader other = (CSVHeader) obj;
171             if (header == null) {
172                 if (other.header != null)
173                     return false;
174             } else if (!header.equals(other.header))
175                 return false;
176             if (index != other.index)
177                 return false;
178             return true;
179         }
180     }
181
182     public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
183         Set<String> results = new HashSet<>();
184         CSVFormat format = CSVFormat.newFormat(delim);
185         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
186             Iterator<CSVRecord> records = parser.iterator();
187             if (records.hasNext())
188                 records.next();
189             while (records.hasNext()) {
190                 CSVRecord row = records.next();
191                 String value = row.get(mappingIndex);
192                 results.add(value);
193             }
194         }
195         return results;
196     }
197
198 }