View Javadoc
1   /*
2    * Copyright (C) 2016 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.collect.ImmutableMap;
24  import com.google.common.collect.Maps;
25  import com.google.errorprone.annotations.Immutable;
26  
27  /**
28   * A {@link ValueGraph} whose elements and structural relationships will never change. Instances of
29   * this class may be obtained with {@link #copyOf(ValueGraph)}.
30   *
31   * <p>See the Guava User's Guide's <a
32   * href="https://github.com/google/guava/wiki/GraphsExplained#immutable-implementations">discussion
33   * of the {@code Immutable*} types</a> for more information on the properties and guarantees
34   * provided by this class.
35   *
36   * @author James Sexton
37   * @param <N> Node parameter type
38   * @param <V> Value parameter type
39   * @since 20.0
40   */
41  @Beta
42  @Immutable(containerOf = {"N", "V"})
43  @SuppressWarnings("Immutable") // Extends ConfigurableValueGraph but uses ImmutableMaps.
44  public final class ImmutableValueGraph<N, V> extends ConfigurableValueGraph<N, V> {
45  
46    private ImmutableValueGraph(ValueGraph<N, V> graph) {
47      super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size());
48    }
49  
50    /** Returns an immutable copy of {@code graph}. */
51    public static <N, V> ImmutableValueGraph<N, V> copyOf(ValueGraph<N, V> graph) {
52      return (graph instanceof ImmutableValueGraph)
53          ? (ImmutableValueGraph<N, V>) graph
54          : new ImmutableValueGraph<N, V>(graph);
55    }
56  
57    /**
58     * Simply returns its argument.
59     *
60     * @deprecated no need to use this
61     */
62    @Deprecated
63    public static <N, V> ImmutableValueGraph<N, V> copyOf(ImmutableValueGraph<N, V> graph) {
64      return checkNotNull(graph);
65    }
66  
67    @Override
68    public ImmutableGraph<N> asGraph() {
69      return new ImmutableGraph<N>(this); // safe because the view is effectively immutable
70    }
71  
72    private static <N, V> ImmutableMap<N, GraphConnections<N, V>> getNodeConnections(
73        ValueGraph<N, V> 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, V>> 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, V> GraphConnections<N, V> connectionsOf(
85        final ValueGraph<N, V> graph, final N node) {
86      Function<N, V> successorNodeToValueFn =
87          new Function<N, V>() {
88            @Override
89            public V apply(N successorNode) {
90              return graph.edgeValueOrDefault(node, successorNode, null);
91            }
92          };
93      return graph.isDirected()
94          ? DirectedGraphConnections.ofImmutable(
95              graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn))
96          : UndirectedGraphConnections.ofImmutable(
97              Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn));
98    }
99  }