Skip to content

Commit 995e069

Browse files
committed
Optionally set number of expected columns. (#471)
1 parent c6f2032 commit 995e069

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
* {@link org.metafacture.metamorph.functions.SetReplace} to remove matching
5252
* keys.
5353
*
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.
5656
*
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!
5959
*
6060
* @author Markus Michael Geipel
6161
*/
@@ -67,8 +67,8 @@ public final class FileMap extends AbstractReadOnlyMap<String, String> {
6767
private ArrayList<String> filenames = new ArrayList<>();
6868
private Pattern split = Pattern.compile("\t", Pattern.LITERAL);
6969
private boolean allowEmptyValues;
70-
private boolean allowTrailingColumns;
7170
private boolean isUninitialized = true;
71+
private int expectedColumns;
7272
private int keyColumn;
7373
private int valueColumn = 1;
7474

@@ -96,15 +96,17 @@ public void setAllowEmptyValues(final boolean allowEmptyValues) {
9696
}
9797

9898
/**
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.
101102
*
102-
* <strong>Default value: false</strong>
103+
* <strong>Default value: calculated from {@link #setKeyColumn key} and
104+
* {@link #setValueColumn value} columns</strong>
103105
*
104-
* @param allowTrailingColumns true if trailing columns in the Map are allowed
106+
* @param expectedColumns number of expected columns
105107
*/
106-
public void setAllowTrailingColumns(final boolean allowTrailingColumns) {
107-
this.allowTrailingColumns = allowTrailingColumns;
108+
public void setExpectedColumns(final int expectedColumns) {
109+
this.expectedColumns = expectedColumns;
108110
}
109111

110112
/**
@@ -162,7 +164,7 @@ private void loadFile(final String file) {
162164
BufferedReader br = new BufferedReader(reader)
163165
) {
164166
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;
166168

167169
String line;
168170
while ((line = br.readLine()) != null) {
@@ -171,7 +173,11 @@ private void loadFile(final String file) {
171173
}
172174

173175
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) {
175181
map.put(parts[keyColumn], parts[valueColumn]);
176182
}
177183
}

metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,20 @@ public void shouldLoadFileWithKeyAndValueColumn() {
271271
}
272272

273273
@Test
274-
public void shouldLoadFileWithTrailingColumns() {
274+
public void shouldLoadFileWithExpectedColumns() {
275+
assertMap(24, i -> {
276+
i.setSeparator(" ");
277+
i.setExpectedColumns(3);
278+
279+
Assert.assertEquals("New", i.get("pp\tPapua"));
280+
});
281+
}
282+
283+
@Test
284+
public void shouldLoadFileWithArbitraryExpectedColumns() {
275285
assertMap(149, i -> {
276286
i.setSeparator(" ");
277-
i.setAllowTrailingColumns(true);
287+
i.setExpectedColumns(-1);
278288

279289
Assert.assertEquals("New", i.get("pp\tPapua"));
280290
});
@@ -294,6 +304,20 @@ public void shouldNotLoadFileWithOutOfRangeValueColumn() {
294304
});
295305
}
296306

307+
@Test
308+
public void shouldNotLoadFileWithTooFewExpectedColumns() {
309+
assertMap(0, i -> {
310+
i.setExpectedColumns(1);
311+
});
312+
}
313+
314+
@Test
315+
public void shouldNotLoadFileWithTooManyExpectedColumns() {
316+
assertMap(0, i -> {
317+
i.setExpectedColumns(99);
318+
});
319+
}
320+
297321
private void assertMap(final int size, final Consumer<FileMap> consumer) {
298322
final FileMap fileMap = new FileMap();
299323
fileMap.setFile(MAPS + "file-map-test-columns.txt");

0 commit comments

Comments
 (0)