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.graph.GraphConstants.GRAPH_STRING_FORMAT; 020 021import com.google.common.annotations.Beta; 022import javax.annotation.Nullable; 023 024/** 025 * This class provides a skeletal implementation of {@link Graph}. It is recommended to extend this 026 * class rather than implement {@link Graph} directly. 027 * 028 * @author James Sexton 029 * @param <N> Node parameter type 030 * @since 20.0 031 */ 032@Beta 033public abstract class AbstractGraph<N> extends AbstractBaseGraph<N> implements Graph<N> { 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 String propertiesString = 059 String.format("isDirected: %s, allowsSelfLoops: %s", isDirected(), allowsSelfLoops()); 060 return String.format(GRAPH_STRING_FORMAT, propertiesString, nodes(), edges()); 061 } 062}