diff --git a/src/main/java/org/cobbzilla/util/collection/Topology.java b/src/main/java/org/cobbzilla/util/collection/Topology.java index 78da159..c81c87b 100644 --- a/src/main/java/org/cobbzilla/util/collection/Topology.java +++ b/src/main/java/org/cobbzilla/util/collection/Topology.java @@ -15,10 +15,10 @@ public class Topology { private final List> nodes = new ArrayList<>(); public void addNode(T thing, Collection refs) { - final Node node = nodes.stream() + final Node existingNode = nodes.stream() .filter(n -> n.thing.equals(thing)) - .findFirst() - .orElse(new Node<>(thing)); + .findFirst().orElse(null); + final Node node = existingNode != null ? existingNode : new Node<>(thing); // add refs as edges; skip self-references refs.stream().filter(ref -> !ref.equals(thing)).forEach(ref -> { @@ -34,7 +34,7 @@ public class Topology { node.addEdge(newEdgeNode); } }); - nodes.add(node); + if (existingNode == null) nodes.add(node); } static class Node {