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.collect.Maps.asMap;
020import static java.util.Objects.requireNonNull;
021
022import java.util.Map;
023import java.util.Optional;
024import java.util.Set;
025import org.jspecify.annotations.Nullable;
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 */
039public abstract class AbstractValueGraph<N, V> extends AbstractBaseGraph<N>
040    implements ValueGraph<N, V> {
041  /** Constructor for use by subclasses. */
042  protected AbstractValueGraph() {}
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 Optional<V> edgeValue(N nodeU, N nodeV) {
111    return Optional.ofNullable(edgeValueOrDefault(nodeU, nodeV, null));
112  }
113
114  @Override
115  public Optional<V> edgeValue(EndpointPair<N> endpoints) {
116    return Optional.ofNullable(edgeValueOrDefault(endpoints, null));
117  }
118
119  @Override
120  public final boolean equals(@Nullable Object obj) {
121    if (obj == this) {
122      return true;
123    }
124    if (!(obj instanceof ValueGraph)) {
125      return false;
126    }
127    ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj;
128
129    return isDirected() == other.isDirected()
130        && nodes().equals(other.nodes())
131        && edgeValueMap(this).equals(edgeValueMap(other));
132  }
133
134  @Override
135  public final int hashCode() {
136    return edgeValueMap(this).hashCode();
137  }
138
139  /** Returns a string representation of this graph. */
140  @Override
141  public String toString() {
142    return "isDirected: "
143        + isDirected()
144        + ", allowsSelfLoops: "
145        + allowsSelfLoops()
146        + ", nodes: "
147        + nodes()
148        + ", edges: "
149        + edgeValueMap(this);
150  }
151
152  private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(ValueGraph<N, V> graph) {
153    return asMap(
154        graph.edges(),
155        edge ->
156            // requireNonNull is safe because the endpoint pair comes from the graph.
157            requireNonNull(graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null)));
158  }
159}