43
43
*
44
44
* The default {@link #setEncoding encoding} is UTF-8.
45
45
* 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}.
46
48
*
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.
49
51
* {@link org.metafacture.metamorph.functions.SetReplace} to remove matching
50
52
* keys.
51
53
*
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!
54
59
*
55
60
* @author Markus Michael Geipel
56
61
*/
@@ -59,10 +64,13 @@ public final class FileMap extends AbstractReadOnlyMap<String, String> {
59
64
private final FileOpener fileOpener = new FileOpener ();
60
65
private final Map <String , String > map = new HashMap <>();
61
66
67
+ private ArrayList <String > filenames = new ArrayList <>();
62
68
private Pattern split = Pattern .compile ("\t " , Pattern .LITERAL );
63
69
private boolean allowEmptyValues ;
70
+ private boolean allowTrailingColumns ;
64
71
private boolean isUninitialized = true ;
65
- private ArrayList <String > filenames = new ArrayList <>();
72
+ private int keyColumn ;
73
+ private int valueColumn = 1 ;
66
74
67
75
/**
68
76
* Creates an instance of {@link FileMap}.
@@ -79,14 +87,26 @@ private void init() {
79
87
* Sets whether to allow empty values in the {@link Map} or ignore these
80
88
* entries.
81
89
*
82
- * <strong>Default value: false </strong>
90
+ * <strong>Default value: false</strong>
83
91
*
84
92
* @param allowEmptyValues true if empty values in the Map are allowed
85
93
*/
86
94
public void setAllowEmptyValues (final boolean allowEmptyValues ) {
87
95
this .allowEmptyValues = allowEmptyValues ;
88
96
}
89
97
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
+
90
110
/**
91
111
* Sets a comma separated list of files which provides the {@link Map}.
92
112
*
@@ -141,14 +161,18 @@ private void loadFile(final String file) {
141
161
Reader reader = fileOpener .open (stream );
142
162
BufferedReader br = new BufferedReader (reader )
143
163
) {
164
+ final int minColumns = Math .max (keyColumn , valueColumn ) + 1 ;
165
+ final int maxColumns = allowTrailingColumns ? Integer .MAX_VALUE : minColumns ;
166
+
144
167
String line ;
145
168
while ((line = br .readLine ()) != null ) {
146
169
if (line .isEmpty ()) {
147
170
continue ;
148
171
}
172
+
149
173
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 ]);
152
176
}
153
177
}
154
178
}
@@ -205,6 +229,28 @@ public void setSeparator(final String delimiter) {
205
229
split = Pattern .compile (delimiter , Pattern .LITERAL );
206
230
}
207
231
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
+
208
254
@ Override
209
255
public String get (final Object key ) {
210
256
if (isUninitialized ) {
0 commit comments