blob: d9b375cfde8eb326dc5203a81349fc931ea22983 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
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
26package com.sun.tools.example.debug.bdi;
27
28import com.sun.jdi.ThreadGroupReference;
29import com.sun.jdi.ThreadReference;
30import java.util.List;
31import java.util.Stack;
32import java.util.ArrayList;
33import java.util.Iterator;
34
35/**
36 * Descend the tree of thread groups.
37 * @author Robert G. Field
38 */
39public class ThreadGroupIterator implements Iterator {
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
59 private Iterator top() {
60 return (Iterator)stack.peek();
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
80 public Object next() {
81 return nextThreadGroup();
82 }
83
84 public ThreadGroupReference nextThreadGroup() {
85 ThreadGroupReference tg = (ThreadGroupReference)top().next();
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}