Coverage Summary for Class: DirectedMultiNetworkConnections (com.google.common.graph)
| Class | Method, % | Line, % |
|---|---|---|
| DirectedMultiNetworkConnections | 0% (0/14) | 0% (0/37) |
| DirectedMultiNetworkConnections$1 | 0% (0/2) | 0% (0/2) |
| Total | 0% (0/16) | 0% (0/39) |
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.checkState; 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.collect.HashMultiset; 24 import com.google.common.collect.ImmutableMap; 25 import com.google.common.collect.Multiset; 26 import com.google.errorprone.annotations.concurrent.LazyInit; 27 import java.lang.ref.Reference; 28 import java.lang.ref.SoftReference; 29 import java.util.Collections; 30 import java.util.HashMap; 31 import java.util.Map; 32 import java.util.Set; 33 import javax.annotation.CheckForNull; 34 35 /** 36 * An implementation of {@link NetworkConnections} for directed networks with parallel edges. 37 * 38 * @author James Sexton 39 * @param <N> Node parameter type 40 * @param <E> Edge parameter type 41 */ 42 @ElementTypesAreNonnullByDefault 43 final class DirectedMultiNetworkConnections<N, E> extends AbstractDirectedNetworkConnections<N, E> { 44 45 private DirectedMultiNetworkConnections( 46 Map<E, N> inEdges, Map<E, N> outEdges, int selfLoopCount) { 47 super(inEdges, outEdges, selfLoopCount); 48 } 49 50 static <N, E> DirectedMultiNetworkConnections<N, E> of() { 51 return new DirectedMultiNetworkConnections<>( 52 new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR), 53 new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR), 54 0); 55 } 56 57 static <N, E> DirectedMultiNetworkConnections<N, E> ofImmutable( 58 Map<E, N> inEdges, Map<E, N> outEdges, int selfLoopCount) { 59 return new DirectedMultiNetworkConnections<>( 60 ImmutableMap.copyOf(inEdges), ImmutableMap.copyOf(outEdges), selfLoopCount); 61 } 62 63 @CheckForNull @LazyInit private transient Reference<Multiset<N>> predecessorsReference; 64 65 @Override 66 public Set<N> predecessors() { 67 return Collections.unmodifiableSet(predecessorsMultiset().elementSet()); 68 } 69 70 private Multiset<N> predecessorsMultiset() { 71 Multiset<N> predecessors = getReference(predecessorsReference); 72 if (predecessors == null) { 73 predecessors = HashMultiset.create(inEdgeMap.values()); 74 predecessorsReference = new SoftReference<>(predecessors); 75 } 76 return predecessors; 77 } 78 79 @CheckForNull @LazyInit private transient Reference<Multiset<N>> successorsReference; 80 81 @Override 82 public Set<N> successors() { 83 return Collections.unmodifiableSet(successorsMultiset().elementSet()); 84 } 85 86 private Multiset<N> successorsMultiset() { 87 Multiset<N> successors = getReference(successorsReference); 88 if (successors == null) { 89 successors = HashMultiset.create(outEdgeMap.values()); 90 successorsReference = new SoftReference<>(successors); 91 } 92 return successors; 93 } 94 95 @Override 96 public Set<E> edgesConnecting(final N node) { 97 return new MultiEdgesConnecting<E>(outEdgeMap, node) { 98 @Override 99 public int size() { 100 return successorsMultiset().count(node); 101 } 102 }; 103 } 104 105 @Override 106 public N removeInEdge(E edge, boolean isSelfLoop) { 107 N node = super.removeInEdge(edge, isSelfLoop); 108 Multiset<N> predecessors = getReference(predecessorsReference); 109 if (predecessors != null) { 110 checkState(predecessors.remove(node)); 111 } 112 return node; 113 } 114 115 @Override 116 public N removeOutEdge(E edge) { 117 N node = super.removeOutEdge(edge); 118 Multiset<N> successors = getReference(successorsReference); 119 if (successors != null) { 120 checkState(successors.remove(node)); 121 } 122 return node; 123 } 124 125 @Override 126 public void addInEdge(E edge, N node, boolean isSelfLoop) { 127 super.addInEdge(edge, node, isSelfLoop); 128 Multiset<N> predecessors = getReference(predecessorsReference); 129 if (predecessors != null) { 130 checkState(predecessors.add(node)); 131 } 132 } 133 134 @Override 135 public void addOutEdge(E edge, N node) { 136 super.addOutEdge(edge, node); 137 Multiset<N> successors = getReference(successorsReference); 138 if (successors != null) { 139 checkState(successors.add(node)); 140 } 141 } 142 143 @CheckForNull 144 private static <T> T getReference(@CheckForNull Reference<T> reference) { 145 return (reference == null) ? null : reference.get(); 146 } 147 }