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