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

Class Class, % Method, % Line, %
AbstractUndirectedNetworkConnections 0% (0/1) 0% (0/11) 0% (0/17)


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 java.util.Objects.requireNonNull; 22  23 import java.util.Collections; 24 import java.util.Map; 25 import java.util.Set; 26 import javax.annotation.CheckForNull; 27  28 /** 29  * A base implementation of {@link NetworkConnections} for undirected networks. 30  * 31  * @author James Sexton 32  * @param <N> Node parameter type 33  * @param <E> Edge parameter type 34  */ 35 @ElementTypesAreNonnullByDefault 36 abstract class AbstractUndirectedNetworkConnections<N, E> implements NetworkConnections<N, E> { 37  /** Keys are edges incident to the origin node, values are the node at the other end. */ 38  final Map<E, N> incidentEdgeMap; 39  40  AbstractUndirectedNetworkConnections(Map<E, N> incidentEdgeMap) { 41  this.incidentEdgeMap = checkNotNull(incidentEdgeMap); 42  } 43  44  @Override 45  public Set<N> predecessors() { 46  return adjacentNodes(); 47  } 48  49  @Override 50  public Set<N> successors() { 51  return adjacentNodes(); 52  } 53  54  @Override 55  public Set<E> incidentEdges() { 56  return Collections.unmodifiableSet(incidentEdgeMap.keySet()); 57  } 58  59  @Override 60  public Set<E> inEdges() { 61  return incidentEdges(); 62  } 63  64  @Override 65  public Set<E> outEdges() { 66  return incidentEdges(); 67  } 68  69  @Override 70  public N adjacentNode(E edge) { 71  // We're relying on callers to call this method only with an edge that's in the graph. 72  return requireNonNull(incidentEdgeMap.get(edge)); 73  } 74  75  @Override 76  @CheckForNull 77  public N removeInEdge(E edge, boolean isSelfLoop) { 78  if (!isSelfLoop) { 79  return removeOutEdge(edge); 80  } 81  return null; 82  } 83  84  @Override 85  public N removeOutEdge(E edge) { 86  N previousNode = incidentEdgeMap.remove(edge); 87  // We're relying on callers to call this method only with an edge that's in the graph. 88  return requireNonNull(previousNode); 89  } 90  91  @Override 92  public void addInEdge(E edge, N node, boolean isSelfLoop) { 93  if (!isSelfLoop) { 94  addOutEdge(edge, node); 95  } 96  } 97  98  @Override 99  public void addOutEdge(E edge, N node) { 100  N previousNode = incidentEdgeMap.put(edge, node); 101  checkState(previousNode == null); 102  } 103 }