Coverage Summary for Class: AbstractDirectedNetworkConnections (com.google.common.graph)
| Class | Method, % | Line, % |
|---|---|---|
| AbstractDirectedNetworkConnections | 0% (0/11) | 0% (0/27) |
| AbstractDirectedNetworkConnections$1 | 0% (0/4) | 0% (0/7) |
| Total | 0% (0/15) | 0% (0/34) |
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.base.Preconditions.checkState; 21 import static com.google.common.graph.Graphs.checkNonNegative; 22 import static com.google.common.graph.Graphs.checkPositive; 23 import static java.util.Objects.requireNonNull; 24 25 import com.google.common.collect.Iterables; 26 import com.google.common.collect.Iterators; 27 import com.google.common.collect.Sets; 28 import com.google.common.collect.UnmodifiableIterator; 29 import com.google.common.math.IntMath; 30 import java.util.AbstractSet; 31 import java.util.Collections; 32 import java.util.Map; 33 import java.util.Set; 34 import javax.annotation.CheckForNull; 35 36 /** 37 * A base implementation of {@link NetworkConnections} for directed networks. 38 * 39 * @author James Sexton 40 * @param <N> Node parameter type 41 * @param <E> Edge parameter type 42 */ 43 @ElementTypesAreNonnullByDefault 44 abstract class AbstractDirectedNetworkConnections<N, E> implements NetworkConnections<N, E> { 45 /** Keys are edges incoming to the origin node, values are the source node. */ 46 final Map<E, N> inEdgeMap; 47 48 /** Keys are edges outgoing from the origin node, values are the target node. */ 49 final Map<E, N> outEdgeMap; 50 51 private int selfLoopCount; 52 53 AbstractDirectedNetworkConnections(Map<E, N> inEdgeMap, Map<E, N> outEdgeMap, int selfLoopCount) { 54 this.inEdgeMap = checkNotNull(inEdgeMap); 55 this.outEdgeMap = checkNotNull(outEdgeMap); 56 this.selfLoopCount = checkNonNegative(selfLoopCount); 57 checkState(selfLoopCount <= inEdgeMap.size() && selfLoopCount <= outEdgeMap.size()); 58 } 59 60 @Override 61 public Set<N> adjacentNodes() { 62 return Sets.union(predecessors(), successors()); 63 } 64 65 @Override 66 public Set<E> incidentEdges() { 67 return new AbstractSet<E>() { 68 @Override 69 public UnmodifiableIterator<E> iterator() { 70 Iterable<E> incidentEdges = 71 (selfLoopCount == 0) 72 ? Iterables.concat(inEdgeMap.keySet(), outEdgeMap.keySet()) 73 : Sets.union(inEdgeMap.keySet(), outEdgeMap.keySet()); 74 return Iterators.unmodifiableIterator(incidentEdges.iterator()); 75 } 76 77 @Override 78 public int size() { 79 return IntMath.saturatedAdd(inEdgeMap.size(), outEdgeMap.size() - selfLoopCount); 80 } 81 82 @Override 83 public boolean contains(@CheckForNull Object obj) { 84 return inEdgeMap.containsKey(obj) || outEdgeMap.containsKey(obj); 85 } 86 }; 87 } 88 89 @Override 90 public Set<E> inEdges() { 91 return Collections.unmodifiableSet(inEdgeMap.keySet()); 92 } 93 94 @Override 95 public Set<E> outEdges() { 96 return Collections.unmodifiableSet(outEdgeMap.keySet()); 97 } 98 99 @Override 100 public N adjacentNode(E edge) { 101 // Since the reference node is defined to be 'source' for directed graphs, 102 // we can assume this edge lives in the set of outgoing edges. 103 // (We're relying on callers to call this method only with an edge that's in the graph.) 104 return requireNonNull(outEdgeMap.get(edge)); 105 } 106 107 @Override 108 public N removeInEdge(E edge, boolean isSelfLoop) { 109 if (isSelfLoop) { 110 checkNonNegative(--selfLoopCount); 111 } 112 N previousNode = inEdgeMap.remove(edge); 113 // We're relying on callers to call this method only with an edge that's in the graph. 114 return requireNonNull(previousNode); 115 } 116 117 @Override 118 public N removeOutEdge(E edge) { 119 N previousNode = outEdgeMap.remove(edge); 120 // We're relying on callers to call this method only with an edge that's in the graph. 121 return requireNonNull(previousNode); 122 } 123 124 @Override 125 public void addInEdge(E edge, N node, boolean isSelfLoop) { 126 checkNotNull(edge); 127 checkNotNull(node); 128 129 if (isSelfLoop) { 130 checkPositive(++selfLoopCount); 131 } 132 N previousNode = inEdgeMap.put(edge, node); 133 checkState(previousNode == null); 134 } 135 136 @Override 137 public void addOutEdge(E edge, N node) { 138 checkNotNull(edge); 139 checkNotNull(node); 140 141 N previousNode = outEdgeMap.put(edge, node); 142 checkState(previousNode == null); 143 } 144 }