51
51
* {@link org.metafacture.metamorph.functions.SetReplace} to remove matching
52
52
* keys.
53
53
*
54
- * <p>By setting {@link #setAllowTrailingColumns allowTrailingColumns } to
55
- * {@code true }, the number of columns is not checked.
54
+ * <p>By setting {@link #setExpectedColumns expectedColumns } to
55
+ * {@code -1 }, the number of columns is not checked.
56
56
*
57
- * <p><strong>Important:</strong> Otherwise, all lines that are not split in
58
- * two parts by the separator are ignored!
57
+ * <p><strong>Important:</strong> Otherwise, all lines that are not split into
58
+ * the expected number of parts by the separator are ignored!
59
59
*
60
60
* @author Markus Michael Geipel
61
61
*/
@@ -67,8 +67,8 @@ public final class FileMap extends AbstractReadOnlyMap<String, String> {
67
67
private ArrayList <String > filenames = new ArrayList <>();
68
68
private Pattern split = Pattern .compile ("\t " , Pattern .LITERAL );
69
69
private boolean allowEmptyValues ;
70
- private boolean allowTrailingColumns ;
71
70
private boolean isUninitialized = true ;
71
+ private int expectedColumns ;
72
72
private int keyColumn ;
73
73
private int valueColumn = 1 ;
74
74
@@ -96,15 +96,17 @@ public void setAllowEmptyValues(final boolean allowEmptyValues) {
96
96
}
97
97
98
98
/**
99
- * Sets whether to allow trailing columns in the {@link Map} or ignore these
100
- * entries.
99
+ * Sets number of expected columns; lines with different number of columns
100
+ * are ignored. Set to {@code -1} to disable the check and allow arbitrary
101
+ * number of columns.
101
102
*
102
- * <strong>Default value: false</strong>
103
+ * <strong>Default value: calculated from {@link #setKeyColumn key} and
104
+ * {@link #setValueColumn value} columns</strong>
103
105
*
104
- * @param allowTrailingColumns true if trailing columns in the Map are allowed
106
+ * @param expectedColumns number of expected columns
105
107
*/
106
- public void setAllowTrailingColumns (final boolean allowTrailingColumns ) {
107
- this .allowTrailingColumns = allowTrailingColumns ;
108
+ public void setExpectedColumns (final int expectedColumns ) {
109
+ this .expectedColumns = expectedColumns ;
108
110
}
109
111
110
112
/**
@@ -162,7 +164,7 @@ private void loadFile(final String file) {
162
164
BufferedReader br = new BufferedReader (reader )
163
165
) {
164
166
final int minColumns = Math .max (keyColumn , valueColumn ) + 1 ;
165
- final int maxColumns = allowTrailingColumns ? Integer . MAX_VALUE : minColumns ;
167
+ final int expColumns = expectedColumns != 0 ? expectedColumns : minColumns ;
166
168
167
169
String line ;
168
170
while ((line = br .readLine ()) != null ) {
@@ -171,7 +173,11 @@ private void loadFile(final String file) {
171
173
}
172
174
173
175
final String [] parts = allowEmptyValues ? split .split (line , -1 ) : split .split (line );
174
- if (parts .length >= minColumns && parts .length <= maxColumns ) {
176
+ if (parts .length < minColumns ) {
177
+ continue ;
178
+ }
179
+
180
+ if (expColumns < 0 || parts .length == expColumns ) {
175
181
map .put (parts [keyColumn ], parts [valueColumn ]);
176
182
}
177
183
}
0 commit comments