KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > graph > SearchTreeBuilder


1 /*
2  * Generic graph library
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.graph;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26 /**
27  * A search tree callback implementation that builds a
28  * list of SearchTrees recording a graph search.
29  *
30  * @see SearchTreeCallback
31  * @author David Hovemeyer
32  */

33 public class SearchTreeBuilder <VertexType extends GraphVertex<VertexType>>
34     implements SearchTreeCallback<VertexType> {
35
36     private HashMap JavaDoc<VertexType, SearchTree<VertexType>> searchTreeMap =
37         new HashMap JavaDoc<VertexType, SearchTree<VertexType>>();
38
39     private LinkedList JavaDoc<SearchTree<VertexType>> searchTreeList =
40         new LinkedList JavaDoc<SearchTree<VertexType>>();
41
42     public void startSearchTree(VertexType vertex) {
43         searchTreeList.add(createSearchTree(vertex));
44     }
45
46     public void addToSearchTree(VertexType parent, VertexType child) {
47         SearchTree<VertexType> parentTree = searchTreeMap.get(parent);
48         if (parentTree == null)
49             throw new IllegalStateException JavaDoc();
50         SearchTree<VertexType> childTree = createSearchTree(child);
51         parentTree.addChild(childTree);
52     }
53
54     /**
55      * Get an Iterator over the recorded SearchTrees.
56      */

57     public Iterator JavaDoc<SearchTree<VertexType>> searchTreeIterator() {
58         return searchTreeList.iterator();
59     }
60
61     private SearchTree<VertexType> createSearchTree(VertexType vertex) {
62         SearchTree<VertexType> searchTree = new SearchTree<VertexType>(vertex);
63         searchTreeMap.put(vertex, searchTree);
64         return searchTree;
65     }
66 }
67
68 // vim:ts=4
69
Popular Tags