/** * Remove switches of components which have no connected equipment, * i.e for which all vertices objects are null. * * @param topo The node-breaker view of a voltage level. */ public static void removeIsolatedSwitches(VoltageLevel.NodeBreakerView topo) { Objects.requireNonNull(topo, "Node breaker topology view is null."); int[] nodes = topo.getNodes(); final TIntSet encountered = new TIntHashSet(); final Set<Switch> encounteredSwitches = new HashSet<>(); for (int n : nodes) { if (encountered.contains(n) || topo.getTerminal(n) == null) { continue; } encountered.add(n); topo.traverse(n, (n1, sw, n2) -> { encountered.add(n2); encounteredSwitches.add(sw); return topo.getTerminal(n2) == null; }); } List<Switch> toRemove = topo.getSwitchStream().filter(sw -> !encounteredSwitches.contains(sw)).collect(Collectors.toList()); for (Switch sw : toRemove) { topo.removeSwitch(sw.getId()); } }