1 /*
   2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.bdi;
  36 
  37 import com.sun.jdi.ThreadGroupReference;
  38 import java.util.List;
  39 import java.util.Stack;
  40 import java.util.ArrayList;
  41 import java.util.Iterator;
  42 
  43 /**
  44  * Descend the tree of thread groups.
  45  * @author Robert G. Field
  46  */
  47 public class ThreadGroupIterator implements Iterator<ThreadGroupReference> {
  48     private final Stack<Iterator<ThreadGroupReference>> stack
  49                         = new Stack<Iterator<ThreadGroupReference>>();
  50 
  51     public ThreadGroupIterator(List<ThreadGroupReference> tgl) {
  52         push(tgl);
  53     }
  54 
  55     public ThreadGroupIterator(ThreadGroupReference tg) {
  56         List<ThreadGroupReference> tgl = new ArrayList<ThreadGroupReference>();
  57         tgl.add(tg);
  58         push(tgl);
  59     }
  60 
  61 /*
  62     ThreadGroupIterator() {
  63         this(Env.vm().topLevelThreadGroups());
  64     }
  65 */
  66 
  67     private Iterator<ThreadGroupReference> top() {
  68         return stack.peek();
  69     }
  70 
  71     /**
  72      * The invariant in this class is that the top iterator
  73      * on the stack has more elements.  If the stack is
  74      * empty, there is no top.  This method assures
  75      * this invariant.
  76      */
  77     private void push(List<ThreadGroupReference> tgl) {
  78         stack.push(tgl.iterator());
  79         while (!stack.isEmpty() && !top().hasNext()) {
  80             stack.pop();
  81         }
  82     }
  83 
  84     @Override
  85     public boolean hasNext() {
  86         return !stack.isEmpty();
  87     }
  88 
  89     @Override
  90     public ThreadGroupReference next() {
  91         return nextThreadGroup();
  92     }
  93 
  94     public ThreadGroupReference nextThreadGroup() {
  95         ThreadGroupReference tg = top().next();
  96         push(tg.threadGroups());
  97         return tg;
  98     }
  99 
 100     @Override
 101     public void remove() {
 102         throw new UnsupportedOperationException();
 103     }
 104 
 105 /*
 106     static ThreadGroupReference find(String name) {
 107         ThreadGroupIterator tgi = new ThreadGroupIterator();
 108         while (tgi.hasNext()) {
 109             ThreadGroupReference tg = tgi.nextThreadGroup();
 110             if (tg.name().equals(name)) {
 111                 return tg;
 112             }
 113         }
 114         return null;
 115     }
 116 */
 117 }