package com.google.common.graph;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.errorprone.annotations.Immutable;
import java.util.Map;
@Beta
@Immutable(containerOf = {"N", "E"})
@SuppressWarnings("Immutable")
public final class ImmutableNetwork<N, E> extends ConfigurableNetwork<N, E> {
private ImmutableNetwork(Network<N, E> network) {
super(
NetworkBuilder.from(network), getNodeConnections(network), getEdgeToReferenceNode(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);
}
@Deprecated
public static <N, E> ImmutableNetwork<N, E> copyOf(ImmutableNetwork<N, E> network) {
return checkNotNull(network);
}
@Override
public ImmutableGraph<N> asGraph() {
return new ImmutableGraph<N>(super.asGraph());
}
private static <N, E> Map<N, NetworkConnections<N, E>> getNodeConnections(Network<N, E> network) {
ImmutableMap.Builder<N, NetworkConnections<N, E>> nodeConnections = ImmutableMap.builder();
for (N node : network.nodes()) {
nodeConnections.put(node, connectionsOf(network, node));
}
return nodeConnections.build();
}
private static <N, E> Map<E, N> getEdgeToReferenceNode(Network<N, E> network) {
ImmutableMap.Builder<E, N> edgeToReferenceNode = ImmutableMap.builder();
for (E edge : network.edges()) {
edgeToReferenceNode.put(edge, network.incidentNodes(edge).nodeU());
}
return edgeToReferenceNode.build();
}
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> Function<E, N> sourceNodeFn(final Network<N, E> network) {
return new Function<E, N>() {
@Override
public N apply(E edge) {
return network.incidentNodes(edge).source();
}
};
}
private static <N, E> Function<E, N> targetNodeFn(final Network<N, E> network) {
return new Function<E, N>() {
@Override
public N apply(E edge) {
return network.incidentNodes(edge).target();
}
};
}
private static <N, E> Function<E, N> adjacentNodeFn(final Network<N, E> network, final N node) {
return new Function<E, N>() {
@Override
public N apply(E edge) {
return network.incidentNodes(edge).adjacentNode(node);
}
};
}
}