package com.google.common.graph;
import java.util.AbstractSet;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
abstract class IncidentEdgeSet<N> extends AbstractSet<EndpointPair<N>> {
protected final N node;
protected final BaseGraph<N> graph;
IncidentEdgeSet(BaseGraph<N> graph, N node) {
this.graph = graph;
this.node = node;
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
if (graph.isDirected()) {
return graph.inDegree(node)
+ graph.outDegree(node)
- (graph.successors(node).contains(node) ? 1 : 0);
} else {
return graph.adjacentNodes(node).size();
}
}
@Override
public boolean contains(@Nullable Object obj) {
if (!(obj instanceof EndpointPair)) {
return false;
}
EndpointPair<?> endpointPair = (EndpointPair<?>) obj;
if (graph.isDirected()) {
if (!endpointPair.isOrdered()) {
return false;
}
Object source = endpointPair.source();
Object target = endpointPair.target();
return (node.equals(source) && graph.successors(node).contains(target))
|| (node.equals(target) && graph.predecessors(node).contains(source));
} else {
if (endpointPair.isOrdered()) {
return false;
}
Set<N> adjacent = graph.adjacentNodes(node);
Object nodeU = endpointPair.nodeU();
Object nodeV = endpointPair.nodeV();
return (node.equals(nodeV) && adjacent.contains(nodeU))
|| (node.equals(nodeU) && adjacent.contains(nodeV));
}
}
}