package com.google.common.graph; import com.google.common.base.Preconditions; import com.google.common.collect.AbstractIterator; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.Iterator; import java.util.Set; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public abstract class EndpointPairIterator extends AbstractIterator> { private final BaseGraph graph; protected N node; private final Iterator nodeIterator; protected Iterator successorIterator; /* JADX INFO: Access modifiers changed from: package-private */ public static EndpointPairIterator of(BaseGraph baseGraph) { return baseGraph.isDirected() ? new Directed(baseGraph) : new Undirected(baseGraph); } private EndpointPairIterator(BaseGraph baseGraph) { this.node = null; this.successorIterator = ImmutableSet.of().iterator(); this.graph = baseGraph; this.nodeIterator = baseGraph.nodes().iterator(); } protected final boolean advance() { Preconditions.checkState(!this.successorIterator.hasNext()); if (!this.nodeIterator.hasNext()) { return false; } N next = this.nodeIterator.next(); this.node = next; this.successorIterator = this.graph.successors((BaseGraph) next).iterator(); return true; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class Directed extends EndpointPairIterator { private Directed(BaseGraph baseGraph) { super(baseGraph); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public final EndpointPair computeNext() { while (!this.successorIterator.hasNext()) { if (!advance()) { return endOfData(); } } return EndpointPair.ordered(this.node, this.successorIterator.next()); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class Undirected extends EndpointPairIterator { private Set visitedNodes; private Undirected(BaseGraph baseGraph) { super(baseGraph); this.visitedNodes = Sets.newHashSetWithExpectedSize(baseGraph.nodes().size()); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator public final EndpointPair computeNext() { while (true) { if (this.successorIterator.hasNext()) { N next = this.successorIterator.next(); if (!this.visitedNodes.contains(next)) { return EndpointPair.unordered(this.node, next); } } else { this.visitedNodes.add(this.node); if (!advance()) { this.visitedNodes = null; return endOfData(); } } } } } }