1 /*
   2  * Copyright (c) 1999, 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.event;
  36 
  37 import com.sun.jdi.*;
  38 import com.sun.jdi.event.*;
  39 import com.sun.jdi.request.*;
  40 
  41 import java.util.*;
  42 
  43 public abstract class AbstractEventSet extends EventObject implements EventSet {
  44 
  45     private static final long serialVersionUID = 2772717574222076977L;
  46     private final EventSet jdiEventSet;
  47     final Event oneEvent;
  48 
  49     /**
  50      */
  51     AbstractEventSet(EventSet jdiEventSet) {
  52         super(jdiEventSet.virtualMachine());
  53         this.jdiEventSet = jdiEventSet;
  54         this.oneEvent = eventIterator().nextEvent();
  55     }
  56 
  57     public static AbstractEventSet toSpecificEventSet(EventSet jdiEventSet) {
  58         Event evt = jdiEventSet.eventIterator().nextEvent();
  59         if (evt instanceof LocatableEvent) {
  60             if (evt instanceof ExceptionEvent) {
  61                 return new ExceptionEventSet(jdiEventSet);
  62             } else if (evt instanceof WatchpointEvent) {
  63                 if (evt instanceof AccessWatchpointEvent) {
  64                     return new AccessWatchpointEventSet(jdiEventSet);
  65                 } else {
  66                     return new ModificationWatchpointEventSet(jdiEventSet);
  67                 }
  68             } else {
  69                 return new LocationTriggerEventSet(jdiEventSet);
  70             }
  71         } else if (evt instanceof ClassPrepareEvent) {
  72             return new ClassPrepareEventSet(jdiEventSet);
  73         } else if (evt instanceof ClassUnloadEvent) {
  74             return new ClassUnloadEventSet(jdiEventSet);
  75         } else if (evt instanceof ThreadDeathEvent) {
  76             return new ThreadDeathEventSet(jdiEventSet);
  77         } else if (evt instanceof ThreadStartEvent) {
  78             return new ThreadStartEventSet(jdiEventSet);
  79         } else if (evt instanceof VMDeathEvent) {
  80             return new VMDeathEventSet(jdiEventSet);
  81         } else if (evt instanceof VMDisconnectEvent) {
  82             return new VMDisconnectEventSet(jdiEventSet);
  83         } else if (evt instanceof VMStartEvent) {
  84             return new VMStartEventSet(jdiEventSet);
  85         } else {
  86             throw new IllegalArgumentException("Unknown event " + evt);
  87         }
  88     }
  89 
  90     public abstract void notify(JDIListener listener);
  91 
  92     // Implement Mirror
  93 
  94     @Override
  95     public VirtualMachine virtualMachine() {
  96         return jdiEventSet.virtualMachine();
  97     }
  98 
  99     public VirtualMachine getVirtualMachine() {
 100         return jdiEventSet.virtualMachine();
 101     }
 102 
 103     // Implement EventSet
 104 
 105     /**
 106      * Returns the policy used to suspend threads in the target VM
 107      * for this event set. This policy is selected from the suspend
 108      * policies for each event's request. The one that suspends the
 109      * most threads is chosen when the event occurs in the target VM
 110      * and that policy is returned here. See
 111      * com.sun.jdi.request.EventRequest for the possible policy values.
 112      *
 113      * @return the integer suspendPolicy
 114      */
 115     public int getSuspendPolicy() {
 116         return jdiEventSet.suspendPolicy();
 117     }
 118 
 119     @Override
 120     public void resume() {
 121         jdiEventSet.resume();
 122     }
 123 
 124     @Override
 125     public int suspendPolicy() {
 126         return jdiEventSet.suspendPolicy();
 127     }
 128 
 129     public boolean suspendedAll() {
 130         return jdiEventSet.suspendPolicy() == EventRequest.SUSPEND_ALL;
 131     }
 132 
 133     public boolean suspendedEventThread() {
 134         return jdiEventSet.suspendPolicy() == EventRequest.SUSPEND_EVENT_THREAD;
 135     }
 136 
 137     public boolean suspendedNone() {
 138         return jdiEventSet.suspendPolicy() == EventRequest.SUSPEND_NONE;
 139     }
 140 
 141     /**
 142      * Return an iterator specific to {@link Event} objects.
 143      */
 144     @Override
 145     public EventIterator eventIterator() {
 146         return jdiEventSet.eventIterator();
 147     }
 148 
 149 
 150     // Implement java.util.Set (by pass through)
 151 
 152     /**
 153      * Returns the number of elements in this set (its cardinality).  If this
 154      * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
 155      * <tt>Integer.MAX_VALUE</tt>.
 156      *
 157      * @return the number of elements in this set (its cardinality).
 158      */
 159     @Override
 160     public int size() {
 161         return jdiEventSet.size();
 162     }
 163 
 164     /**
 165      * Returns <tt>true</tt> if this set contains no elements.
 166      *
 167      * @return <tt>true</tt> if this set contains no elements.
 168      */
 169     @Override
 170     public boolean isEmpty() {
 171         return jdiEventSet.isEmpty();
 172     }
 173 
 174     /**
 175      * Returns <tt>true</tt> if this set contains the specified element.  More
 176      * formally, returns <tt>true</tt> if and only if this set contains an
 177      * element <code>e</code> such that <code>(o==null ? e==null :
 178      * o.equals(e))</code>.
 179      *
 180      * @return <tt>true</tt> if this set contains the specified element.
 181      */
 182     @Override
 183     public boolean contains(Object o) {
 184         return jdiEventSet.contains(o);
 185     }
 186 
 187     /**
 188      * Returns an iterator over the elements in this set.  The elements are
 189      * returned in no particular order (unless this set is an instance of some
 190      * class that provides a guarantee).
 191      *
 192      * @return an iterator over the elements in this set.
 193      */
 194     @Override
 195     public Iterator<Event> iterator() {
 196         return jdiEventSet.iterator();
 197     }
 198 
 199     /**
 200      * Returns an array containing all of the elements in this set.
 201      * Obeys the general contract of the <tt>Collection.toArray</tt> method.
 202      *
 203      * @return an array containing all of the elements in this set.
 204      */
 205     @Override
 206     public Object[] toArray() {
 207         return jdiEventSet.toArray();
 208     }
 209 
 210     /**
 211      * Returns an array containing all of the elements in this set whose
 212      * runtime type is that of the specified array.  Obeys the general
 213      * contract of the <tt>Collection.toArray(Object[])</tt> method.
 214      *
 215      * @param a the array into which the elements of this set are to
 216      *          be stored, if it is big enough {
 217         return jdiEventSet.XXX();
 218     } otherwise, a new array of the
 219      *          same runtime type is allocated for this purpose.
 220      * @return an array containing the elements of this set.
 221      * @throws    ArrayStoreException the runtime type of a is not a supertype
 222      * of the runtime type of every element in this set.
 223      */
 224     @Override
 225     public <T> T[] toArray(T a[]) {
 226         return jdiEventSet.toArray(a);
 227     }
 228 
 229     // Bulk Operations
 230 
 231     /**
 232      * Returns <tt>true</tt> if this set contains all of the elements of the
 233      * specified collection.  If the specified collection is also a set, this
 234      * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
 235      *
 236      * @param c collection to be checked for containment in this set.
 237      * @return <tt>true</tt> if this set contains all of the elements of the
 238      *         specified collection.
 239      */
 240     @Override
 241     public boolean containsAll(Collection<?> c) {
 242         return jdiEventSet.containsAll(c);
 243     }
 244 
 245 
 246     // Make the rest of Set unmodifiable
 247 
 248     @Override
 249     public boolean add(Event e){
 250         throw new UnsupportedOperationException();
 251     }
 252     @Override
 253     public boolean remove(Object o) {
 254         throw new UnsupportedOperationException();
 255     }
 256     @Override
 257     public boolean addAll(Collection<? extends Event> coll) {
 258         throw new UnsupportedOperationException();
 259     }
 260     @Override
 261     public boolean removeAll(Collection<?> coll) {
 262         throw new UnsupportedOperationException();
 263     }
 264     @Override
 265     public boolean retainAll(Collection<?> coll) {
 266         throw new UnsupportedOperationException();
 267     }
 268     @Override
 269     public void clear() {
 270         throw new UnsupportedOperationException();
 271     }
 272 }