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;
026import javax.annotation.Nullable;
027
028/**
029 * A {@link ValueGraph} whose elements and structural relationships will never change. Instances of
030 * this class may be obtained with {@link #copyOf(ValueGraph)}.
031 *
032 * <p>See the Guava User's Guide's <a
033 * href="https://github.com/google/guava/wiki/GraphsExplained#immutable-implementations">discussion
034 * of the {@code Immutable*} types</a> for more information on the properties and guarantees
035 * provided by this class.
036 *
037 * @author James Sexton
038 * @param <N> Node parameter type
039 * @param <V> Value parameter type
040 * @since 20.0
041 */
042@Beta
043public final class ImmutableValueGraph<N, V> extends ImmutableGraph.ValueBackedImpl<N, V>
044    implements ValueGraph<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  private static <N, V> ImmutableMap<N, GraphConnections<N, V>> getNodeConnections(
068      ValueGraph<N, V> graph) {
069    // ImmutableMap.Builder maintains the order of the elements as inserted, so the map will have
070    // whatever ordering the graph's nodes do, so ImmutableSortedMap is unnecessary even if the
071    // input nodes are sorted.
072    ImmutableMap.Builder<N, GraphConnections<N, V>> nodeConnections = ImmutableMap.builder();
073    for (N node : graph.nodes()) {
074      nodeConnections.put(node, connectionsOf(graph, node));
075    }
076    return nodeConnections.build();
077  }
078
079  private static <N, V> GraphConnections<N, V> connectionsOf(
080      final ValueGraph<N, V> graph, final N node) {
081    Function<N, V> successorNodeToValueFn =
082        new Function<N, V>() {
083          @Override
084          public V apply(N successorNode) {
085            return graph.edgeValue(node, successorNode);
086          }
087        };
088    return graph.isDirected()
089        ? DirectedGraphConnections.ofImmutable(
090            graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn))
091        : UndirectedGraphConnections.ofImmutable(
092            Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn));
093  }
094
095  @Override
096  public V edgeValue(Object nodeU, Object nodeV) {
097    return backingValueGraph.edgeValue(nodeU, nodeV);
098  }
099
100  @Override
101  public V edgeValueOrDefault(Object nodeU, Object nodeV, @Nullable V defaultValue) {
102    return backingValueGraph.edgeValueOrDefault(nodeU, nodeV, defaultValue);
103  }
104
105  @Override
106  public String toString() {
107    return backingValueGraph.toString();
108  }
109}