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