package com.google.common.graph;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.graph.GraphConstants.PARALLEL_EDGES_NOT_ALLOWED;
import static com.google.common.graph.GraphConstants.REUSING_EDGE;
import static com.google.common.graph.GraphConstants.SELF_LOOPS_NOT_ALLOWED;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
final class ConfigurableMutableNetwork<N, E> extends ConfigurableNetwork<N, E>
implements MutableNetwork<N, E> {
ConfigurableMutableNetwork(NetworkBuilder<? super N, ? super E> builder) {
super(builder);
}
@Override
@CanIgnoreReturnValue
public boolean addNode(N node) {
checkNotNull(node, "node");
if (containsNode(node)) {
return false;
}
addNodeInternal(node);
return true;
}
@CanIgnoreReturnValue
private NetworkConnections<N, E> addNodeInternal(N node) {
NetworkConnections<N, E> connections = newConnections();
checkState(nodeConnections.put(node, connections) == null);
return connections;
}
@Override
@CanIgnoreReturnValue
public boolean addEdge(N nodeU, N nodeV, E edge) {
checkNotNull(nodeU, "nodeU");
checkNotNull(nodeV, "nodeV");
checkNotNull(edge, "edge");
if (containsEdge(edge)) {
EndpointPair<N> existingIncidentNodes = incidentNodes(edge);
EndpointPair<N> newIncidentNodes = EndpointPair.of(this, nodeU, nodeV);
checkArgument(
existingIncidentNodes.equals(newIncidentNodes),
REUSING_EDGE,
edge,
existingIncidentNodes,
newIncidentNodes);
return false;
}
NetworkConnections<N, E> connectionsU = nodeConnections.get(nodeU);
if (!allowsParallelEdges()) {
checkArgument(
!(connectionsU != null && connectionsU.successors().contains(nodeV)),
PARALLEL_EDGES_NOT_ALLOWED,
nodeU,
nodeV);
}
boolean isSelfLoop = nodeU.equals(nodeV);
if (!allowsSelfLoops()) {
checkArgument(!isSelfLoop, SELF_LOOPS_NOT_ALLOWED, nodeU);
}
if (connectionsU == null) {
connectionsU = addNodeInternal(nodeU);
}
connectionsU.addOutEdge(edge, nodeV);
NetworkConnections<N, E> connectionsV = nodeConnections.get(nodeV);
if (connectionsV == null) {
connectionsV = addNodeInternal(nodeV);
}
connectionsV.addInEdge(edge, nodeU, isSelfLoop);
edgeToReferenceNode.put(edge, nodeU);
return true;
}
@Override
@CanIgnoreReturnValue
public boolean removeNode(N node) {
checkNotNull(node, "node");
NetworkConnections<N, E> connections = nodeConnections.get(node);
if (connections == null) {
return false;
}
for (E edge : ImmutableList.copyOf(connections.incidentEdges())) {
removeEdge(edge);
}
nodeConnections.remove(node);
return true;
}
@Override
@CanIgnoreReturnValue
public boolean removeEdge(E edge) {
checkNotNull(edge, "edge");
N nodeU = edgeToReferenceNode.get(edge);
if (nodeU == null) {
return false;
}
NetworkConnections<N, E> connectionsU = nodeConnections.get(nodeU);
N nodeV = connectionsU.adjacentNode(edge);
NetworkConnections<N, E> connectionsV = nodeConnections.get(nodeV);
connectionsU.removeOutEdge(edge);
connectionsV.removeInEdge(edge, allowsSelfLoops() && nodeU.equals(nodeV));
edgeToReferenceNode.remove(edge);
return true;
}
private NetworkConnections<N, E> newConnections() {
return isDirected()
? allowsParallelEdges()
? DirectedMultiNetworkConnections.<N, E>of()
: DirectedNetworkConnections.<N, E>of()
: allowsParallelEdges()
? UndirectedMultiNetworkConnections.<N, E>of()
: UndirectedNetworkConnections.<N, E>of();
}
}