Commit 98fc75aa authored by Jakob Schöttl's avatar Jakob Schöttl

Use sorted TreeMap and refactor

parent 40adb55b
package org.vadere.simulator.projects.dataprocessing_mtp;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
......@@ -10,6 +8,8 @@ import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public abstract class OutputFile<K extends Comparable<K>> {
private String[] keyHeaders;
private String fileName;
......@@ -17,7 +17,7 @@ public abstract class OutputFile<K extends Comparable<K>> {
private List<Integer> processorIds;
private List<Processor<K, ?>> processors;
private static Character SEPARATOR = ' ';
private static String SEPARATOR = " ";
OutputFile(final String... keyHeaders) {
this.keyHeaders = keyHeaders;
......@@ -33,6 +33,7 @@ public abstract class OutputFile<K extends Comparable<K>> {
this.processors.clear();
}
@SuppressWarnings("unchecked")
public void init(final ProcessorManager manager) {
processorIds.forEach(pid -> this.processors.add((Processor<K, ?>) manager.getProcessor(pid)));
}
......@@ -52,8 +53,8 @@ public abstract class OutputFile<K extends Comparable<K>> {
: String.join(SEPARATOR.toString(), this.keyHeaders) + SEPARATOR)
+ this.processors.stream().map(p -> String.join(SEPARATOR.toString(), p.getHeaders()) + SEPARATOR).reduce("", (s1, s2) -> s1 + s2), SEPARATOR.toString()));
this.processors.stream().flatMap(p -> p.getKeys().stream()).distinct().sorted()
.forEach(key -> printRow(out, key, this.processors));
this.processors.stream().flatMap(p -> p.getKeys().stream()).distinct()
.forEach(key -> printRow(out, key));
out.flush();
}
......@@ -62,14 +63,15 @@ public abstract class OutputFile<K extends Comparable<K>> {
}
}
private void printRow(PrintWriter out, final K key, final List<Processor<K, ?>> ps) {
private void printRow(final PrintWriter out, final K key) {
out.println(StringUtils.substringBeforeLast(
(this.toString() == null || this.toStrings(key).length == 0
(this.toStrings(key).length == 0
? ""
: String.join(SEPARATOR.toString(), String.join(SEPARATOR.toString(), this.toStrings(key)) + SEPARATOR))
+ ps.stream().map(p -> String.join(SEPARATOR.toString(), p.toStrings(key)) + SEPARATOR).reduce("", (s1, s2) -> s1 + s2), SEPARATOR.toString()));
: String.join(SEPARATOR, String.join(SEPARATOR, this.toStrings(key)) + SEPARATOR))
+ processors.stream().map(p -> String.join(SEPARATOR, p.toStrings(key)) + SEPARATOR).reduce("", (s1, s2) -> s1 + s2), SEPARATOR));
}
/** Return the column headers as string or the empty array. */
public String[] toStrings(K key) {
return new String[] { key.toString() };
}
......
package org.vadere.simulator.projects.dataprocessing_mtp;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.vadere.simulator.control.SimulationState;
......@@ -21,7 +21,7 @@ public abstract class Processor<K extends Comparable<K>, V> {
protected Processor(final String... headers) {
this.headers = headers;
this.column = new HashMap<>();
this.column = new TreeMap<>();
this.lastStep = 0;
}
......
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