Skip to content

Commit 4bfbff8

Browse files
committed
Optionally set key and/or value column. (hbz/lobid-resources#1461)
1 parent 2fa964a commit 4bfbff8

File tree

1 file changed

+54
-8
lines changed
  • metamorph/src/main/java/org/metafacture/metamorph/maps

1 file changed

+54
-8
lines changed

metamorph/src/main/java/org/metafacture/metamorph/maps/FileMap.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@
4343
*
4444
* The default {@link #setEncoding encoding} is UTF-8.
4545
* The default {@link #setSeparator separator} is {@code \t}.
46+
* The default {@link #setKeyColumn keyColumn} is {@code 0}.
47+
* The default {@link #setValueColumn valueColumn} is {@code 1}.
4648
*
47-
* By setting {@link #allowEmptyValues} to {@code true} the values in the
48-
* {@link Map} can be empty thus enabling e.g.
49+
* <p>By setting {@link #setAllowEmptyValues allowEmptyValues} to {@code true},
50+
* the values in the {@link Map} can be empty; thus enabling e.g.
4951
* {@link org.metafacture.metamorph.functions.SetReplace} to remove matching
5052
* keys.
5153
*
52-
* <strong>Important:</strong> All other lines that are not split in two parts
53-
* by the separator are ignored!
54+
* <p>By setting {@link #setAllowTrailingColumns allowTrailingColumns} to
55+
* {@code true}, the number of columns is not checked.
56+
*
57+
* <p><strong>Important:</strong> Otherwise, all lines that are not split in
58+
* two parts by the separator are ignored!
5459
*
5560
* @author Markus Michael Geipel
5661
*/
@@ -59,10 +64,13 @@ public final class FileMap extends AbstractReadOnlyMap<String, String> {
5964
private final FileOpener fileOpener = new FileOpener();
6065
private final Map<String, String> map = new HashMap<>();
6166

67+
private ArrayList<String> filenames = new ArrayList<>();
6268
private Pattern split = Pattern.compile("\t", Pattern.LITERAL);
6369
private boolean allowEmptyValues;
70+
private boolean allowTrailingColumns;
6471
private boolean isUninitialized = true;
65-
private ArrayList<String> filenames = new ArrayList<>();
72+
private int keyColumn;
73+
private int valueColumn = 1;
6674

6775
/**
6876
* Creates an instance of {@link FileMap}.
@@ -79,14 +87,26 @@ private void init() {
7987
* Sets whether to allow empty values in the {@link Map} or ignore these
8088
* entries.
8189
*
82-
* <strong>Default value: false </strong>
90+
* <strong>Default value: false</strong>
8391
*
8492
* @param allowEmptyValues true if empty values in the Map are allowed
8593
*/
8694
public void setAllowEmptyValues(final boolean allowEmptyValues) {
8795
this.allowEmptyValues = allowEmptyValues;
8896
}
8997

98+
/**
99+
* Sets whether to allow trailing columns in the {@link Map} or ignore these
100+
* entries.
101+
*
102+
* <strong>Default value: false</strong>
103+
*
104+
* @param allowTrailingColumns true if trailing columns in the Map are allowed
105+
*/
106+
public void setAllowTrailingColumns(final boolean allowTrailingColumns) {
107+
this.allowTrailingColumns = allowTrailingColumns;
108+
}
109+
90110
/**
91111
* Sets a comma separated list of files which provides the {@link Map}.
92112
*
@@ -141,14 +161,18 @@ private void loadFile(final String file) {
141161
Reader reader = fileOpener.open(stream);
142162
BufferedReader br = new BufferedReader(reader)
143163
) {
164+
final int minColumns = Math.max(keyColumn, valueColumn) + 1;
165+
final int maxColumns = allowTrailingColumns ? Integer.MAX_VALUE : minColumns;
166+
144167
String line;
145168
while ((line = br.readLine()) != null) {
146169
if (line.isEmpty()) {
147170
continue;
148171
}
172+
149173
final String[] parts = allowEmptyValues ? split.split(line, -1) : split.split(line);
150-
if (parts.length == 2) {
151-
map.put(parts[0], parts[1]);
174+
if (parts.length >= minColumns && parts.length <= maxColumns) {
175+
map.put(parts[keyColumn], parts[valueColumn]);
152176
}
153177
}
154178
}
@@ -205,6 +229,28 @@ public void setSeparator(final String delimiter) {
205229
split = Pattern.compile(delimiter, Pattern.LITERAL);
206230
}
207231

232+
/**
233+
* Sets the key column (0-based).
234+
*
235+
* <strong>Default value: {@code 0}</strong>
236+
*
237+
* @param keyColumn the key column
238+
*/
239+
public void setKeyColumn(final int keyColumn) {
240+
this.keyColumn = keyColumn;
241+
}
242+
243+
/**
244+
* Sets the value column (0-based).
245+
*
246+
* <strong>Default value: {@code 1}</strong>
247+
*
248+
* @param valueColumn the value column
249+
*/
250+
public void setValueColumn(final int valueColumn) {
251+
this.valueColumn = valueColumn;
252+
}
253+
208254
@Override
209255
public String get(final Object key) {
210256
if (isUninitialized) {

0 commit comments

Comments
 (0)