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 java.util.Objects.requireNonNull;
020
021import com.google.common.annotations.Beta;
022import com.google.common.collect.Maps;
023import java.util.Map;
024import java.util.Set;
025import javax.annotation.CheckForNull;
026
027/**
028 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend
029 * this class rather than implement {@link ValueGraph} directly.
030 *
031 * <p>The methods implemented in this class should not be overridden unless the subclass admits a
032 * more efficient implementation.
033 *
034 * @author James Sexton
035 * @param <N> Node parameter type
036 * @param <V> Value parameter type
037 * @since 20.0
038 */
039@Beta
040@ElementTypesAreNonnullByDefault
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 ElementOrder<N> incidentEdgeOrder() {
074        return AbstractValueGraph.this.incidentEdgeOrder();
075      }
076
077      @Override
078      public Set<N> adjacentNodes(N node) {
079        return AbstractValueGraph.this.adjacentNodes(node);
080      }
081
082      @Override
083      public Set<N> predecessors(N node) {
084        return AbstractValueGraph.this.predecessors(node);
085      }
086
087      @Override
088      public Set<N> successors(N node) {
089        return AbstractValueGraph.this.successors(node);
090      }
091
092      @Override
093      public int degree(N node) {
094        return AbstractValueGraph.this.degree(node);
095      }
096
097      @Override
098      public int inDegree(N node) {
099        return AbstractValueGraph.this.inDegree(node);
100      }
101
102      @Override
103      public int outDegree(N node) {
104        return AbstractValueGraph.this.outDegree(node);
105      }
106    };
107  }
108
109  @Override
110  public final boolean equals(@CheckForNull Object obj) {
111    if (obj == this) {
112      return true;
113    }
114    if (!(obj instanceof ValueGraph)) {
115      return false;
116    }
117    ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj;
118
119    return isDirected() == other.isDirected()
120        && nodes().equals(other.nodes())
121        && edgeValueMap(this).equals(edgeValueMap(other));
122  }
123
124  @Override
125  public final int hashCode() {
126    return edgeValueMap(this).hashCode();
127  }
128
129  /** Returns a string representation of this graph. */
130  @Override
131  public String toString() {
132    return "isDirected: "
133        + isDirected()
134        + ", allowsSelfLoops: "
135        + allowsSelfLoops()
136        + ", nodes: "
137        + nodes()
138        + ", edges: "
139        + edgeValueMap(this);
140  }
141
142  private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) {
143    return Maps.asMap(
144        graph.edges(),
145        edge ->
146            // requireNonNull is safe because the endpoint pair comes from the graph.
147            requireNonNull(graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null)));
148  }
149}