Coverage Summary for Class: UndirectedMultiNetworkConnections (com.google.common.graph)

Class Method, % Line, %
UndirectedMultiNetworkConnections 0% (0/12) 0% (0/26)
UndirectedMultiNetworkConnections$1 0% (0/2) 0% (0/2)
Total 0% (0/14) 0% (0/28)


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 undirected 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 UndirectedMultiNetworkConnections<N, E> 44  extends AbstractUndirectedNetworkConnections<N, E> { 45  46  private UndirectedMultiNetworkConnections(Map<E, N> incidentEdges) { 47  super(incidentEdges); 48  } 49  50  static <N, E> UndirectedMultiNetworkConnections<N, E> of() { 51  return new UndirectedMultiNetworkConnections<>( 52  new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR)); 53  } 54  55  static <N, E> UndirectedMultiNetworkConnections<N, E> ofImmutable(Map<E, N> incidentEdges) { 56  return new UndirectedMultiNetworkConnections<>(ImmutableMap.copyOf(incidentEdges)); 57  } 58  59  @CheckForNull @LazyInit private transient Reference<Multiset<N>> adjacentNodesReference; 60  61  @Override 62  public Set<N> adjacentNodes() { 63  return Collections.unmodifiableSet(adjacentNodesMultiset().elementSet()); 64  } 65  66  private Multiset<N> adjacentNodesMultiset() { 67  Multiset<N> adjacentNodes = getReference(adjacentNodesReference); 68  if (adjacentNodes == null) { 69  adjacentNodes = HashMultiset.create(incidentEdgeMap.values()); 70  adjacentNodesReference = new SoftReference<>(adjacentNodes); 71  } 72  return adjacentNodes; 73  } 74  75  @Override 76  public Set<E> edgesConnecting(final N node) { 77  return new MultiEdgesConnecting<E>(incidentEdgeMap, node) { 78  @Override 79  public int size() { 80  return adjacentNodesMultiset().count(node); 81  } 82  }; 83  } 84  85  @Override 86  @CheckForNull 87  public N removeInEdge(E edge, boolean isSelfLoop) { 88  if (!isSelfLoop) { 89  return removeOutEdge(edge); 90  } 91  return null; 92  } 93  94  @Override 95  public N removeOutEdge(E edge) { 96  N node = super.removeOutEdge(edge); 97  Multiset<N> adjacentNodes = getReference(adjacentNodesReference); 98  if (adjacentNodes != null) { 99  checkState(adjacentNodes.remove(node)); 100  } 101  return node; 102  } 103  104  @Override 105  public void addInEdge(E edge, N node, boolean isSelfLoop) { 106  if (!isSelfLoop) { 107  addOutEdge(edge, node); 108  } 109  } 110  111  @Override 112  public void addOutEdge(E edge, N node) { 113  super.addOutEdge(edge, node); 114  Multiset<N> adjacentNodes = getReference(adjacentNodesReference); 115  if (adjacentNodes != null) { 116  checkState(adjacentNodes.add(node)); 117  } 118  } 119  120  @CheckForNull 121  private static <T> T getReference(@CheckForNull Reference<T> reference) { 122  return (reference == null) ? null : reference.get(); 123  } 124 }