com.google.common.graph.ImmutableValueGraph Maven / Gradle / Ivy
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.graph;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;
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.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
/**
* A {@link ValueGraph} whose elements and structural relationships will never change. Instances of
* this class may be obtained with {@link #copyOf(ValueGraph)}.
*
* See the Guava User's Guide's discussion
* of the {@code Immutable*} types for more information on the properties and guarantees
* provided by this class.
*
* @author James Sexton
* @author Jens Nyman
* @param Node parameter type
* @param Value parameter type
* @since 20.0
*/
@Beta
@Immutable(containerOf = {"N", "V"})
@SuppressWarnings("Immutable") // Extends StandardValueGraph but uses ImmutableMaps.
@ElementTypesAreNonnullByDefault
public final class ImmutableValueGraph extends StandardValueGraph {
private ImmutableValueGraph(ValueGraph graph) {
super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size());
}
/** Returns an immutable copy of {@code graph}. */
public static ImmutableValueGraph copyOf(ValueGraph graph) {
return (graph instanceof ImmutableValueGraph)
? (ImmutableValueGraph) graph
: new ImmutableValueGraph(graph);
}
/**
* Simply returns its argument.
*
* @deprecated no need to use this
*/
@Deprecated
public static ImmutableValueGraph copyOf(ImmutableValueGraph graph) {
return checkNotNull(graph);
}
@Override
public ElementOrder incidentEdgeOrder() {
return ElementOrder.stable();
}
@Override
public ImmutableGraph asGraph() {
return new ImmutableGraph<>(this); // safe because the view is effectively immutable
}
private static ImmutableMap> getNodeConnections(
ValueGraph graph) {
// ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
// whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
// input nodes are sorted.
ImmutableMap.Builder> nodeConnections = ImmutableMap.builder();
for (N node : graph.nodes()) {
nodeConnections.put(node, connectionsOf(graph, node));
}
return nodeConnections.buildOrThrow();
}
private static GraphConnections connectionsOf(ValueGraph graph, N node) {
Function successorNodeToValueFn =
(N successorNode) ->
// requireNonNull is safe because the endpoint pair comes from the graph.
requireNonNull(graph.edgeValueOrDefault(node, successorNode, null));
return graph.isDirected()
? DirectedGraphConnections.ofImmutable(
node, graph.incidentEdges(node), successorNodeToValueFn)
: UndirectedGraphConnections.ofImmutable(
Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn));
}
/**
* A builder for creating {@link ImmutableValueGraph} instances, especially {@code static final}
* graphs. Example:
*
* {@code
* static final ImmutableValueGraph CITY_ROAD_DISTANCE_GRAPH =
* ValueGraphBuilder.undirected()
* .immutable()
* .putEdgeValue(PARIS, BERLIN, kilometers(1060))
* .putEdgeValue(PARIS, BRUSSELS, kilometers(317))
* .putEdgeValue(BERLIN, BRUSSELS, kilometers(764))
* .addNode(REYKJAVIK)
* .build();
* }
*
* Builder instances can be reused; it is safe to call {@link #build} multiple times to build
* multiple graphs in series. Each new graph contains all the elements of the ones created before
* it.
*
* @since 28.0
*/
public static class Builder {
private final MutableValueGraph mutableValueGraph;
Builder(ValueGraphBuilder graphBuilder) {
// The incidentEdgeOrder for immutable graphs is always stable. However, we don't want to
// modify this builder, so we make a copy instead.
this.mutableValueGraph =
graphBuilder.copy().incidentEdgeOrder(ElementOrder.stable()).build();
}
/**
* Adds {@code node} if it is not already present.
*
* Nodes must be unique, just as {@code Map} keys must be. They must also be non-null.
*
* @return this {@code Builder} object
*/
@CanIgnoreReturnValue
public ImmutableValueGraph.Builder addNode(N node) {
mutableValueGraph.addNode(node);
return this;
}
/**
* Adds an edge connecting {@code nodeU} to {@code nodeV} if one is not already present, and
* sets a value for that edge to {@code value} (overwriting the existing value, if any).
*
* If the graph is directed, the resultant edge will be directed; otherwise, it will be
* undirected.
*
*
Values do not have to be unique. However, values must be non-null.
*
*
If {@code nodeU} and {@code nodeV} are not already present in this graph, this method will
* silently {@link #addNode(Object) add} {@code nodeU} and {@code nodeV} to the graph.
*
* @return this {@code Builder} object
* @throws IllegalArgumentException if the introduction of the edge would violate {@link
* #allowsSelfLoops()}
*/
@CanIgnoreReturnValue
public ImmutableValueGraph.Builder putEdgeValue(N nodeU, N nodeV, V value) {
mutableValueGraph.putEdgeValue(nodeU, nodeV, value);
return this;
}
/**
* Adds an edge connecting {@code endpoints} if one is not already present, and sets a value for
* that edge to {@code value} (overwriting the existing value, if any).
*
* If the graph is directed, the resultant edge will be directed; otherwise, it will be
* undirected.
*
*
If this graph is directed, {@code endpoints} must be ordered.
*
*
Values do not have to be unique. However, values must be non-null.
*
*
If either or both endpoints are not already present in this graph, this method will
* silently {@link #addNode(Object) add} each missing endpoint to the graph.
*
* @return this {@code Builder} object
* @throws IllegalArgumentException if the introduction of the edge would violate {@link
* #allowsSelfLoops()}
* @throws IllegalArgumentException if the endpoints are unordered and the graph is directed
*/
@CanIgnoreReturnValue
public ImmutableValueGraph.Builder putEdgeValue(EndpointPair endpoints, V value) {
mutableValueGraph.putEdgeValue(endpoints, value);
return this;
}
/**
* Returns a newly-created {@code ImmutableValueGraph} based on the contents of this {@code
* Builder}.
*/
public ImmutableValueGraph build() {
return ImmutableValueGraph.copyOf(mutableValueGraph);
}
}
}