Coverage Summary for Class: UndirectedGraphConnections (com.google.common.graph)
| Class | Method, % | Line, % |
|---|---|---|
| UndirectedGraphConnections | 0% (0/12) | 0% (0/17) |
| UndirectedGraphConnections$1 | 0% (0/2) | 0% (0/2) |
| UndirectedGraphConnections$2 | 0% (0/1) | 0% (0/1) |
| Total | 0% (0/15) | 0% (0/20) |
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 com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.graph.GraphConstants.INNER_CAPACITY; 21 import static com.google.common.graph.GraphConstants.INNER_LOAD_FACTOR; 22 23 import com.google.common.base.Function; 24 import com.google.common.collect.ImmutableMap; 25 import com.google.common.collect.Iterators; 26 import java.util.Collections; 27 import java.util.HashMap; 28 import java.util.Iterator; 29 import java.util.LinkedHashMap; 30 import java.util.Map; 31 import java.util.Set; 32 import javax.annotation.CheckForNull; 33 34 /** 35 * An implementation of {@link GraphConnections} for undirected graphs. 36 * 37 * @author James Sexton 38 * @param <N> Node parameter type 39 * @param <V> Value parameter type 40 */ 41 @ElementTypesAreNonnullByDefault 42 final class UndirectedGraphConnections<N, V> implements GraphConnections<N, V> { 43 private final Map<N, V> adjacentNodeValues; 44 45 private UndirectedGraphConnections(Map<N, V> adjacentNodeValues) { 46 this.adjacentNodeValues = checkNotNull(adjacentNodeValues); 47 } 48 49 static <N, V> UndirectedGraphConnections<N, V> of(ElementOrder<N> incidentEdgeOrder) { 50 switch (incidentEdgeOrder.type()) { 51 case UNORDERED: 52 return new UndirectedGraphConnections<>( 53 new HashMap<N, V>(INNER_CAPACITY, INNER_LOAD_FACTOR)); 54 case STABLE: 55 return new UndirectedGraphConnections<>( 56 new LinkedHashMap<N, V>(INNER_CAPACITY, INNER_LOAD_FACTOR)); 57 default: 58 throw new AssertionError(incidentEdgeOrder.type()); 59 } 60 } 61 62 static <N, V> UndirectedGraphConnections<N, V> ofImmutable(Map<N, V> adjacentNodeValues) { 63 return new UndirectedGraphConnections<>(ImmutableMap.copyOf(adjacentNodeValues)); 64 } 65 66 @Override 67 public Set<N> adjacentNodes() { 68 return Collections.unmodifiableSet(adjacentNodeValues.keySet()); 69 } 70 71 @Override 72 public Set<N> predecessors() { 73 return adjacentNodes(); 74 } 75 76 @Override 77 public Set<N> successors() { 78 return adjacentNodes(); 79 } 80 81 @Override 82 public Iterator<EndpointPair<N>> incidentEdgeIterator(final N thisNode) { 83 return Iterators.transform( 84 adjacentNodeValues.keySet().iterator(), 85 new Function<N, EndpointPair<N>>() { 86 @Override 87 public EndpointPair<N> apply(N incidentNode) { 88 return EndpointPair.unordered(thisNode, incidentNode); 89 } 90 }); 91 } 92 93 @Override 94 @CheckForNull 95 public V value(N node) { 96 return adjacentNodeValues.get(node); 97 } 98 99 @Override 100 public void removePredecessor(N node) { 101 @SuppressWarnings("unused") 102 V unused = removeSuccessor(node); 103 } 104 105 @Override 106 @CheckForNull 107 public V removeSuccessor(N node) { 108 return adjacentNodeValues.remove(node); 109 } 110 111 @Override 112 public void addPredecessor(N node, V value) { 113 @SuppressWarnings("unused") 114 V unused = addSuccessor(node, value); 115 } 116 117 @Override 118 @CheckForNull 119 public V addSuccessor(N node, V value) { 120 return adjacentNodeValues.put(node, value); 121 } 122 }