KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > traverse > AbstractGraphIterator


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (barak_naveh@users.sourceforge.net)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* --------------------------
26  * AbstractGraphIterator.java
27  * --------------------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Barak Naveh
31  * Contributor(s): Christian Hammer
32  *
33  * $Id: AbstractGraphIterator.java 487 2006-07-02 00:53:17Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 24-Jul-2003 : Initial revision (BN);
38  * 11-Aug-2003 : Adaptation to new event model (BN);
39  * 04-May-2004 : Made generic (CH)
40  *
41  */

42 package org.jgrapht.traverse;
43
44 import java.util.*;
45
46 import org.jgrapht.event.*;
47
48
49 /**
50  * An empty implementation of a graph iterator to minimize the effort required
51  * to implement graph iterators.
52  *
53  * @author Barak Naveh
54  * @since Jul 19, 2003
55  */

56 public abstract class AbstractGraphIterator<V, E>
57     implements GraphIterator<V, E>
58 {
59
60     //~ Instance fields -------------------------------------------------------
61

62     private List<TraversalListener<V, E>> traversalListeners =
63         new ArrayList<TraversalListener<V, E>>();
64     private boolean crossComponentTraversal = true;
65     private boolean reuseEvents = false;
66
67     //~ Methods ---------------------------------------------------------------
68

69     /**
70      * Sets the cross component traversal flag - indicates whether to traverse
71      * the graph across connected components.
72      *
73      * @param crossComponentTraversal if <code>true</code> traverses across
74      * connected components.
75      */

76     public void setCrossComponentTraversal(boolean crossComponentTraversal)
77     {
78         this.crossComponentTraversal = crossComponentTraversal;
79     }
80
81     /**
82      * Test whether this iterator is set to traverse the graph across connected
83      * components.
84      *
85      * @return <code>true</code> if traverses across connected components,
86      * otherwise <code>false</code>.
87      */

88     public boolean isCrossComponentTraversal()
89     {
90         return crossComponentTraversal;
91     }
92
93     /**
94      * @see GraphIterator#setReuseEvents(boolean)
95      */

96     public void setReuseEvents(boolean reuseEvents)
97     {
98         this.reuseEvents = reuseEvents;
99     }
100
101     /**
102      * @see GraphIterator#isReuseEvents()
103      */

104     public boolean isReuseEvents()
105     {
106         return reuseEvents;
107     }
108
109     /**
110      * Adds the specified traversal listener to this iterator.
111      *
112      * @param l the traversal listener to be added.
113      */

114     public void addTraversalListener(TraversalListener<V, E> l)
115     {
116         if (!traversalListeners.contains(l)) {
117             traversalListeners.add(l);
118         }
119     }
120
121     /**
122      * Unsupported.
123      *
124      * @throws UnsupportedOperationException
125      */

126     public void remove()
127     {
128         throw new UnsupportedOperationException JavaDoc();
129     }
130
131     /**
132      * Removes the specified traversal listener from this iterator.
133      *
134      * @param l the traversal listener to be removed.
135      */

136     public void removeTraversalListener(TraversalListener<V, E> l)
137     {
138         traversalListeners.remove(l);
139     }
140
141     /**
142      * Informs all listeners that the traversal of the current connected
143      * component finished.
144      *
145      * @param e the connected component finished event.
146      */

147     protected void fireConnectedComponentFinished(
148         ConnectedComponentTraversalEvent e)
149     {
150         int len = traversalListeners.size();
151
152         for (int i = 0; i < len; i++) {
153             TraversalListener l = traversalListeners.get(i);
154             l.connectedComponentFinished(e);
155         }
156     }
157
158     /**
159      * Informs all listeners that a traversal of a new connected component has
160      * started.
161      *
162      * @param e the connected component started event.
163      */

164     protected void fireConnectedComponentStarted(
165         ConnectedComponentTraversalEvent e)
166     {
167         int len = traversalListeners.size();
168
169         for (int i = 0; i < len; i++) {
170             TraversalListener l = traversalListeners.get(i);
171             l.connectedComponentStarted(e);
172         }
173     }
174
175     /**
176      * Informs all listeners that a the specified edge was visited.
177      *
178      * @param e the edge traversal event.
179      */

180     protected void fireEdgeTraversed(EdgeTraversalEvent<V, E> e)
181     {
182         int len = traversalListeners.size();
183
184         for (int i = 0; i < len; i++) {
185             TraversalListener<V, E> l = traversalListeners.get(i);
186             l.edgeTraversed(e);
187         }
188     }
189
190     /**
191      * Informs all listeners that a the specified vertex was visited.
192      *
193      * @param e the vertex traversal event.
194      */

195     protected void fireVertexTraversed(VertexTraversalEvent<V> e)
196     {
197         int len = traversalListeners.size();
198
199         for (int i = 0; i < len; i++) {
200             TraversalListener<V, E> l = traversalListeners.get(i);
201             l.vertexTraversed(e);
202         }
203     }
204 }
205
Popular Tags