FileDocCategorySizeDatePackage
ThreadCursoredList.javaAPI DocGlassfish v2 API16055Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.helper

ThreadCursoredList

public class ThreadCursoredList extends Vector
Special List/Vector subclass that allows concurrent population of the contents while the list is in use. The list will allow iteration while it is still being populated to allow concurrent processing of the contents. Other API such as size that require to full contents know will wait until the list is notified as being complete. This is use to allow the rows and objects of a read-all query to be processed concurrently.
author
James Sutherland
since
OracleAS 10g TopLink (10.0.3)

Fields Summary
protected boolean
isComplete
Store if the list is fully populated.
protected RuntimeException
exception
Used to throw exception that occur from the concurrent population thread.
Constructors Summary
public ThreadCursoredList()
Construct an empty list so that its internal data array has size 10 and its standard capacity increment is zero.

        this(10);
    
public ThreadCursoredList(int initialCapacity)
Construct an empty list with the specified initial capacity and with its capacity increment equal to zero.

        this(initialCapacity, 0);
    
public ThreadCursoredList(int initialCapacity, int capacityIncrement)
Construct an empty list with the specified initial capacity and capacity increment.

        super(0);
        this.isComplete = false;
    
Methods Summary
public synchronized voidadd(int index, java.lang.Object element)
Add and notify any waiters that there are new elements.

        super.add(index, element);
        this.notifyAll();
    
public synchronized booleanadd(java.lang.Object element)
Add and notify any waiters that there are new elements.

        boolean result = super.add(element);
        notifyAll();
        return result;
    
public synchronized booleanaddAll(int index, java.util.Collection collection)
Add and notify any waiters that there are new elements.

        boolean result = super.addAll(index, collection);
        notifyAll();
        return result;
    
public synchronized booleanaddAll(java.util.Collection collection)
Add and notify any waiters that there are new elements.

        boolean result = super.addAll(collection);
        notifyAll();
        return result;
    
public synchronized voidaddElement(java.lang.Object object)
Add and notify any waiters that there are new elements.

        super.addElement(object);
        notifyAll();
    
public synchronized voidclear()
First wait until complete.

        waitUntilComplete();
        super.clear();
    
public synchronized java.lang.Objectclone()
First wait until complete.

        waitUntilComplete();
        return super.clone();
    
public synchronized booleancontains(java.lang.Object element)
If it does not contain the object must wait until it is complete.

        boolean result = super.contains(element);
        if ((result != true) && (!isComplete())) {
            waitUntilComplete();
            result = super.contains(element);
        }
        return result;
    
public synchronized booleancontainsAll(java.util.Collection collection)
If it does not contain the object must wait until it is complete.

        boolean result = super.containsAll(collection);
        if ((result != true) && (!isComplete())) {
            waitUntilComplete();
            result = super.containsAll(collection);
        }
        return result;
    
public synchronized voidcopyInto(java.lang.Object[] array)
First wait until complete.

        waitUntilComplete();
        super.copyInto(array);
    
public synchronized java.lang.ObjectelementAt(int index)
If the index is beyond the size wait until complete.

        Object result = super.elementAt(index);
        if ((result == null) && (!isComplete())) {
            waitUntilComplete();
            result = super.elementAt(index);
        }
        return result;
    
public java.util.Enumerationelements()
Allow concurrent streaming of the elements.

        return new Enumeration() {
                int count = 0;

                public boolean hasMoreElements() {
                    synchronized (ThreadCursoredList.this) {
                        boolean result = count < ThreadCursoredList.this.getSize();
                        while ((!result) && (!isComplete())) {
                            waitUntilAdd();
                            result = count < ThreadCursoredList.this.getSize();
                        }
                        return result;
                    }
                }

                public Object nextElement() {
                    synchronized (ThreadCursoredList.this) {
                        boolean result = count < ThreadCursoredList.this.getSize();
                        while ((!result) && (!isComplete())) {
                            waitUntilAdd();
                            result = count < ThreadCursoredList.this.getSize();
                        }
                        if (result) {
                            return get(count++);
                        }
                    }
                    throw new NoSuchElementException("Vector Enumeration");
                }
            };
    
public synchronized booleanequals(java.lang.Object object)
First wait until complete.

        waitUntilComplete();
        return super.equals(object);
    
public synchronized java.lang.ObjectfirstElement()
Wait until has an element or is complete.

        while ((!isComplete()) && (super.size() < 1)) {
            waitUntilAdd();
        }
        return super.firstElement();
    
public synchronized java.lang.Objectget(int index)
Wait until has the element or is complete.

        while ((!isComplete()) && (super.size() < index)) {
            waitUntilAdd();
        }
        return super.get(index);
    
public java.lang.RuntimeExceptiongetException()
Return any exception that was throw from concurrent population thread.

        return exception;
    
protected intgetSize()

        return super.size();
    
public booleanhasException()
Return if any exception that was throw from concurrent population thread.

        return getException() != null;
    
public synchronized inthashCode()
First wait until complete.

        waitUntilComplete();
        return super.hashCode();
    
public intindexOf(java.lang.Object element)
If does not contain the object wait until complete.

        int result = super.indexOf(element);
        if ((result == -1) && (!isComplete())) {
            waitUntilComplete();
            result = super.indexOf(element);
        }
        return result;
    
public synchronized intindexOf(java.lang.Object element, int index)
If does not contain the object wait until complete.

        int result = super.indexOf(element, index);
        if ((result == -1) && (!isComplete())) {
            waitUntilComplete();
            result = super.indexOf(element, index);
        }
        return result;
    
public synchronized voidinsertElementAt(java.lang.Object element, int index)
Add the element a notify any waiters that there are new elements.

        super.insertElementAt(element, index);
        notify();
    
public synchronized booleanisComplete()
Return if the list is complete. If an exception was thrown during the concurrent population throw the exception.

        if (hasException()) {
            // Set the exception to null so it is only thrown once.
            RuntimeException thrownException = this.exception;
            this.exception = null;
            throw thrownException;
        }
        return isComplete;
    
public booleanisEmpty()
If empty wait until an element has been added or is complete.

        boolean result = super.isEmpty();
        if (result && (!isComplete())) {
            waitUntilAdd();
            result = super.isEmpty();
        }
        return result;
    
public java.util.Iteratoriterator()
Not supported currently.

        throw ValidationException.operationNotSupported("iterator");
    
public synchronized java.lang.ObjectlastElement()
First wait until complete.

        waitUntilComplete();
        return super.lastElement();
    
public intlastIndexOf(java.lang.Object element)
First wait until complete.

        waitUntilComplete();
        return super.lastIndexOf(element);
    
public synchronized intlastIndexOf(java.lang.Object element, int index)
First wait until complete.

        waitUntilComplete();
        return super.lastIndexOf(element, index);
    
public java.util.ListIteratorlistIterator()
Not supported currently.

        throw ValidationException.operationNotSupported("iterator");
    
public java.util.ListIteratorlistIterator(int index)
Not supported currently.

        throw ValidationException.operationNotSupported("iterator");
    
public synchronized java.lang.Objectremove(int index)
If index is missing wait until is there.

        while ((!isComplete()) && (super.size() < index)) {
            waitUntilAdd();
        }
        return super.remove(index);
    
public booleanremove(java.lang.Object element)
If object is missing wait until complete.

        boolean result = super.remove(element);
        if ((!result) && (!isComplete())) {
            waitUntilAdd();
            result = super.remove(element);
        }
        return result;
    
public synchronized booleanremoveAll(java.util.Collection collection)
First wait until complete.

        waitUntilComplete();
        return super.removeAll(collection);
    
public synchronized voidremoveAllElements()
First wait until complete.

        waitUntilComplete();
        super.removeAllElements();
    
public synchronized booleanremoveElement(java.lang.Object element)
If missing wait until complete.

        boolean result = super.removeElement(element);
        if ((!result) && (!isComplete())) {
            waitUntilAdd();
            result = super.removeElement(element);
        }
        return result;
    
public synchronized voidremoveElementAt(int index)
If index is missing wait until reasched or complete.

        while ((!isComplete()) && (super.size() < index)) {
            waitUntilAdd();
        }
        super.removeElementAt(index);
    
public synchronized booleanretainAll(java.util.Collection collection)
First wait until complete.

        waitUntilComplete();
        return super.retainAll(collection);
    
public synchronized java.lang.Objectset(int index, java.lang.Object element)
If index is missing wait until reached or complete.

        while ((!isComplete()) && (super.size() < index)) {
            waitUntilAdd();
        }
        return super.set(index, element);
    
public synchronized voidsetElementAt(java.lang.Object element, int index)
If index is missing wait until reached or complete.

        while ((!isComplete()) && (super.size() < index)) {
            waitUntilAdd();
        }
        super.setElementAt(element, index);
    
public synchronized voidsetIsComplete(boolean isComplete)
Set the list complete and notify any waiters.

        this.isComplete = isComplete;
        notifyAll();
    
public intsize()
First wait until complete.

        waitUntilComplete();
        return super.size();
    
public java.util.ListsubList(int fromIndex, int toIndex)
If index is missing wait until reached or complete.

        while ((!isComplete()) && (super.size() < toIndex)) {
            waitUntilAdd();
        }
        return super.subList(fromIndex, toIndex);
    
public synchronized voidthrowException(java.lang.RuntimeException exception)
Record that the population thread hit an exception, that should be thrown to the processing thread on the next access. This also records the list and complete.

        this.exception = exception;
        setIsComplete(true);
    
public synchronized java.lang.Object[]toArray()
First wait until complete.

        waitUntilComplete();
        return super.toArray();
    
public synchronized java.lang.Object[]toArray(java.lang.Object[] array)
First wait until complete.

        waitUntilComplete();
        return super.toArray(array);
    
public synchronized java.lang.StringtoString()
First wait until complete.

        waitUntilComplete();
        return super.toString();
    
public synchronized voidtrimToSize()
First wait until complete.

        waitUntilComplete();
        super.trimToSize();
    
public synchronized voidwaitUntilAdd()
Wait until a new element has been added.

        try {
            wait();
        } catch (InterruptedException ignore) {
        }
    
public synchronized voidwaitUntilComplete()
Wait until the list has been fully populated.

        while (!isComplete()) {
            try {
                wait();
            } catch (InterruptedException ignore) {
            }
        }