/** Returns an immutable copy of {@code network}. */ public static <N, E> ImmutableNetwork<N, E> copyOf(Network<N, E> network) { return (network instanceof ImmutableNetwork) ? (ImmutableNetwork<N, E>) network : new ImmutableNetwork<N, E>(network); }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }
private static <N, E> Map<N, NetworkConnections<N, E>> getNodeConnections(Network<N, E> network) { // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have // whatever ordering the network's nodes do, so ImmutableSortedMap is unnecessary even if the // input nodes are sorted. ImmutableMap.Builder<N, NetworkConnections<N, E>> nodeConnections = ImmutableMap.builder(); for (N node : network.nodes()) { nodeConnections.put(node, connectionsOf(network, node)); } return nodeConnections.build(); }
private ImmutableNetwork(Network<N, E> network) { super( NetworkBuilder.from(network), getNodeConnections(network), getEdgeToReferenceNode(network)); }
@Test public void immutableNetwork() { MutableNetwork<String, Integer> mutableNetwork = NetworkBuilder.directed().build(); mutableNetwork.addNode("A"); ImmutableNetwork<String, Integer> immutableNetwork = ImmutableNetwork.copyOf(mutableNetwork); assertThat(immutableNetwork.asGraph()).isInstanceOf(ImmutableGraph.class); assertThat(immutableNetwork).isNotInstanceOf(MutableNetwork.class); assertThat(immutableNetwork).isEqualTo(mutableNetwork); mutableNetwork.addNode("B"); assertThat(immutableNetwork).isNotEqualTo(mutableNetwork); }
public Set<VxlanEdge> getEdges() { return ImmutableSet.copyOf(_graph.edges()); } }
/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */ // TODO(dpb): Return a network containing only Binding nodes. private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph( BindingGraph bindingGraph) { MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph.network()) .expectedNodeCount(bindingGraph.network().nodes().size()) .expectedEdgeCount(bindingGraph.dependencyEdges().size()) .build(); bindingGraph.dependencyEdges().stream() .filter(edge -> !breaksCycle(edge, bindingGraph)) .forEach( edge -> { EndpointPair<Node> endpoints = bindingGraph.network().incidentNodes(edge); dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge); }); return ImmutableNetwork.copyOf(dependencyNetwork); }
@Override public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) { ImmutableNetwork<Node, DependencyEdge> dependencyGraph = nonCycleBreakingDependencyGraph(bindingGraph); // Check each endpoint pair only once, no matter how many parallel edges connect them. Set<EndpointPair<Node>> dependencyEndpointPairs = dependencyGraph.asGraph().edges(); Set<EndpointPair<Node>> visited = newHashSetWithExpectedSize(dependencyEndpointPairs.size()); for (EndpointPair<Node> endpointPair : dependencyEndpointPairs) { cycleContainingEndpointPair(endpointPair, dependencyGraph, visited) .ifPresent(cycle -> reportCycle(cycle, bindingGraph, diagnosticReporter)); } }
private ImmutableNetwork(Network<N, E> network) { super( NetworkBuilder.from(network), getNodeConnections(network), getEdgeToReferenceNode(network)); }
private static <N> ImmutableNetwork<N, N> networkWithNode(N node) { MutableNetwork<N, N> network = NetworkBuilder.directed().build(); network.addNode(node); return ImmutableNetwork.copyOf(network); } }
@JsonProperty(PROP_EDGES) private SortedSet<Layer3Edge> getJsonEdges() { return ImmutableSortedSet.copyOf(_graph.edges()); }
@Test public void immutableNetwork() { MutableNetwork<String, Integer> mutableNetwork = NetworkBuilder.directed().build(); mutableNetwork.addNode("A"); ImmutableNetwork<String, Integer> immutableNetwork = ImmutableNetwork.copyOf(mutableNetwork); assertThat(immutableNetwork.asGraph()).isInstanceOf(ImmutableGraph.class); assertThat(immutableNetwork).isNotInstanceOf(MutableNetwork.class); assertThat(immutableNetwork).isEqualTo(mutableNetwork); mutableNetwork.addNode("B"); assertThat(immutableNetwork).isNotEqualTo(mutableNetwork); }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }
private ImmutableNetwork(Network<N, E> network) { super( NetworkBuilder.from(network), getNodeConnections(network), getEdgeToReferenceNode(network)); }
@Test public void edgesConnecting_undirected() { MutableNetwork<String, String> mutableNetwork = NetworkBuilder.undirected().allowsSelfLoops(true).build(); mutableNetwork.addEdge("A", "A", "AA"); mutableNetwork.addEdge("A", "B", "AB"); Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork); assertThat(network.edgesConnecting("A", "A")).containsExactly("AA"); assertThat(network.edgesConnecting("A", "B")).containsExactly("AB"); assertThat(network.edgesConnecting("B", "A")).containsExactly("AB"); } }
@JsonProperty(PROP_EDGES) private SortedSet<Layer1Edge> getJsonEdges() { return ImmutableSortedSet.copyOf(_graph.edges()); }
private static <N, E> Map<N, NetworkConnections<N, E>> getNodeConnections(Network<N, E> network) { // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have // whatever ordering the network's nodes do, so ImmutableSortedMap is unnecessary even if the // input nodes are sorted. ImmutableMap.Builder<N, NetworkConnections<N, E>> nodeConnections = ImmutableMap.builder(); for (N node : network.nodes()) { nodeConnections.put(node, connectionsOf(network, node)); } return nodeConnections.build(); }
/** Returns an immutable copy of {@code network}. */ public static <N, E> ImmutableNetwork<N, E> copyOf(Network<N, E> network) { return (network instanceof ImmutableNetwork) ? (ImmutableNetwork<N, E>) network : new ImmutableNetwork<N, E>(network); }
private static <N, E> NetworkConnections<N, E> connectionsOf(Network<N, E> network, N node) { if (network.isDirected()) { Map<E, N> inEdgeMap = Maps.asMap(network.inEdges(node), sourceNodeFn(network)); Map<E, N> outEdgeMap = Maps.asMap(network.outEdges(node), targetNodeFn(network)); int selfLoopCount = network.edgesConnecting(node, node).size(); return network.allowsParallelEdges() ? DirectedMultiNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount) : DirectedNetworkConnections.ofImmutable(inEdgeMap, outEdgeMap, selfLoopCount); } else { Map<E, N> incidentEdgeMap = Maps.asMap(network.incidentEdges(node), adjacentNodeFn(network, node)); return network.allowsParallelEdges() ? UndirectedMultiNetworkConnections.ofImmutable(incidentEdgeMap) : UndirectedNetworkConnections.ofImmutable(incidentEdgeMap); } }