View Javadoc
1   /*
2    * Copyright (C) 2014 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.graph;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import com.google.common.annotations.Beta;
22  import com.google.common.base.Function;
23  import com.google.common.base.Functions;
24  import com.google.common.collect.ImmutableMap;
25  import com.google.common.collect.Maps;
26  import com.google.common.graph.GraphConstants.Presence;
27  import com.google.errorprone.annotations.Immutable;
28  
29  /**
30   * A {@link Graph} whose elements and structural relationships will never change. Instances of this
31   * class may be obtained with {@link #copyOf(Graph)}.
32   *
33   * <p>See the Guava User's Guide's <a
34   * href="https://github.com/google/guava/wiki/GraphsExplained#immutable-implementations">discussion
35   * of the {@code Immutable*} types</a> for more information on the properties and guarantees
36   * provided by this class.
37   *
38   * @author James Sexton
39   * @author Joshua O'Madadhain
40   * @author Omar Darwish
41   * @param <N> Node parameter type
42   * @since 20.0
43   */
44  @Beta
45  @Immutable(containerOf = {"N"})
46  public class ImmutableGraph<N> extends ForwardingGraph<N> {
47    @SuppressWarnings("Immutable") // The backing graph must be immutable.
48    private final BaseGraph<N> backingGraph;
49  
50    ImmutableGraph(BaseGraph<N> backingGraph) {
51      this.backingGraph = backingGraph;
52    }
53  
54    /** Returns an immutable copy of {@code graph}. */
55    public static <N> ImmutableGraph<N> copyOf(Graph<N> graph) {
56      return (graph instanceof ImmutableGraph)
57          ? (ImmutableGraph<N>) graph
58          : new ImmutableGraph<N>(new ConfigurableValueGraph<N, Presence>(
59              GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size()));
60    }
61  
62    /**
63     * Simply returns its argument.
64     *
65     * @deprecated no need to use this
66     */
67    @Deprecated
68    public static <N> ImmutableGraph<N> copyOf(ImmutableGraph<N> graph) {
69      return checkNotNull(graph);
70    }
71  
72    private static <N> ImmutableMap<N, GraphConnections<N, Presence>> getNodeConnections(
73        Graph<N> graph) {
74      // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
75      // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
76      // input nodes are sorted.
77      ImmutableMap.Builder<N, GraphConnections<N, Presence>> nodeConnections = ImmutableMap.builder();
78      for (N node : graph.nodes()) {
79        nodeConnections.put(node, connectionsOf(graph, node));
80      }
81      return nodeConnections.build();
82    }
83  
84    private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) {
85      Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS);
86      return graph.isDirected()
87          ? DirectedGraphConnections.ofImmutable(
88              graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn))
89          : UndirectedGraphConnections.ofImmutable(
90              Maps.asMap(graph.adjacentNodes(node), edgeValueFn));
91    }
92  
93    @Override
94    protected BaseGraph<N> delegate() {
95      return backingGraph;
96    }
97  }