KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > event > EventSetImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal.event;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.jdi.internal.MirrorImpl;
22 import org.eclipse.jdi.internal.VirtualMachineImpl;
23 import org.eclipse.jdi.internal.request.EventRequestImpl;
24
25 import com.sun.jdi.InternalException;
26 import com.sun.jdi.ThreadReference;
27 import com.sun.jdi.event.EventIterator;
28 import com.sun.jdi.event.EventSet;
29 import com.sun.jdi.request.EventRequest;
30
31 /**
32  * this class implements the corresponding interfaces
33  * declared by the JDI specification. See the com.sun.jdi package
34  * for more information.
35  *
36  */

37 public class EventSetImpl extends MirrorImpl implements EventSet {
38     /** Set that is used to store events. */
39     private List JavaDoc fEvents;
40     /** Which threads were suspended by this composite event. */
41     private byte fSuspendPolicy;
42
43     /**
44      * Creates new EventSetImpl.
45      */

46     private EventSetImpl(VirtualMachineImpl vmImpl) {
47         super("EventSet", vmImpl); //$NON-NLS-1$
48
}
49
50     /**
51      * Creates new EventSetImpl with events in a given array.
52      */

53     public EventSetImpl(VirtualMachineImpl vmImpl, EventImpl[] events) {
54         this(vmImpl);
55         fEvents = new ArrayList JavaDoc(events.length);
56         for (int i = 0; i < events.length; i++)
57             fEvents.add(events[i]);
58     }
59
60     /**
61      * Creates new EventSetImpl with given event.
62      */

63     public EventSetImpl(VirtualMachineImpl vmImpl, EventImpl event) {
64         this(vmImpl);
65         fEvents = new ArrayList JavaDoc(1);
66         fEvents.add(event);
67     }
68
69     /**
70      * @return Returns iterator over events.
71      */

72     public EventIterator eventIterator() {
73         return new EventIteratorImpl(fEvents.listIterator());
74     }
75
76     /**
77      * @return Returns which threads were suspended by this composite event.
78      */

79     public int suspendPolicy() {
80         switch(fSuspendPolicy) {
81             case EventRequestImpl.SUSPENDPOL_NONE_JDWP:
82                 return EventRequest.SUSPEND_NONE;
83             case EventRequestImpl.SUSPENDPOL_EVENT_THREAD_JDWP:
84                 return EventRequest.SUSPEND_EVENT_THREAD;
85             case EventRequestImpl.SUSPENDPOL_ALL_JDWP:
86                 return EventRequest.SUSPEND_ALL;
87             default:
88                 throw new InternalException(EventMessages.EventSetImpl_Invalid_suspend_policy_encountered___1 + fSuspendPolicy);
89         }
90     }
91     
92     /**
93      * Resumes threads that were suspended by this event set.
94      */

95     public void resume() {
96         switch(fSuspendPolicy) {
97             case EventRequestImpl.SUSPENDPOL_NONE_JDWP:
98                 break;
99             case EventRequestImpl.SUSPENDPOL_EVENT_THREAD_JDWP:
100                 resumeThreads();
101                 break;
102             case EventRequestImpl.SUSPENDPOL_ALL_JDWP:
103                 virtualMachineImpl().resume();
104                 break;
105             default:
106                 throw new InternalException(EventMessages.EventSetImpl_Invalid_suspend_policy_encountered___1 + fSuspendPolicy);
107         }
108     }
109     
110     /**
111      * Resumes threads that were suspended by this event set.
112      */

113     private void resumeThreads() {
114         if (fEvents.size() == 1) {
115             // Most event sets have only one event.
116
// Avoid expensive object creation.
117
ThreadReference ref= ((EventImpl)fEvents.get(0)).thread();
118             if (ref != null) {
119                 ref.resume();
120             } else {
121                 ((EventImpl)fEvents.get(0)).virtualMachine().resume();
122             }
123             return;
124         }
125         Iterator JavaDoc iter = fEvents.iterator();
126         List JavaDoc resumedThreads= new ArrayList JavaDoc(fEvents.size());
127         while (iter.hasNext()) {
128             EventImpl event = (EventImpl)iter.next();
129             ThreadReference thread= event.thread();
130             if (thread == null) {
131                 event.virtualMachine().resume();
132                 return;
133             }
134             if (!resumedThreads.contains(thread)) {
135                 resumedThreads.add(thread);
136             }
137         }
138         Iterator JavaDoc resumeIter= resumedThreads.iterator();
139         while (resumeIter.hasNext()) {
140             ((ThreadReference)resumeIter.next()).resume();
141         }
142     }
143     
144     /**
145      * @return Returns EventSetImpl that was read from InputStream.
146      */

147     public static EventSetImpl read(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
148         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
149         EventSetImpl eventSet = new EventSetImpl(vmImpl);
150         
151         // Read suspend policy.
152
eventSet.fSuspendPolicy = target.readByte("suspendPolicy", EventRequestImpl.suspendPolicyMap(), in); //$NON-NLS-1$
153
// Read size.
154
int size = target.readInt("size", in); //$NON-NLS-1$
155
// Create event list.
156
eventSet.fEvents = new ArrayList JavaDoc(size);
157         
158         while (size-- > 0) {
159             EventImpl event = EventImpl.read(target, in);
160             
161             // If event == null than it is an event that must not be given to the application.
162
// See ClassPrepareEvent.
163
if (event == null)
164                 continue;
165
166             EventRequestImpl request = (EventRequestImpl)event.request();
167             
168             // Check if the request corresponding to the event was not generated from inside this JDI implementation.
169
if (request == null || !request.isGeneratedInside())
170                 eventSet.fEvents.add(event);
171
172         }
173         return eventSet;
174     }
175
176     /**
177      * @see java.util.Collection
178      */

179     public boolean contains(Object JavaDoc event) {
180         return fEvents.contains(event);
181     }
182     
183     /**
184      * @see java.util.Collection
185      */

186     public boolean containsAll(Collection JavaDoc events) {
187         return fEvents.containsAll(events);
188     }
189     
190     /**
191      * @see java.util.Collection
192      */

193     public boolean equals(Object JavaDoc object) {
194         return object != null && object.getClass().equals(this.getClass()) && fEvents.equals(((EventSetImpl)object).fEvents);
195     }
196     
197     /**
198      * @see java.util.Collection
199      */

200     public int hashCode() {
201         return fEvents.hashCode();
202     }
203     
204     /**
205      * @see java.util.Collection
206      */

207     public boolean isEmpty() {
208         return fEvents.isEmpty();
209     }
210     
211     /**
212      * @see java.util.Collection#iterator()
213      */

214     public Iterator JavaDoc iterator() {
215         return fEvents.iterator();
216     }
217     
218     /**
219      * @see java.util.Collection#size()
220      */

221     public int size() {
222         return fEvents.size();
223     }
224
225     /**
226      * @see java.util.Collection#toArray()
227      */

228     public Object JavaDoc[] toArray() {
229         return fEvents.toArray();
230     }
231
232     /**
233      * @see java.util.Collection#toArray(Object[])
234      */

235     public Object JavaDoc[] toArray(Object JavaDoc[] events) {
236         return fEvents.toArray(events);
237     }
238
239     /**
240      * @see java.util.Collection#add(Object).
241      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
242      */

243     public boolean add(Object JavaDoc arg1) {
244         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
245     }
246
247     /**
248      * @see java.util.Collection#addAll(Collection)
249      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
250      */

251     public boolean addAll(Collection JavaDoc arg1) {
252         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
253     }
254
255     /**
256      * @see java.util.Collection#clear()
257      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
258      */

259     public void clear() {
260         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
261     }
262
263     /**
264      * @see java.util.Collection#remove(Object)
265      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
266      */

267     public boolean remove(Object JavaDoc arg1) {
268         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
269     }
270
271     /**
272      * @see java.util.Collection#removeAll(Collection)
273      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
274      */

275     public boolean removeAll(Collection JavaDoc arg1) {
276         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
277     }
278
279     /**
280      * @see java.util.Collection#retainAll(Collection)
281      * @exception UnsupportedOperationException always thrown since EventSets are unmodifiable.
282      */

283     public boolean retainAll(Collection JavaDoc arg1) {
284         throw new UnsupportedOperationException JavaDoc(EventMessages.EventSetImpl_EventSets_are_unmodifiable_3);
285     }
286 }
287
Popular Tags