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 javax.annotation.CheckForNull;
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
031@ElementTypesAreNonnullByDefault
032public abstract class AbstractGraph<N> extends AbstractBaseGraph<N> implements Graph<N> {
033  /** Constructor for use by subclasses. */
034  public AbstractGraph() {}
035
036  @Override
037  public final boolean equals(@CheckForNull Object obj) {
038    if (obj == this) {
039      return true;
040    }
041    if (!(obj instanceof Graph)) {
042      return false;
043    }
044    Graph<?> other = (Graph<?>) obj;
045
046    return isDirected() == other.isDirected()
047        && nodes().equals(other.nodes())
048        && edges().equals(other.edges());
049  }
050
051  @Override
052  public final int hashCode() {
053    return edges().hashCode();
054  }
055
056  /** Returns a string representation of this graph. */
057  @Override
058  public String toString() {
059    return "isDirected: "
060        + isDirected()
061        + ", allowsSelfLoops: "
062        + allowsSelfLoops()
063        + ", nodes: "
064        + nodes()
065        + ", edges: "
066        + edges();
067  }
068}