001/*
002 * Copyright (C) 2016 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.graph;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.Beta;
022import com.google.common.base.Function;
023import com.google.common.collect.ImmutableMap;
024import com.google.common.collect.Maps;
025import com.google.errorprone.annotations.Immutable;
026
027/**
028 * A {@link ValueGraph} whose elements and structural relationships will never change. Instances of
029 * this class may be obtained with {@link #copyOf(ValueGraph)}.
030 *
031 * <p>See the Guava User's Guide's <a
032 * href="https://github.com/google/guava/wiki/GraphsExplained#immutable-implementations">discussion
033 * of the {@code Immutable*} types</a> for more information on the properties and guarantees
034 * provided by this class.
035 *
036 * @author James Sexton
037 * @param <N> Node parameter type
038 * @param <V> Value parameter type
039 * @since 20.0
040 */
041@Beta
042@Immutable(containerOf = {"N", "V"})
043@SuppressWarnings("Immutable") // Extends ConfigurableValueGraph but uses ImmutableMaps.
044public final class ImmutableValueGraph<N, V> extends ConfigurableValueGraph<N, V> {
045
046  private ImmutableValueGraph(ValueGraph<N, V> graph) {
047    super(ValueGraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size());
048  }
049
050  /** Returns an immutable copy of {@code graph}. */
051  public static <N, V> ImmutableValueGraph<N, V> copyOf(ValueGraph<N, V> graph) {
052    return (graph instanceof ImmutableValueGraph)
053        ? (ImmutableValueGraph<N, V>) graph
054        : new ImmutableValueGraph<N, V>(graph);
055  }
056
057  /**
058   * Simply returns its argument.
059   *
060   * @deprecated no need to use this
061   */
062  @Deprecated
063  public static <N, V> ImmutableValueGraph<N, V> copyOf(ImmutableValueGraph<N, V> graph) {
064    return checkNotNull(graph);
065  }
066
067  @Override
068  public ImmutableGraph<N> asGraph() {
069    return new ImmutableGraph<N>(this); // safe because the view is effectively immutable
070  }
071
072  private static <N, V> ImmutableMap<N, GraphConnections<N, V>> getNodeConnections(
073      ValueGraph<N, V> graph) {
074    // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
075    // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
076    // input nodes are sorted.
077    ImmutableMap.Builder<N, GraphConnections<N, V>> nodeConnections = ImmutableMap.builder();
078    for (N node : graph.nodes()) {
079      nodeConnections.put(node, connectionsOf(graph, node));
080    }
081    return nodeConnections.build();
082  }
083
084  private static <N, V> GraphConnections<N, V> connectionsOf(
085      final ValueGraph<N, V> graph, final N node) {
086    Function<N, V> successorNodeToValueFn =
087        new Function<N, V>() {
088          @Override
089          public V apply(N successorNode) {
090            return graph.edgeValueOrDefault(node, successorNode, null);
091          }
092        };
093    return graph.isDirected()
094        ? DirectedGraphConnections.ofImmutable(
095            graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn))
096        : UndirectedGraphConnections.ofImmutable(
097            Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn));
098  }
099}