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 java.util.Objects.requireNonNull; 020 021import com.google.common.annotations.Beta; 022import com.google.common.collect.Maps; 023import java.util.Map; 024import java.util.Set; 025import javax.annotation.CheckForNull; 026 027/** 028 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend 029 * this class rather than implement {@link ValueGraph} directly. 030 * 031 * <p>The methods implemented in this class should not be overridden unless the subclass admits a 032 * more efficient implementation. 033 * 034 * @author James Sexton 035 * @param <N> Node parameter type 036 * @param <V> Value parameter type 037 * @since 20.0 038 */ 039@Beta 040@ElementTypesAreNonnullByDefault 041public abstract class AbstractValueGraph<N, V> extends AbstractBaseGraph<N> 042 implements ValueGraph<N, V> { 043 /** Constructor for use by subclasses. */ 044 public AbstractValueGraph() {} 045 046 @Override 047 public Graph<N> asGraph() { 048 return new AbstractGraph<N>() { 049 @Override 050 public Set<N> nodes() { 051 return AbstractValueGraph.this.nodes(); 052 } 053 054 @Override 055 public Set<EndpointPair<N>> edges() { 056 return AbstractValueGraph.this.edges(); 057 } 058 059 @Override 060 public boolean isDirected() { 061 return AbstractValueGraph.this.isDirected(); 062 } 063 064 @Override 065 public boolean allowsSelfLoops() { 066 return AbstractValueGraph.this.allowsSelfLoops(); 067 } 068 069 @Override 070 public ElementOrder<N> nodeOrder() { 071 return AbstractValueGraph.this.nodeOrder(); 072 } 073 074 @Override 075 public ElementOrder<N> incidentEdgeOrder() { 076 return AbstractValueGraph.this.incidentEdgeOrder(); 077 } 078 079 @Override 080 public Set<N> adjacentNodes(N node) { 081 return AbstractValueGraph.this.adjacentNodes(node); 082 } 083 084 @Override 085 public Set<N> predecessors(N node) { 086 return AbstractValueGraph.this.predecessors(node); 087 } 088 089 @Override 090 public Set<N> successors(N node) { 091 return AbstractValueGraph.this.successors(node); 092 } 093 094 @Override 095 public int degree(N node) { 096 return AbstractValueGraph.this.degree(node); 097 } 098 099 @Override 100 public int inDegree(N node) { 101 return AbstractValueGraph.this.inDegree(node); 102 } 103 104 @Override 105 public int outDegree(N node) { 106 return AbstractValueGraph.this.outDegree(node); 107 } 108 }; 109 } 110 111 @Override 112 public final boolean equals(@CheckForNull Object obj) { 113 if (obj == this) { 114 return true; 115 } 116 if (!(obj instanceof ValueGraph)) { 117 return false; 118 } 119 ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj; 120 121 return isDirected() == other.isDirected() 122 && nodes().equals(other.nodes()) 123 && edgeValueMap(this).equals(edgeValueMap(other)); 124 } 125 126 @Override 127 public final int hashCode() { 128 return edgeValueMap(this).hashCode(); 129 } 130 131 /** Returns a string representation of this graph. */ 132 @Override 133 public String toString() { 134 return "isDirected: " 135 + isDirected() 136 + ", allowsSelfLoops: " 137 + allowsSelfLoops() 138 + ", nodes: " 139 + nodes() 140 + ", edges: " 141 + edgeValueMap(this); 142 } 143 144 private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) { 145 return Maps.asMap( 146 graph.edges(), 147 edge -> 148 // requireNonNull is safe because the endpoint pair comes from the graph. 149 requireNonNull(graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null))); 150 } 151}