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;
028import java.util.Set;
029import javax.annotation.Nullable;
030
031/**
032 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend
033 * this class rather than implement {@link ValueGraph} directly.
034 *
035 * @author James Sexton
036 * @param <N> Node parameter type
037 * @param <V> Value parameter type
038 * @since 20.0
039 */
040@Beta
041public abstract class AbstractValueGraph<N, V> extends AbstractBaseGraph<N>
042    implements ValueGraph<N, V> {
043
044  @Override
045  public Graph<N> asGraph() {
046    return new AbstractGraph<N>() {
047      @Override
048      public Set<N> nodes() {
049        return AbstractValueGraph.this.nodes();
050      }
051
052      @Override
053      public Set<EndpointPair<N>> edges() {
054        return AbstractValueGraph.this.edges();
055      }
056
057      @Override
058      public boolean isDirected() {
059        return AbstractValueGraph.this.isDirected();
060      }
061
062      @Override
063      public boolean allowsSelfLoops() {
064        return AbstractValueGraph.this.allowsSelfLoops();
065      }
066
067      @Override
068      public ElementOrder<N> nodeOrder() {
069        return AbstractValueGraph.this.nodeOrder();
070      }
071
072      @Override
073      public Set<N> adjacentNodes(N node) {
074        return AbstractValueGraph.this.adjacentNodes(node);
075      }
076
077      @Override
078      public Set<N> predecessors(N node) {
079        return AbstractValueGraph.this.predecessors(node);
080      }
081
082      @Override
083      public Set<N> successors(N node) {
084        return AbstractValueGraph.this.successors(node);
085      }
086
087      @Override
088      public int degree(N node) {
089        return AbstractValueGraph.this.degree(node);
090      }
091
092      @Override
093      public int inDegree(N node) {
094        return AbstractValueGraph.this.inDegree(node);
095      }
096
097      @Override
098      public int outDegree(N node) {
099        return AbstractValueGraph.this.outDegree(node);
100      }
101    };
102  }
103
104  @Override
105  public V edgeValue(N nodeU, N nodeV) {
106    V value = edgeValueOrDefault(nodeU, nodeV, null);
107    if (value == null) {
108      checkArgument(nodes().contains(nodeU), NODE_NOT_IN_GRAPH, nodeU);
109      checkArgument(nodes().contains(nodeV), NODE_NOT_IN_GRAPH, nodeV);
110      throw new IllegalArgumentException(String.format(EDGE_CONNECTING_NOT_IN_GRAPH, nodeU, nodeV));
111    }
112    return value;
113  }
114
115  @Override
116  public final boolean equals(@Nullable Object obj) {
117    if (obj == this) {
118      return true;
119    }
120    if (!(obj instanceof ValueGraph)) {
121      return false;
122    }
123    ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj;
124
125    return isDirected() == other.isDirected()
126        && nodes().equals(other.nodes())
127        && edgeValueMap(this).equals(edgeValueMap(other));
128  }
129
130  @Override
131  public final int hashCode() {
132    return edgeValueMap(this).hashCode();
133  }
134
135  /** Returns a string representation of this graph. */
136  @Override
137  public String toString() {
138    String propertiesString =
139        String.format("isDirected: %s, allowsSelfLoops: %s", isDirected(), allowsSelfLoops());
140    return String.format(GRAPH_STRING_FORMAT, propertiesString, nodes(), edgeValueMap(this));
141  }
142
143  private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) {
144    Function<EndpointPair<N>, V> edgeToValueFn =
145        new Function<EndpointPair<N>, V>() {
146          @Override
147          public V apply(EndpointPair<N> edge) {
148            return graph.edgeValue(edge.nodeU(), edge.nodeV());
149          }
150        };
151    return Maps.asMap(graph.edges(), edgeToValueFn);
152  }
153}