Skip to content

Commit

Permalink
feat(solrteur): introduce configuration POJO IQSS#7662
Browse files Browse the repository at this point in the history
This simple class will allow to make the parser somewhat configurable,
so future changes and command line options can be integrated more
easily.
  • Loading branch information
poikilotherm committed Apr 29, 2022
1 parent ff0e91c commit 2dae76c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cli.util.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Configuration {

private final String comment;
private final String trigger;
private final String column;
private final Matcher rtrimMatcher;

public Configuration(
String comment,
String trigger,
String column
) {
notNullNotEmpty("Comment indicator", comment);
this.comment = comment;

notNullNotEmpty("Triggering indicator (keyword prefix)", trigger);
this.trigger = trigger;

notNullNotEmpty("Column separator", column);
this.column = column;

this.rtrimMatcher = Pattern.compile("(" + this.column + ")+$").matcher("");
}

public static Configuration defaultConfig() {
return new Configuration("%%", "#", "\t");
}

private static void notNullNotEmpty(String optionName, String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException(optionName + " may not be null or empty");
}
}

public String commentIndicator() {
return comment;
}

public String triggerIndicator() {
return trigger;
}

public String columnSeparator() {
return column;
}

public String rtrimColumns(String line) {
return line == null ? null : rtrimMatcher.reset(line).replaceAll("");
}

public String trigger(String keyword) {
return this.triggerIndicator() + keyword;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cli.util.model;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.*;

class ConfigurationTest {

private final Configuration testConfiguration = Configuration.defaultConfig();

@ParameterizedTest
@CsvSource(nullValues = "NULL",
value = {
"NULL,NULL",
"hello,hello",
"' hello',' hello'",
"' hello ',' hello '",
"' hello',' hello\t\t\t'",
"'\t\t\thello','\t\t\thello\t\t\t'",
"'\t\t\thello\ttest','\t\t\thello\ttest\t\t'",
"'\t\t\thello\ttest\t\t ','\t\t\thello\ttest\t\t '",
})
void trimming(String expected, String sut) {
assertEquals(expected, testConfiguration.rtrimColumns(sut));
}
}

0 comments on commit 2dae76c

Please sign in to comment.