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.checkArgument;
020import static com.google.common.graph.GraphConstants.EDGE_CONNECTING_NOT_IN_GRAPH;
021import static com.google.common.graph.GraphConstants.GRAPH_STRING_FORMAT;
022import static com.google.common.graph.GraphConstants.NODE_NOT_IN_GRAPH;
023
024import com.google.common.annotations.Beta;
025import com.google.common.base.Function;
026import com.google.common.collect.Maps;
027import java.util.Map;
028
029/**
030 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend
031 * this class rather than implement {@link ValueGraph} directly.
032 *
033 * @author James Sexton
034 * @param <N> Node parameter type
035 * @param <V> Value parameter type
036 * @since 20.0
037 */
038@Beta
039public abstract class AbstractValueGraph<N, V> extends AbstractGraph<N>
040    implements ValueGraph<N, V> {
041
042  @Override
043  public V edgeValue(Object nodeU, Object nodeV) {
044    V value = edgeValueOrDefault(nodeU, nodeV, null);
045    if (value == null) {
046      checkArgument(nodes().contains(nodeU), NODE_NOT_IN_GRAPH, nodeU);
047      checkArgument(nodes().contains(nodeV), NODE_NOT_IN_GRAPH, nodeV);
048      throw new IllegalArgumentException(String.format(EDGE_CONNECTING_NOT_IN_GRAPH, nodeU, nodeV));
049    }
050    return value;
051  }
052
053  /** Returns a string representation of this graph. */
054  @Override
055  public String toString() {
056    String propertiesString =
057        String.format("isDirected: %s, allowsSelfLoops: %s", isDirected(), allowsSelfLoops());
058    return String.format(GRAPH_STRING_FORMAT, propertiesString, nodes(), edgeValueMap());
059  }
060
061  private Map<EndpointPair<N>, V> edgeValueMap() {
062    Function<EndpointPair<N>, V> edgeToValueFn =
063        new Function<EndpointPair<N>, V>() {
064          @Override
065          public V apply(EndpointPair<N> edge) {
066            return edgeValue(edge.nodeU(), edge.nodeV());
067          }
068        };
069    return Maps.asMap(edges(), edgeToValueFn);
070  }
071}