org.jgrapht.traverse.AbstractGraphIterator Maven / Gradle / Ivy
/* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh ([email protected])
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This program and the accompanying materials are dual-licensed under
* either
*
* (a) the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation, or (at your option) any
* later version.
*
* or (per the licensee's choosing)
*
* (b) the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
/* --------------------------
* AbstractGraphIterator.java
* --------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): Christian Hammer
*
* $Id$
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
* 11-Aug-2003 : Adaptation to new event model (BN);
* 04-May-2004 : Made generic (CH)
*
*/
package org.jgrapht.traverse;
import java.util.*;
import org.jgrapht.event.*;
/**
* An empty implementation of a graph iterator to minimize the effort required
* to implement graph iterators.
*
* @author Barak Naveh
* @since Jul 19, 2003
*/
public abstract class AbstractGraphIterator
implements GraphIterator
{
private List> traversalListeners =
new ArrayList>();
private boolean crossComponentTraversal = true;
private boolean reuseEvents = false;
// We keep this cached redundantly with traversalListeners.size()
// so that subclasses can use it as a fast check to see if
// event firing calls can be skipped.
protected int nListeners = 0;
/**
* Sets the cross component traversal flag - indicates whether to traverse
* the graph across connected components.
*
* @param crossComponentTraversal if true
traverses across
* connected components.
*/
public void setCrossComponentTraversal(boolean crossComponentTraversal)
{
this.crossComponentTraversal = crossComponentTraversal;
}
/**
* Test whether this iterator is set to traverse the graph across connected
* components.
*
* @return true
if traverses across connected components,
* otherwise false
.
*/
@Override public boolean isCrossComponentTraversal()
{
return crossComponentTraversal;
}
/**
* @see GraphIterator#setReuseEvents(boolean)
*/
@Override public void setReuseEvents(boolean reuseEvents)
{
this.reuseEvents = reuseEvents;
}
/**
* @see GraphIterator#isReuseEvents()
*/
@Override public boolean isReuseEvents()
{
return reuseEvents;
}
/**
* Adds the specified traversal listener to this iterator.
*
* @param l the traversal listener to be added.
*/
@Override public void addTraversalListener(TraversalListener l)
{
if (!traversalListeners.contains(l)) {
traversalListeners.add(l);
nListeners = traversalListeners.size();
}
}
/**
* Unsupported.
*
* @throws UnsupportedOperationException
*/
@Override public void remove()
{
throw new UnsupportedOperationException();
}
/**
* Removes the specified traversal listener from this iterator.
*
* @param l the traversal listener to be removed.
*/
@Override public void removeTraversalListener(TraversalListener l)
{
traversalListeners.remove(l);
nListeners = traversalListeners.size();
}
/**
* Informs all listeners that the traversal of the current connected
* component finished.
*
* @param e the connected component finished event.
*/
protected void fireConnectedComponentFinished(
ConnectedComponentTraversalEvent e)
{
for (int i = 0; i < nListeners; i++) {
TraversalListener l = traversalListeners.get(i);
l.connectedComponentFinished(e);
}
}
/**
* Informs all listeners that a traversal of a new connected component has
* started.
*
* @param e the connected component started event.
*/
protected void fireConnectedComponentStarted(
ConnectedComponentTraversalEvent e)
{
for (int i = 0; i < nListeners; i++) {
TraversalListener l = traversalListeners.get(i);
l.connectedComponentStarted(e);
}
}
/**
* Informs all listeners that a the specified edge was visited.
*
* @param e the edge traversal event.
*/
protected void fireEdgeTraversed(EdgeTraversalEvent e)
{
for (int i = 0; i < nListeners; i++) {
TraversalListener l = traversalListeners.get(i);
l.edgeTraversed(e);
}
}
/**
* Informs all listeners that a the specified vertex was visited.
*
* @param e the vertex traversal event.
*/
protected void fireVertexTraversed(VertexTraversalEvent e)
{
for (int i = 0; i < nListeners; i++) {
TraversalListener l = traversalListeners.get(i);
l.vertexTraversed(e);
}
}
/**
* Informs all listeners that a the specified vertex was finished.
*
* @param e the vertex traversal event.
*/
protected void fireVertexFinished(VertexTraversalEvent e)
{
for (int i = 0; i < nListeners; i++) {
TraversalListener l = traversalListeners.get(i);
l.vertexFinished(e);
}
}
}
// End AbstractGraphIterator.java