org.testifyproject.google.common.graph.MultiEdgesConnecting Maven / Gradle / Ivy
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in org.testifyproject.testifyprojectpliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org.testifyproject/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.graph;
import static org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.base.Preconditions.checkNotNull;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.AbstractIterator;
import org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.UnmodifiableIterator;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
/**
* A class to represent the set of edges connecting an (implicit) origin node to a target node.
*
* The {@link #outEdgeToNode} map allows this class to work on networks with parallel edges. See
* {@link EdgesConnecting} for a class that is more efficient but forbids parallel edges.
*
* @author James Sexton
* @param Edge parameter type
*/
abstract class MultiEdgesConnecting extends AbstractSet {
private final Map outEdgeToNode;
private final Object targetNode;
MultiEdgesConnecting(Map outEdgeToNode, Object targetNode) {
this.outEdgeToNode = checkNotNull(outEdgeToNode);
this.targetNode = checkNotNull(targetNode);
}
@Override
public UnmodifiableIterator iterator() {
final Iterator extends Entry> entries = outEdgeToNode.entrySet().iterator();
return new AbstractIterator() {
@Override
protected E org.testifyproject.testifyprojectputeNext() {
while (entries.hasNext()) {
Entry entry = entries.next();
if (targetNode.equals(entry.getValue())) {
return entry.getKey();
}
}
return endOfData();
}
};
}
@Override
public boolean contains(@Nullable Object edge) {
return targetNode.equals(outEdgeToNode.get(edge));
}
}