Coverage Summary for Class: AbstractValueGraph (com.google.common.graph)
| Class | Method, % | Line, % |
|---|---|---|
| AbstractValueGraph | 0% (0/8) | 0% (0/20) |
| AbstractValueGraph$1 | 0% (0/13) | 0% (0/13) |
| AbstractValueGraph$2 | 0% (0/2) | 0% (0/2) |
| Total | 0% (0/23) | 0% (0/35) |
1 /* 2 * Copyright (C) 2016 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.graph; 18 19 import static java.util.Objects.requireNonNull; 20 21 import com.google.common.annotations.Beta; 22 import com.google.common.base.Function; 23 import com.google.common.collect.Maps; 24 import java.util.Map; 25 import java.util.Optional; 26 import java.util.Set; 27 import javax.annotation.CheckForNull; 28 29 /** 30 * This class provides a skeletal implementation of {@link ValueGraph}. It is recommended to extend 31 * this class rather than implement {@link ValueGraph} directly. 32 * 33 * <p>The methods implemented in this class should not be overridden unless the subclass admits a 34 * more efficient implementation. 35 * 36 * @author James Sexton 37 * @param <N> Node parameter type 38 * @param <V> Value parameter type 39 * @since 20.0 40 */ 41 @Beta 42 @ElementTypesAreNonnullByDefault 43 public abstract class AbstractValueGraph<N, V> extends AbstractBaseGraph<N> 44 implements ValueGraph<N, V> { 45 46 @Override 47 public Graph<N> asGraph() { 48 return new AbstractGraph<N>() { 49 @Override 50 public Set<N> nodes() { 51 return AbstractValueGraph.this.nodes(); 52 } 53 54 @Override 55 public Set<EndpointPair<N>> edges() { 56 return AbstractValueGraph.this.edges(); 57 } 58 59 @Override 60 public boolean isDirected() { 61 return AbstractValueGraph.this.isDirected(); 62 } 63 64 @Override 65 public boolean allowsSelfLoops() { 66 return AbstractValueGraph.this.allowsSelfLoops(); 67 } 68 69 @Override 70 public ElementOrder<N> nodeOrder() { 71 return AbstractValueGraph.this.nodeOrder(); 72 } 73 74 @Override 75 public ElementOrder<N> incidentEdgeOrder() { 76 return AbstractValueGraph.this.incidentEdgeOrder(); 77 } 78 79 @Override 80 public Set<N> adjacentNodes(N node) { 81 return AbstractValueGraph.this.adjacentNodes(node); 82 } 83 84 @Override 85 public Set<N> predecessors(N node) { 86 return AbstractValueGraph.this.predecessors(node); 87 } 88 89 @Override 90 public Set<N> successors(N node) { 91 return AbstractValueGraph.this.successors(node); 92 } 93 94 @Override 95 public int degree(N node) { 96 return AbstractValueGraph.this.degree(node); 97 } 98 99 @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 Optional<V> edgeValue(N nodeU, N nodeV) { 113 return Optional.ofNullable(edgeValueOrDefault(nodeU, nodeV, null)); 114 } 115 116 @Override 117 public Optional<V> edgeValue(EndpointPair<N> endpoints) { 118 return Optional.ofNullable(edgeValueOrDefault(endpoints, null)); 119 } 120 121 @Override 122 public final boolean equals(@CheckForNull Object obj) { 123 if (obj == this) { 124 return true; 125 } 126 if (!(obj instanceof ValueGraph)) { 127 return false; 128 } 129 ValueGraph<?, ?> other = (ValueGraph<?, ?>) obj; 130 131 return isDirected() == other.isDirected() 132 && nodes().equals(other.nodes()) 133 && edgeValueMap(this).equals(edgeValueMap(other)); 134 } 135 136 @Override 137 public final int hashCode() { 138 return edgeValueMap(this).hashCode(); 139 } 140 141 /** Returns a string representation of this graph. */ 142 @Override 143 public String toString() { 144 return "isDirected: " 145 + isDirected() 146 + ", allowsSelfLoops: " 147 + allowsSelfLoops() 148 + ", nodes: " 149 + nodes() 150 + ", edges: " 151 + edgeValueMap(this); 152 } 153 154 private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) { 155 Function<EndpointPair<N>, V> edgeToValueFn = 156 new Function<EndpointPair<N>, V>() { 157 @Override 158 public V apply(EndpointPair<N> edge) { 159 // requireNonNull is safe because the endpoint pair comes from the graph. 160 return requireNonNull(graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null)); 161 } 162 }; 163 return Maps.asMap(graph.edges(), edgeToValueFn); 164 } 165 }