J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 26 | package com.sun.tools.example.debug.tty; |
| 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 | */ |
| 39 | class 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 | } |