KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > ThreadCursoredList


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import java.util.*;
25 import oracle.toplink.essentials.exceptions.ValidationException;
26
27 /**
28  * Special List/Vector subclass that allows concurrent
29  * population of the contents while the list is in use.
30  * The list will allow iteration while it is still being populated
31  * to allow concurrent processing of the contents.
32  * Other API such as size that require to full contents know will wait until
33  * the list is notified as being complete.
34  * This is use to allow the rows and objects of a read-all query to be processed concurrently.
35  *
36  * @author James Sutherland
37  * @since OracleAS 10g TopLink (10.0.3)
38  */

39 public class ThreadCursoredList extends Vector {
40
41     /** Store if the list is fully populated. */
42     protected boolean isComplete;
43
44     /** Used to throw exception that occur from the concurrent population thread. */
45     protected RuntimeException JavaDoc exception;
46
47     /**
48      * Construct an empty list so that its internal data array
49      * has size <tt>10</tt> and its standard capacity increment is zero.
50      */

51     public ThreadCursoredList() {
52         this(10);
53     }
54
55     /**
56      * Construct an empty list with the specified initial capacity and
57      * with its capacity increment equal to zero.
58      */

59     public ThreadCursoredList(int initialCapacity) {
60         this(initialCapacity, 0);
61     }
62
63     /**
64      * Construct an empty list with the specified initial capacity and
65      * capacity increment.
66      */

67     public ThreadCursoredList(int initialCapacity, int capacityIncrement) {
68         super(0);
69         this.isComplete = false;
70     }
71
72     /**
73      * Add and notify any waiters that there are new elements.
74      */

75     public synchronized void add(int index, Object JavaDoc element) {
76         super.add(index, element);
77         this.notifyAll();
78     }
79
80     /**
81      * Add and notify any waiters that there are new elements.
82      */

83     public synchronized boolean add(Object JavaDoc element) {
84         boolean result = super.add(element);
85         notifyAll();
86         return result;
87     }
88
89     /**
90      * Add and notify any waiters that there are new elements.
91      */

92     public synchronized boolean addAll(int index, Collection collection) {
93         boolean result = super.addAll(index, collection);
94         notifyAll();
95         return result;
96     }
97
98     /**
99      * Add and notify any waiters that there are new elements.
100      */

101     public synchronized boolean addAll(Collection collection) {
102         boolean result = super.addAll(collection);
103         notifyAll();
104         return result;
105     }
106
107     /**
108      * Add and notify any waiters that there are new elements.
109      */

110     public synchronized void addElement(Object JavaDoc object) {
111         super.addElement(object);
112         notifyAll();
113     }
114
115     /**
116      * First wait until complete.
117      */

118     public synchronized void clear() {
119         waitUntilComplete();
120         super.clear();
121     }
122
123     /**
124      * First wait until complete.
125      */

126     public synchronized Object JavaDoc clone() {
127         waitUntilComplete();
128         return super.clone();
129     }
130
131     /**
132      * Return if any exception that was throw from concurrent population thread.
133      */

134     public boolean hasException() {
135         return getException() != null;
136     }
137
138     /**
139      * Return any exception that was throw from concurrent population thread.
140      */

141     public RuntimeException JavaDoc getException() {
142         return exception;
143     }
144
145     /**
146      * Record that the population thread hit an exception,
147      * that should be thrown to the processing thread on the next access.
148      * This also records the list and complete.
149      */

150     public synchronized void throwException(RuntimeException JavaDoc exception) {
151         this.exception = exception;
152         setIsComplete(true);
153     }
154
155     /**
156      * Return if the list is complete.
157      * If an exception was thrown during the concurrent population throw the exception.
158      */

159     public synchronized boolean isComplete() {
160         if (hasException()) {
161             // Set the exception to null so it is only thrown once.
162
RuntimeException JavaDoc thrownException = this.exception;
163             this.exception = null;
164             throw thrownException;
165         }
166         return isComplete;
167     }
168
169     /**
170      * Set the list complete and notify any waiters.
171      */

172     public synchronized void setIsComplete(boolean isComplete) {
173         this.isComplete = isComplete;
174         notifyAll();
175     }
176
177     /**
178      * Wait until the list has been fully populated.
179      */

180     public synchronized void waitUntilComplete() {
181         while (!isComplete()) {
182             try {
183                 wait();
184             } catch (InterruptedException JavaDoc ignore) {
185             }
186         }
187     }
188
189     /**
190      * Wait until a new element has been added.
191      */

192     public synchronized void waitUntilAdd() {
193         try {
194             wait();
195         } catch (InterruptedException JavaDoc ignore) {
196         }
197     }
198
199     /**
200      * If it does not contain the object must wait until it is complete.
201      */

202     public synchronized boolean contains(Object JavaDoc element) {
203         boolean result = super.contains(element);
204         if ((result != true) && (!isComplete())) {
205             waitUntilComplete();
206             result = super.contains(element);
207         }
208         return result;
209     }
210
211     /**
212      * If it does not contain the object must wait until it is complete.
213      */

214     public synchronized boolean containsAll(Collection collection) {
215         boolean result = super.containsAll(collection);
216         if ((result != true) && (!isComplete())) {
217             waitUntilComplete();
218             result = super.containsAll(collection);
219         }
220         return result;
221     }
222
223     /**
224      * First wait until complete.
225      */

226     public synchronized void copyInto(Object JavaDoc[] array) {
227         waitUntilComplete();
228         super.copyInto(array);
229     }
230
231     /**
232      * If the index is beyond the size wait until complete.
233      */

234     public synchronized Object JavaDoc elementAt(int index) {
235         Object JavaDoc result = super.elementAt(index);
236         if ((result == null) && (!isComplete())) {
237             waitUntilComplete();
238             result = super.elementAt(index);
239         }
240         return result;
241     }
242     
243     protected int getSize() {
244         return super.size();
245     }
246
247     /**
248      * Allow concurrent streaming of the elements.
249      */

250     public Enumeration elements() {
251         return new Enumeration() {
252                 int count = 0;
253
254                 public boolean hasMoreElements() {
255                     synchronized (ThreadCursoredList.this) {
256                         boolean result = count < ThreadCursoredList.this.getSize();
257                         while ((!result) && (!isComplete())) {
258                             waitUntilAdd();
259                             result = count < ThreadCursoredList.this.getSize();
260                         }
261                         return result;
262                     }
263                 }
264
265                 public Object JavaDoc nextElement() {
266                     synchronized (ThreadCursoredList.this) {
267                         boolean result = count < ThreadCursoredList.this.getSize();
268                         while ((!result) && (!isComplete())) {
269                             waitUntilAdd();
270                             result = count < ThreadCursoredList.this.getSize();
271                         }
272                         if (result) {
273                             return get(count++);
274                         }
275                     }
276                     throw new NoSuchElementException("Vector Enumeration");
277                 }
278             };
279     }
280
281     /**
282      * First wait until complete.
283      */

284     public synchronized boolean equals(Object JavaDoc object) {
285         waitUntilComplete();
286         return super.equals(object);
287     }
288
289     /**
290      * Wait until has an element or is complete.
291      */

292     public synchronized Object JavaDoc firstElement() {
293         while ((!isComplete()) && (super.size() < 1)) {
294             waitUntilAdd();
295         }
296         return super.firstElement();
297     }
298
299     /**
300      * Wait until has the element or is complete.
301      */

302     public synchronized Object JavaDoc get(int index) {
303         while ((!isComplete()) && (super.size() < index)) {
304             waitUntilAdd();
305         }
306         return super.get(index);
307     }
308
309     /**
310      * First wait until complete.
311      */

312     public synchronized int hashCode() {
313         waitUntilComplete();
314         return super.hashCode();
315     }
316
317     /**
318      * If does not contain the object wait until complete.
319      */

320     public int indexOf(Object JavaDoc element) {
321         int result = super.indexOf(element);
322         if ((result == -1) && (!isComplete())) {
323             waitUntilComplete();
324             result = super.indexOf(element);
325         }
326         return result;
327     }
328
329     /**
330      * If does not contain the object wait until complete.
331      */

332     public synchronized int indexOf(Object JavaDoc element, int index) {
333         int result = super.indexOf(element, index);
334         if ((result == -1) && (!isComplete())) {
335             waitUntilComplete();
336             result = super.indexOf(element, index);
337         }
338         return result;
339     }
340
341     /**
342      * Add the element a notify any waiters that there are new elements.
343      */

344     public synchronized void insertElementAt(Object JavaDoc element, int index) {
345         super.insertElementAt(element, index);
346         notify();
347     }
348
349     /**
350      * If empty wait until an element has been added or is complete.
351      */

352     public boolean isEmpty() {
353         boolean result = super.isEmpty();
354         if (result && (!isComplete())) {
355             waitUntilAdd();
356             result = super.isEmpty();
357         }
358         return result;
359     }
360
361     /**
362      * Not supported currently.
363      */

364     public Iterator iterator() {
365         throw ValidationException.operationNotSupported("iterator");
366     }
367
368     /**
369      * First wait until complete.
370      */

371     public synchronized Object JavaDoc lastElement() {
372         waitUntilComplete();
373         return super.lastElement();
374     }
375
376     /**
377      * First wait until complete.
378      */

379     public int lastIndexOf(Object JavaDoc element) {
380         waitUntilComplete();
381         return super.lastIndexOf(element);
382     }
383
384     /**
385      * First wait until complete.
386      */

387     public synchronized int lastIndexOf(Object JavaDoc element, int index) {
388         waitUntilComplete();
389         return super.lastIndexOf(element, index);
390     }
391
392     /**
393      * Not supported currently.
394      */

395     public ListIterator listIterator() {
396         throw ValidationException.operationNotSupported("iterator");
397     }
398
399     /**
400      * Not supported currently.
401      */

402     public ListIterator listIterator(final int index) {
403         throw ValidationException.operationNotSupported("iterator");
404     }
405
406     /**
407      * If index is missing wait until is there.
408      */

409     public synchronized Object JavaDoc remove(int index) {
410         while ((!isComplete()) && (super.size() < index)) {
411             waitUntilAdd();
412         }
413         return super.remove(index);
414     }
415
416     /**
417      * If object is missing wait until complete.
418      */

419     public boolean remove(Object JavaDoc element) {
420         boolean result = super.remove(element);
421         if ((!result) && (!isComplete())) {
422             waitUntilAdd();
423             result = super.remove(element);
424         }
425         return result;
426     }
427
428     /**
429      * First wait until complete.
430      */

431     public synchronized boolean removeAll(Collection collection) {
432         waitUntilComplete();
433         return super.removeAll(collection);
434     }
435
436     /**
437      * First wait until complete.
438      */

439     public synchronized void removeAllElements() {
440         waitUntilComplete();
441         super.removeAllElements();
442     }
443
444     /**
445      * If missing wait until complete.
446      */

447     public synchronized boolean removeElement(Object JavaDoc element) {
448         boolean result = super.removeElement(element);
449         if ((!result) && (!isComplete())) {
450             waitUntilAdd();
451             result = super.removeElement(element);
452         }
453         return result;
454     }
455
456     /**
457      * If index is missing wait until reasched or complete.
458      */

459     public synchronized void removeElementAt(int index) {
460         while ((!isComplete()) && (super.size() < index)) {
461             waitUntilAdd();
462         }
463         super.removeElementAt(index);
464     }
465
466     /**
467      * First wait until complete.
468      */

469     public synchronized boolean retainAll(Collection collection) {
470         waitUntilComplete();
471         return super.retainAll(collection);
472     }
473
474     /**
475      * If index is missing wait until reached or complete.
476      */

477     public synchronized Object JavaDoc set(int index, Object JavaDoc element) {
478         while ((!isComplete()) && (super.size() < index)) {
479             waitUntilAdd();
480         }
481         return super.set(index, element);
482     }
483
484     /**
485      * If index is missing wait until reached or complete.
486      */

487     public synchronized void setElementAt(Object JavaDoc element, int index) {
488         while ((!isComplete()) && (super.size() < index)) {
489             waitUntilAdd();
490         }
491         super.setElementAt(element, index);
492     }
493
494     /**
495      * First wait until complete.
496      */

497     public int size() {
498         waitUntilComplete();
499         return super.size();
500     }
501
502     /**
503      * If index is missing wait until reached or complete.
504      */

505     public List subList(int fromIndex, int toIndex) {
506         while ((!isComplete()) && (super.size() < toIndex)) {
507             waitUntilAdd();
508         }
509         return super.subList(fromIndex, toIndex);
510     }
511
512     /**
513      * First wait until complete.
514      */

515     public synchronized Object JavaDoc[] toArray() {
516         waitUntilComplete();
517         return super.toArray();
518     }
519
520     /**
521      * First wait until complete.
522      */

523     public synchronized Object JavaDoc[] toArray(Object JavaDoc[] array) {
524         waitUntilComplete();
525         return super.toArray(array);
526     }
527
528     /**
529      * First wait until complete.
530      */

531     public synchronized String JavaDoc toString() {
532         waitUntilComplete();
533         return super.toString();
534     }
535
536     /**
537      * First wait until complete.
538      */

539     public synchronized void trimToSize() {
540         waitUntilComplete();
541         super.trimToSize();
542     }
543 }
544
Popular Tags