TracerouteEngine: simpler way of building result map
Do not need to use concurrent data structures,  just collectors.
@@ -5,10 +5,13 @@ import static org.batfish.dataplane.traceroute.TracerouteUtils.buildSessionsByIn
import static org.batfish.dataplane.traceroute.TracerouteUtils.buildSessionsByOriginatingVrf;
import static org.batfish.dataplane.traceroute.TracerouteUtils.validateInputs;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
+import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Multimap;
-import java.util.ArrayList;
+import com.google.common.collect.Ordering;
+import java.util.AbstractMap.SimpleEntry;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -17,8 +20,6 @@ import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
-import java.util.TreeMap;
-import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import org.batfish.common.BatfishException;
import org.batfish.datamodel.Configuration;
@@ -78,21 +79,21 @@ public class TracerouteEngineImplContext {
* @return {@link SortedMap} of {@link Flow} to a {@link List} of {@link Trace}s
*/
public SortedMap<Flow, List<TraceAndReverseFlow>> buildTracesAndReturnFlows() {
- Map<Flow, List<TraceAndReverseFlow>> traces = new ConcurrentHashMap<>();
- _flows.parallelStream()
- .forEach(
+ return _flows.parallelStream()
+ .map(
flow -> {
- List<TraceAndReverseFlow> currentTraces =
- traces.computeIfAbsent(flow, k -> new ArrayList<>());
validateInputs(_configurations, flow);
String ingressNodeName = flow.getIngressNode();
String ingressInterfaceName = flow.getIngressInterface();
DagTraceRecorder recorder = new DagTraceRecorder(flow);
initialFlowTracer(this, ingressNodeName, ingressInterfaceName, flow, recorder)
.processHop();
- recorder.build().getTraces().forEach(currentTraces::add);
- });
- return new TreeMap<>(traces);
+ return new SimpleEntry<>(
+ flow, recorder.build().getTraces().collect(ImmutableList.toImmutableList()));
+ })
+ .collect(
+ ImmutableSortedMap.toImmutableSortedMap(
+ Ordering.natural(), Entry::getKey, Entry::getValue));
}
/**
