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