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.checkerframework.checker.nullness.compatqual.NullableDecl; 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 033 @Override 034 public final boolean equals(@NullableDecl Object obj) { 035 if (obj == this) { 036 return true; 037 } 038 if (!(obj instanceof Graph)) { 039 return false; 040 } 041 Graph<?> other = (Graph<?>) obj; 042 043 return isDirected() == other.isDirected() 044 && nodes().equals(other.nodes()) 045 && edges().equals(other.edges()); 046 } 047 048 @Override 049 public final int hashCode() { 050 return edges().hashCode(); 051 } 052 053 /** Returns a string representation of this graph. */ 054 @Override 055 public String toString() { 056 return "isDirected: " 057 + isDirected() 058 + ", allowsSelfLoops: " 059 + allowsSelfLoops() 060 + ", nodes: " 061 + nodes() 062 + ", edges: " 063 + edges(); 064 } 065}