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 org.jspecify.annotations.Nullable;
021
022/**
023 * This class provides a skeletal implementation of {@link Graph}. It is recommended to extend this
024 * class rather than implement {@link Graph} directly.
025 *
026 * @author James Sexton
027 * @param <N> Node parameter type
028 * @since 20.0
029 */
030@Beta
031public abstract class AbstractGraph<N> extends AbstractBaseGraph<N> implements Graph<N> {
032  /** Constructor for use by subclasses. */
033  public AbstractGraph() {}
034
035  @Override
036  public final boolean equals(@Nullable Object obj) {
037    if (obj == this) {
038      return true;
039    }
040    if (!(obj instanceof Graph)) {
041      return false;
042    }
043    Graph<?> other = (Graph<?>) obj;
044
045    return isDirected() == other.isDirected()
046        && nodes().equals(other.nodes())
047        && edges().equals(other.edges());
048  }
049
050  @Override
051  public final int hashCode() {
052    return edges().hashCode();
053  }
054
055  /** Returns a string representation of this graph. */
056  @Override
057  public String toString() {
058    return "isDirected: "
059        + isDirected()
060        + ", allowsSelfLoops: "
061        + allowsSelfLoops()
062        + ", nodes: "
063        + nodes()
064        + ", edges: "
065        + edges();
066  }
067}