KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > EventTypeSet


1 package org.jacorb.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 import org.omg.CosNotification.EventType;
30
31 import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock;
32 import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
33
34 /**
35  * @author Alphonse Bendt
36  * @version $Id: EventTypeSet.java,v 1.9 2005/04/13 20:32:52 alphonse.bendt Exp $
37  */

38
39 public abstract class EventTypeSet
40 {
41     private final static EventTypeWrapper[] EVENT_TYPE_WRAPPER_TEMPLATE = new EventTypeWrapper[0];
42
43     private Set JavaDoc eventTypeSet_ = new TreeSet JavaDoc();
44
45     private final ReadWriteLock readWriteLock_ = new FIFOReadWriteLock();
46
47     private final Object JavaDoc arrayViewLock_ = new Object JavaDoc();
48
49     private EventType[] arrayView_ = null;
50
51     private boolean eventTypeSetModified_ = true;
52
53     protected static final EventType[] EMPTY_EVENT_TYPE = new EventType[0];
54     
55     ////////////////////////////////////////
56

57     public void changeSet(EventType[] added, EventType[] removed) throws InterruptedException JavaDoc
58     {
59         final List JavaDoc _addedList = new ArrayList JavaDoc();
60
61         final List JavaDoc _removedList = new ArrayList JavaDoc();
62
63         boolean _modified = false;
64
65         try
66         {
67             readWriteLock_.writeLock().acquire();
68
69             Set JavaDoc _modifiedSet = new TreeSet JavaDoc(eventTypeSet_);
70
71             for (int x = 0; x < removed.length; ++x)
72             {
73                 EventTypeWrapper event = new EventTypeWrapper(removed[x]);
74                 _modifiedSet.remove(event);
75             }
76
77             for (int x = 0; x < added.length; ++x)
78             {
79                 EventTypeWrapper event = new EventTypeWrapper(added[x]);
80                 _modifiedSet.add(event);
81             }
82
83             Iterator JavaDoc _i = _modifiedSet.iterator();
84
85             while (_i.hasNext())
86             {
87                 Object JavaDoc _eventType = _i.next();
88
89                 if (!eventTypeSet_.contains(_eventType))
90                 {
91                     _addedList.add(_eventType);
92                     _modified = true;
93                 }
94             }
95
96             _i = eventTypeSet_.iterator();
97
98             while (_i.hasNext())
99             {
100                 Object JavaDoc _eventType = _i.next();
101
102                 if (!_modifiedSet.contains(_eventType))
103                 {
104                     _removedList.add(_eventType);
105                     _modified = true;
106                 }
107             }
108
109             if (_modified)
110             {
111                 synchronized (arrayViewLock_)
112                 {
113                     eventTypeSet_ = _modifiedSet;
114
115                     eventTypeSetModified_ = true;
116                 }
117             }
118         } finally
119         {
120             readWriteLock_.writeLock().release();
121         }
122
123         if (_modified)
124         {
125             fireSetChanged(_addedList, _removedList);
126         }
127     }
128
129     private void fireSetChanged(List JavaDoc added, List JavaDoc removed)
130     {
131         EventType[] _addedArray = new EventType[added.size()];
132
133         for (int x = 0; x < _addedArray.length; ++x)
134         {
135             _addedArray[x] = ((EventTypeWrapper) added.get(x)).getEventType();
136         }
137
138         EventType[] _removedArray = new EventType[removed.size()];
139
140         for (int x = 0; x < _removedArray.length; ++x)
141         {
142             _removedArray[x] = ((EventTypeWrapper) removed.get(x)).getEventType();
143         }
144
145         actionSetChanged(_addedArray, _removedArray);
146     }
147
148     protected abstract void actionSetChanged(EventType[] added, EventType[] removed);
149
150     protected EventType[] getAllTypes() throws InterruptedException JavaDoc
151     {
152         try
153         {
154             readWriteLock_.readLock().acquire();
155
156             updateArrayView();
157
158             return arrayView_;
159
160         } finally
161         {
162             readWriteLock_.readLock().release();
163         }
164     }
165
166     private void updateArrayView()
167     {
168         synchronized (arrayViewLock_)
169         {
170             if (eventTypeSetModified_)
171             {
172                 EventTypeWrapper[] _allWrapped = (EventTypeWrapper[]) eventTypeSet_
173                         .toArray(EVENT_TYPE_WRAPPER_TEMPLATE);
174
175                 EventType[] _all = new EventType[_allWrapped.length];
176
177                 for (int x = 0; x < _allWrapped.length; ++x)
178                 {
179                     _all[x] = _allWrapped[x].getEventType();
180                 }
181
182                 eventTypeSetModified_ = false;
183
184                 arrayView_ = _all;
185             }
186         }
187     }
188 }
Popular Tags