blob: c62a882200376cdc490c7f90f3d7bead662494b4 [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.tty;
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 */
39class ThreadGroupIterator implements Iterator {
40 private final Stack<Iterator<ThreadGroupReference>> stack = new Stack<Iterator<ThreadGroupReference>>();
41
42 ThreadGroupIterator(List<ThreadGroupReference> tgl) {
43 push(tgl);
44 }
45
46 ThreadGroupIterator(ThreadGroupReference tg) {
47 List<ThreadGroupReference> tgl = new ArrayList<ThreadGroupReference>();
48 tgl.add(tg);
49 push(tgl);
50 }
51
52 ThreadGroupIterator() {
53 this(Env.vm().topLevelThreadGroups());
54 }
55
56 private Iterator top() {
57 return (Iterator)stack.peek();
58 }
59
60 /**
61 * The invariant in this class is that the top iterator
62 * on the stack has more elements. If the stack is
63 * empty, there is no top. This method assures
64 * this invariant.
65 */
66 private void push(List<ThreadGroupReference> tgl) {
67 stack.push(tgl.iterator());
68 while (!stack.isEmpty() && !top().hasNext()) {
69 stack.pop();
70 }
71 }
72
73 public boolean hasNext() {
74 return !stack.isEmpty();
75 }
76
77 public Object next() {
78 return nextThreadGroup();
79 }
80
81 public ThreadGroupReference nextThreadGroup() {
82 ThreadGroupReference tg = (ThreadGroupReference)top().next();
83 push(tg.threadGroups());
84 return tg;
85 }
86
87 public void remove() {
88 throw new UnsupportedOperationException();
89 }
90
91 static ThreadGroupReference find(String name) {
92 ThreadGroupIterator tgi = new ThreadGroupIterator();
93 while (tgi.hasNext()) {
94 ThreadGroupReference tg = tgi.nextThreadGroup();
95 if (tg.name().equals(name)) {
96 return tg;
97 }
98 }
99 return null;
100 }
101
102}