2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.bdi;
|
|
27 |
|
|
28 |
import com.sun.jdi.ThreadGroupReference;
|
|
29 |
import com.sun.jdi.ThreadReference;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.Stack;
|
|
32 |
import java.util.ArrayList;
|
|
33 |
import java.util.Iterator;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Descend the tree of thread groups.
|
|
37 |
* @author Robert G. Field
|
|
38 |
*/
|
51
|
39 |
public class ThreadGroupIterator implements Iterator<ThreadGroupReference> {
|
2
|
40 |
private final Stack<Iterator<ThreadGroupReference>> stack
|
|
41 |
= new Stack<Iterator<ThreadGroupReference>>();
|
|
42 |
|
|
43 |
public ThreadGroupIterator(List<ThreadGroupReference> tgl) {
|
|
44 |
push(tgl);
|
|
45 |
}
|
|
46 |
|
|
47 |
public ThreadGroupIterator(ThreadGroupReference tg) {
|
|
48 |
List<ThreadGroupReference> tgl = new ArrayList<ThreadGroupReference>();
|
|
49 |
tgl.add(tg);
|
|
50 |
push(tgl);
|
|
51 |
}
|
|
52 |
|
|
53 |
/*
|
|
54 |
ThreadGroupIterator() {
|
|
55 |
this(Env.vm().topLevelThreadGroups());
|
|
56 |
}
|
|
57 |
*/
|
|
58 |
|
51
|
59 |
private Iterator<ThreadGroupReference> top() {
|
|
60 |
return stack.peek();
|
2
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* The invariant in this class is that the top iterator
|
|
65 |
* on the stack has more elements. If the stack is
|
|
66 |
* empty, there is no top. This method assures
|
|
67 |
* this invariant.
|
|
68 |
*/
|
|
69 |
private void push(List<ThreadGroupReference> tgl) {
|
|
70 |
stack.push(tgl.iterator());
|
|
71 |
while (!stack.isEmpty() && !top().hasNext()) {
|
|
72 |
stack.pop();
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
public boolean hasNext() {
|
|
77 |
return !stack.isEmpty();
|
|
78 |
}
|
|
79 |
|
51
|
80 |
public ThreadGroupReference next() {
|
2
|
81 |
return nextThreadGroup();
|
|
82 |
}
|
|
83 |
|
|
84 |
public ThreadGroupReference nextThreadGroup() {
|
51
|
85 |
ThreadGroupReference tg = top().next();
|
2
|
86 |
push(tg.threadGroups());
|
|
87 |
return tg;
|
|
88 |
}
|
|
89 |
|
|
90 |
public void remove() {
|
|
91 |
throw new UnsupportedOperationException();
|
|
92 |
}
|
|
93 |
|
|
94 |
/*
|
|
95 |
static ThreadGroupReference find(String name) {
|
|
96 |
ThreadGroupIterator tgi = new ThreadGroupIterator();
|
|
97 |
while (tgi.hasNext()) {
|
|
98 |
ThreadGroupReference tg = tgi.nextThreadGroup();
|
|
99 |
if (tg.name().equals(name)) {
|
|
100 |
return tg;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
return null;
|
|
104 |
}
|
|
105 |
*/
|
|
106 |
}
|