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 com.google.common.annotations.Beta;
020import com.google.common.base.Function;
021import com.google.common.collect.Maps;
022import java.util.Map;
023import java.util.Set;
024import org.checkerframework.checker.nullness.compatqual.NullableDecl;
025
026/**
027 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend
028 * this class rather than implement {@link ValueGraph} directly.
029 *
030 * <p>The methods implemented in this class should not be overridden unless the subclass admits a
031 * more efficient implementation.
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 AbstractBaseGraph<N>
040    implements ValueGraph<N, V> {
041
042  @Override
043  public Graph<N> asGraph() {
044    return new AbstractGraph<N>() {
045      @Override
046      public Set<N> nodes() {
047        return AbstractValueGraph.this.nodes();
048      }
049
050      @Override
051      public Set<EndpointPair<N>> edges() {
052        return AbstractValueGraph.this.edges();
053      }
054
055      @Override
056      public boolean isDirected() {
057        return AbstractValueGraph.this.isDirected();
058      }
059
060      @Override
061      public boolean allowsSelfLoops() {
062        return AbstractValueGraph.this.allowsSelfLoops();
063      }
064
065      @Override
066      public ElementOrder<N> nodeOrder() {
067        return AbstractValueGraph.this.nodeOrder();
068      }
069
070      @Override
071      public ElementOrder<N> incidentEdgeOrder() {
072        return AbstractValueGraph.this.incidentEdgeOrder();
073      }
074
075      @Override
076      public Set<N> adjacentNodes(N node) {
077        return AbstractValueGraph.this.adjacentNodes(node);
078      }
079
080      @Override
081      public Set<N> predecessors(N node) {
082        return AbstractValueGraph.this.predecessors(node);
083      }
084
085      @Override
086      public Set<N> successors(N node) {
087        return AbstractValueGraph.this.successors(node);
088      }
089
090      @Override
091      public int degree(N node) {
092        return AbstractValueGraph.this.degree(node);
093      }
094
095      @Override
096      public int inDegree(N node) {
097        return AbstractValueGraph.this.inDegree(node);
098      }
099
100      @Override
101      public int outDegree(N node) {
102        return AbstractValueGraph.this.outDegree(node);
103      }
104    };
105  }
106
107  @Override
108  public final boolean equals(@NullableDecl Object obj) {
109    if (obj == this) {
110      return true;
111    }
112    if (!(obj instanceof ValueGraph)) {
113      return false;
114    }
115    ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj;
116
117    return isDirected() == other.isDirected()
118        && nodes().equals(other.nodes())
119        && edgeValueMap(this).equals(edgeValueMap(other));
120  }
121
122  @Override
123  public final int hashCode() {
124    return edgeValueMap(this).hashCode();
125  }
126
127  /** Returns a string representation of this graph. */
128  @Override
129  public String toString() {
130    return "isDirected: "
131        + isDirected()
132        + ", allowsSelfLoops: "
133        + allowsSelfLoops()
134        + ", nodes: "
135        + nodes()
136        + ", edges: "
137        + edgeValueMap(this);
138  }
139
140  private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) {
141    Function<EndpointPair<N>, V> edgeToValueFn =
142        new Function<EndpointPair<N>, V>() {
143          @Override
144          public V apply(EndpointPair<N> edge) {
145            return graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null);
146          }
147        };
148    return Maps.asMap(graph.edges(), edgeToValueFn);
149  }
150}