Java Developer

ot-clean, Functional config file preprocessing

YAML support alias nodes which were causing some unnecessary double quotes. It is not a very big deal but I it is forcing you to type “**/*.log” instead of just **/*.log. I implemented simple file preprocessing which handles this case.

Functional style replace

I had to find specific lines in the input file and produce preprocessed one with replaced values. That sounds trivial but I wanted to abstract matching and replacing process. I decided to create new Replace class with functional style replace method.

* Apply replace transformation on selected line only if matching
* function returned true.
* @param matching Closure that returns true if line has been matched.
* @param transformation Closure with line transformation.
* @return Replace object.
public Replace replace(
   final Function<String, Boolean> matching,
   final Function<String, String> transformation

As you can see it is returning Replace object so replacing can be chained.

new Replace(text).replace(...).replace(...).replace(...).output();

The problem with proper new line character was resolved by Scanner. StrBuilder from apache commons in case of writing. Plus automatic resource release:

public Replace replace(
   final Function<String, Boolean> matching,
   final Function<String, String> transformation
) {
   final StrBuilder out = new StrBuilder();
   try (Scanner scanner = new Scanner(this.text)) {
       while (scanner.hasNextLine()) {
           final String line = scanner.nextLine();
           if (matching.apply(line)) {
           } else {
   return new Replace(;

Final preprocessing method:

* Preprocess input file and append double quotes for all paths in the file.
* @param file Input file.
* @return Preprocessed file.
* @throws IOException On file error.
private static String preprocess(final File file) throws IOException {
   final String pattern = "- *";
   return new Replace(
       line -> line.contains(pattern),
       line -> StringUtils.join(
           StringUtils.replace(line, pattern, "- \"*"),

The ot-clean series

This blog post is one of the series about the ot-clean. Simple open source tool that maintains your projects directories cleaned from all the garbage. Feel free to look at the source code or readme page for more information. You can also submit pull request or post suggestion. This text participates in open source blogging competition.

Project status

Build Status

All posts from the ot-clean series

Like it? Share it on , , ,

About the author

Grzegorz Gajos, Software Architect with international consulting and programming background. Co-founder of Open Tangerine Software House. Quality evangelist. An experienced entrepreneur, out of the box thinker and problem solver.