Commit c8acc2ff authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Refactor and remove duplication

parent 39e8f663
......@@ -8,7 +8,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class OutputFile<K extends Comparable<K>> {
private String[] keyHeaders;
......@@ -53,26 +55,25 @@ public abstract class OutputFile<K extends Comparable<K>> {
}
private void printHeader(PrintWriter out) {
final List<String> fieldHeaders = new LinkedList<>(Arrays.asList(keyHeaders));
final List<String> processorFieldHeaders = processors.stream()
.flatMap(p -> Arrays.stream(p.getHeaders()))
.collect(Collectors.toList());
fieldHeaders.addAll(processorFieldHeaders);
final List<String> fieldHeaders = composeLine(keyHeaders, p -> Arrays.stream(p.getHeaders()));
writeLine(out, fieldHeaders);
}
private void printRow(final PrintWriter out, final K key) {
final List<String> fields = new LinkedList<>(Arrays.asList(toStrings(key)));
final List<String> fields = composeLine(toStrings(key), p -> Arrays.stream(p.toStrings(key)));
writeLine(out, fields);
}
private List<String> composeLine(String[] keyFieldArray, Function<Processor, Stream<String>> valueFields) {
final List<String> fields = new LinkedList<>(Arrays.asList(keyFieldArray));
final List<String> processorFields = processors.stream()
.flatMap(p -> Arrays.stream(p.toStrings(key)))
.flatMap(valueFields)
.collect(Collectors.toList());
fields.addAll(processorFields);
writeLine(out, fields);
return fields;
}
private void writeLine(PrintWriter out, final List<String> fields) {
out.println(String.join(SEPARATOR, fields));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment