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