001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.internal.impl;
016:
017: import java.util.Collection;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.ListIterator;
021: import java.util.concurrent.locks.Lock;
022:
023: import net.refractions.udig.ui.UDIGDisplaySafeLock;
024:
025: import org.eclipse.emf.common.util.EList;
026:
027: /**
028: * A decorator that synchronizes on all methods of the list.
029: *
030: * When iterating make sure to use:
031: * list.lock();
032: * try{
033: * do iterations
034: * }finally{
035: * list.unlock();
036: * }
037: *
038: * @author Jesse
039: * @since 1.1.0
040: */
041: public class SynchronizedEList implements EList {
042: private EList wrapped;
043: Lock lock = new UDIGDisplaySafeLock();
044:
045: /**
046: * Lock this list.
047: */
048: public void lock() {
049: lock.lock();
050: }
051:
052: public void unlock() {
053: lock.unlock();
054: }
055:
056: public SynchronizedEList(EList list) {
057: wrapped = list;
058: }
059:
060: @SuppressWarnings("unchecked")
061: public void add(int arg0, Object arg1) {
062: lock.lock();
063: try {
064: wrapped.add(arg0, arg1);
065: } finally {
066: lock.unlock();
067: }
068: }
069:
070: @SuppressWarnings("unchecked")
071: public boolean add(Object arg0) {
072: lock.lock();
073: try {
074: return wrapped.add(arg0);
075: } finally {
076: lock.unlock();
077: }
078: }
079:
080: @SuppressWarnings("unchecked")
081: public boolean addAll(Collection arg0) {
082: lock.lock();
083: try {
084: return wrapped.addAll(arg0);
085: } finally {
086: lock.unlock();
087: }
088: }
089:
090: @SuppressWarnings("unchecked")
091: public boolean addAll(int arg0, Collection arg1) {
092: lock.lock();
093: try {
094: return wrapped.addAll(arg0, arg1);
095: } finally {
096: lock.unlock();
097: }
098: }
099:
100: public void clear() {
101: lock.lock();
102: try {
103: wrapped.clear();
104: } finally {
105: lock.unlock();
106: }
107: }
108:
109: public boolean contains(Object arg0) {
110: lock.lock();
111: try {
112: return wrapped.contains(arg0);
113: } finally {
114: lock.unlock();
115: }
116: }
117:
118: @SuppressWarnings("unchecked")
119: public boolean containsAll(Collection arg0) {
120: lock.lock();
121: try {
122: return wrapped.containsAll(arg0);
123: } finally {
124: lock.unlock();
125: }
126: }
127:
128: public boolean equals(Object arg0) {
129: lock.lock();
130: try {
131: return wrapped.equals(arg0);
132: } finally {
133: lock.unlock();
134: }
135: }
136:
137: public Object get(int arg0) {
138: lock.lock();
139: try {
140: return wrapped.get(arg0);
141: } finally {
142: lock.unlock();
143: }
144: }
145:
146: public int hashCode() {
147: lock.lock();
148: try {
149: return wrapped.hashCode();
150: } finally {
151: lock.unlock();
152: }
153: }
154:
155: public int indexOf(Object arg0) {
156: lock.lock();
157: try {
158: return wrapped.indexOf(arg0);
159: } finally {
160: lock.unlock();
161: }
162: }
163:
164: public boolean isEmpty() {
165: lock.lock();
166: try {
167: return wrapped.isEmpty();
168: } finally {
169: lock.unlock();
170: }
171: }
172:
173: public Iterator iterator() {
174: lock.lock();
175: try {
176: return listIterator();
177: } finally {
178: lock.unlock();
179: }
180: }
181:
182: public int lastIndexOf(Object arg0) {
183: lock.lock();
184: try {
185: return wrapped.lastIndexOf(arg0);
186: } finally {
187: lock.unlock();
188: }
189: }
190:
191: public ListIterator listIterator() {
192: lock.lock();
193: try {
194: return listIterator(0);
195: } finally {
196: lock.unlock();
197: }
198: }
199:
200: @SuppressWarnings("unchecked")
201: public ListIterator listIterator(int arg0) {
202: lock.lock();
203: try {
204: return new ListIterator<Object>() {
205: ListIterator<Object> iter = wrapped.listIterator();
206:
207: public boolean hasNext() {
208: return iter.hasNext();
209: }
210:
211: public Object next() {
212: return iter.next();
213: }
214:
215: public void remove() {
216: iter.remove();
217: }
218:
219: public void add(Object o) {
220: iter.add(o);
221: }
222:
223: public boolean hasPrevious() {
224: return iter.hasPrevious();
225: }
226:
227: public int nextIndex() {
228: return iter.nextIndex();
229: }
230:
231: public Object previous() {
232: return iter.previous();
233: }
234:
235: public int previousIndex() {
236: return iter.previousIndex();
237: }
238:
239: public void set(Object o) {
240: iter.set(o);
241: }
242:
243: };
244: } finally {
245: lock.unlock();
246: }
247: }
248:
249: public Object move(int arg0, int arg1) {
250: lock.lock();
251: try {
252: return wrapped.move(arg0, arg1);
253: } finally {
254: lock.unlock();
255: }
256: }
257:
258: public void move(int arg0, Object arg1) {
259: lock.lock();
260: try {
261: wrapped.move(arg0, arg1);
262: } finally {
263: lock.unlock();
264: }
265: }
266:
267: public Object remove(int arg0) {
268: lock.lock();
269: try {
270: return wrapped.remove(arg0);
271: } finally {
272: lock.unlock();
273: }
274: }
275:
276: public boolean remove(Object arg0) {
277: lock.lock();
278: try {
279: return wrapped.remove(arg0);
280: } finally {
281: lock.unlock();
282: }
283: }
284:
285: @SuppressWarnings("unchecked")
286: public boolean removeAll(Collection arg0) {
287: lock.lock();
288: try {
289: return wrapped.removeAll(arg0);
290: } finally {
291: lock.unlock();
292: }
293: }
294:
295: @SuppressWarnings("unchecked")
296: public boolean retainAll(Collection arg0) {
297: lock.lock();
298: try {
299: return wrapped.retainAll(arg0);
300: } finally {
301: lock.unlock();
302: }
303: }
304:
305: @SuppressWarnings("unchecked")
306: public Object set(int arg0, Object arg1) {
307: lock.lock();
308: try {
309: return wrapped.set(arg0, arg1);
310: } finally {
311: lock.unlock();
312: }
313: }
314:
315: public int size() {
316: lock.lock();
317: try {
318: return wrapped.size();
319: } finally {
320: lock.unlock();
321: }
322:
323: }
324:
325: public List subList(int arg0, int arg1) {
326: lock.lock();
327: try {
328: return wrapped.subList(arg0, arg1);
329: } finally {
330: lock.unlock();
331: }
332:
333: }
334:
335: public Object[] toArray() {
336: lock.lock();
337: try {
338: return wrapped.toArray();
339: } finally {
340: lock.unlock();
341: }
342:
343: }
344:
345: @SuppressWarnings("unchecked")
346: public Object[] toArray(Object[] arg0) {
347: lock.lock();
348: try {
349: return wrapped.toArray(arg0);
350: } finally {
351: lock.unlock();
352: }
353: }
354: }
|