Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.pool.impl;
  
  import java.util.List;

This class has been copied from Commons Collections, version 3.1 in order to eliminate the dependency of pool on collections. It has package scope to prevent its inclusion in the pool public API. The class declaration below should *not* be changed to public.

A doubly-linked list implementation of the java.util.List interface, supporting a java.util.ListIterator that allows concurrent modifications to the underlying list.

Implements all of the optional java.util.List operations, the stack/queue/dequeue operations available in java.util.LinkedList and supports a java.util.ListIterator that allows concurrent modifications to the underlying list (see cursor()).

Note that this implementation is not synchronized.

Parameters:
<E> the type of elements held in this collection
Author(s):
Rodney Waldhoff
Janek Bogucki
Simon Kitching
Version:
$Revision: 480452 $ $Date: 2006-11-29 00:45:14 -0700 (Wed, 29 Nov 2006) $
See also:
java.util.LinkedList
  
  class CursorableLinkedList<E> implements List<E>, Serializable {
    
Ensure serialization compatibility
  
      private static final long serialVersionUID = 8836393098519411393L;
  
      //--- public methods ---------------------------------------------
      // CHECKSTYLE: stop all checks
  
    
Appends the specified element to the end of this list.

Parameters:
o element to be appended to this list.
Returns:
true
  
      public boolean add(E o) {
          insertListable(.prev(),null,o);
          return true;
      }

    
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index index at which the specified element is to be inserted.
element element to be inserted.
Throws:
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list.
java.lang.IllegalArgumentException if some aspect of the specified element prevents it from being added to this list.
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).
  
      public void add(int index, E element) {
          if(index == ) {
              add(element);
         } else {
             if(index < 0 || index > ) {
                 throw new IndexOutOfBoundsException(String.valueOf(index) + " < 0 or " + String.valueOf(index) + " > " + );
             }
             Listable<E> succ = (isEmpty() ? null : getListableAt(index));
             Listable<E> pred = (null == succ ? null : succ.prev());
             insertListable(pred,succ,element);
         }
     }

    
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified java.util.Collection's java.util.Iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
c collection whose elements are to be added to this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.ClassCastException if the class of an element in the specified collection prevents it from being added to this list.
java.lang.IllegalArgumentException if some aspect of an element in the specified collection prevents it from being added to this list.
 
     public boolean addAll(Collection<? extends E> c) {
         if(c.isEmpty()) {
             return false;
         }
         Iterator<? extends E> it = c.iterator();
         while(it.hasNext()) {
             insertListable(.prev(),null,it.next());
         }
         return true;
     }

    
Inserts all of the elements in the specified collection into this list at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified java.util.Collection's java.util.Iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
index index at which to insert first element from the specified collection.
c elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
java.lang.ClassCastException if the class of one of elements of the specified collection prevents it from being added to this list.
java.lang.IllegalArgumentException if some aspect of one of elements of the specified collection prevents it from being added to this list.
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).
 
     public boolean addAll(int indexCollection<? extends E> c) {
         if(c.isEmpty()) {
             return false;
         } else if( == index ||  == 0) {
             return addAll(c);
         } else {
             Listable<E> succ = getListableAt(index);
             Listable<E> pred = (null == succ) ? null : succ.prev();
             Iterator<? extends E> it = c.iterator();
             while(it.hasNext()) {
                 pred = insertListable(pred,succ,it.next());
             }
             return true;
         }
     }

    
Inserts the specified element at the beginning of this list. (Equivalent to add(0,o)).

Parameters:
o element to be prepended to this list.
Returns:
true
 
     public boolean addFirst(E o) {
         insertListable(null,.next(),o);
         return true;
     }

    
Inserts the specified element at the end of this list. (Equivalent to add(java.lang.Object)).

Parameters:
o element to be appended to this list.
Returns:
true
 
     public boolean addLast(E o) {
         insertListable(.prev(),null,o);
         return true;
     }

    
Removes all of the elements from this list. This list will be empty after this call returns (unless it throws an exception).
 
     public void clear() {
         /*
         // this is the quick way, but would force us
         // to break all the cursors
         _modCount++;
         _head.setNext(null);
         _head.setPrev(null);
         _size = 0;
         */
         Iterator<E> it = iterator();
         while(it.hasNext()) {
             it.next();
             it.remove();
         }
     }

    
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Parameters:
o element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.
 
     public boolean contains(Object o) {
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             if((null == o && null == elt.value()) ||
                (o != null && o.equals(elt.value()))) {
                 return true;
             }
         }
         return false;
     }

    
Returns true if this list contains all of the elements of the specified collection.

Parameters:
c collection to be checked for containment in this list.
Returns:
true if this list contains all of the elements of the specified collection.
 
     public boolean containsAll(Collection<?> c) {
         Iterator<?> it = c.iterator();
         while(it.hasNext()) {
             if(!this.contains(it.next())) {
                 return false;
             }
         }
         return true;
     }

    
Returns a java.util.ListIterator for iterating through the elements of this list. Unlike iterator(), a cursor is not bothered by concurrent modifications to the underlying list.

Specifically, when elements are added to the list before or after the cursor, the cursor simply picks them up automatically. When the "current" (i.e., last returned by java.util.ListIterator.next() or java.util.ListIterator.previous()) element of the list is removed, the cursor automatically adjusts to the change (invalidating the last returned value--i.e., it cannot be removed).

Note that the returned java.util.ListIterator does not support the java.util.ListIterator.nextIndex() and java.util.ListIterator.previousIndex() methods (they throw java.lang.UnsupportedOperationException when invoked.

Historical Note: In previous versions of this class, the object returned from this method was required to be explicitly closed. This is no longer necessary.

 
     public CursorableLinkedList<E>.Cursor cursor() {
         return new Cursor(0);
     }

    
Returns a java.util.ListIterator for iterating through the elements of this list, initialized such that java.util.ListIterator.next() will return the element at the specified index (if any) and java.util.ListIterator.previous() will return the element immediately preceding it (if any). Unlike iterator(), a cursor is not bothered by concurrent modifications to the underlying list.

Throws:
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).
See also:
cursor()
listIterator(int)
CursorableLinkedList.Cursor
 
     public CursorableLinkedList<E>.Cursor cursor(int i) {
         return new Cursor(i);
     }

    
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Parameters:
o the object to be compared for equality with this list.
Returns:
true if the specified object is equal to this list.
 
     @Override
     public boolean equals(Object o) {
         if(o == this) {
             return true;
         } else if(!(o instanceof List)) {
             return false;
         }
         Iterator<?> it = ((List<?>)o).listIterator();
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             if(!it.hasNext() || (null == elt.value() ? null != it.next() : !(elt.value().equals(it.next()))) ) {
                 return false;
             }
         }
         return !it.hasNext();
     }

    
Returns the element at the specified position in this list.

Parameters:
index index of element to return.
Returns:
the element at the specified position in this list.
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).
 
     public E get(int index) {
         return getListableAt(index).value();
     }

    
Returns the element at the beginning of this list.
 
     public E getFirst() {
         try {
             return .next().value();
         } catch(NullPointerException e) {
             throw new NoSuchElementException();
         }
     }

    
Returns the element at the end of this list.
 
     public E getLast() {
         try {
             return .prev().value();
         } catch(NullPointerException e) {
             throw new NoSuchElementException();
         }
     }

    
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
  hashCode = 1;
  Iterator i = list.iterator();
  while (i.hasNext()) {
      Object obj = i.next();
      hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  }
 
This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode.

 
     @Override
     public int hashCode() {
         int hash = 1;
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             hash = 31*hash + (null == elt.value() ? 0 : elt.value().hashCode());
         }
         return hash;
     }

    
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o element to search for.
Returns:
the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
 
     public int indexOf(Object o) {
         int ndx = 0;
 
         // perform the null check outside of the loop to save checking every
         // single time through the loop.
         if (null == o) {
             for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
                 if (null == elt.value()) {
                     return ndx;
                 }
                 ndx++;
             }
         } else {
 
             for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
                 if (o.equals(elt.value())) {
                     return ndx;
                 }
                 ndx++;
             }
         }
         return -1;
     }

    
Returns true if this list contains no elements.

Returns:
true if this list contains no elements.
 
     public boolean isEmpty() {
         return(0 == );
     }

    
Returns a fail-fast iterator.

 
     public Iterator<E> iterator() {
         return listIterator(0);
     }

    
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o element to search for.
Returns:
the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
 
     public int lastIndexOf(Object o) {
         int ndx = -1;
 
         // perform the null check outside of the loop to save checking every
         // single time through the loop.
         if (null == o) {
             for(Listable<E> elt = .prev(), past = nullnull != elt && past != .next(); elt = (past = elt).prev()) {
                 if (null == elt.value()) {
                     return ndx;
                 }
                 ndx--;
             }
         } else {
             for(Listable<E> elt = .prev(), past = nullnull != elt && past != .next(); elt = (past = elt).prev()) {
                 if (o.equals(elt.value())) {
                     return ndx;
                 }
                 ndx--;
             }
         }
         return -1;
     }

    
Returns a fail-fast ListIterator.

 
     public ListIterator<E> listIterator() {
         return listIterator(0);
     }

    
Returns a fail-fast ListIterator.

 
     public ListIterator<E> listIterator(int index) {
         if(index<0 || index > ) {
             throw new IndexOutOfBoundsException(index + " < 0 or > " + );
         }
         return new ListIter(index);
     }

    
Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Parameters:
o element to be removed from this list, if present.
Returns:
true if this list contained the specified element.
 
     public boolean remove(Object o) {
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             if(null == o && null == elt.value()) {
                 removeListable(elt);
                 return true;
             } else if(o != null && o.equals(elt.value())) {
                 removeListable(elt);
                 return true;
             }
         }
         return false;
     }

    
Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters:
index the index of the element to removed.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).
 
     public E remove(int index) {
         Listable<E> elt = getListableAt(index);
         E ret = elt.value();
         removeListable(elt);
         return ret;
     }

    
Removes from this list all the elements that are contained in the specified collection.

Parameters:
c collection that defines which elements will be removed from this list.
Returns:
true if this list changed as a result of the call.
 
     public boolean removeAll(Collection<?> c) {
         if(0 == c.size() || 0 == ) {
             return false;
         } else {
             boolean changed = false;
             Iterator<?> it = iterator();
             while(it.hasNext()) {
                 if(c.contains(it.next())) {
                     it.remove();
                     changed = true;
                 }
             }
             return changed;
         }
     }

    
Removes the first element of this list, if any.
 
     public E removeFirst() {
         if(.next() != null) {
             E val = .next().value();
             removeListable(.next());
             return val;
         } else {
             throw new NoSuchElementException();
         }
     }

    
Removes the last element of this list, if any.
 
     public E removeLast() {
         if(.prev() != null) {
             E val = .prev().value();
             removeListable(.prev());
             return val;
         } else {
             throw new NoSuchElementException();
         }
     }

    
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all the elements that are not contained in the specified collection.

Parameters:
c collection that defines which elements this set will retain.
Returns:
true if this list changed as a result of the call.
 
     public boolean retainAll(Collection<?> c) {
         boolean changed = false;
         Iterator<?> it = iterator();
         while(it.hasNext()) {
             if(!c.contains(it.next())) {
                 it.remove();
                 changed = true;
             }
         }
         return changed;
     }

    
Replaces the element at the specified position in this list with the specified element.

Parameters:
index index of element to replace.
element element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
java.lang.ClassCastException if the class of the specified element prevents it from being added to this list.
java.lang.IllegalArgumentException if some aspect of the specified element prevents it from being added to this list.
java.lang.IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).
 
     public E set(int index, E element) {
         Listable<E> elt = getListableAt(index);
         E val = elt.setValue(element);
         broadcastListableChanged(elt);
         return val;
     }

    
Returns the number of elements in this list.

Returns:
the number of elements in this list.
 
     public int size() {
         return ;
     }

    
Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the java.util.Collection.toArray() method.

Returns:
an array containing all of the elements in this list in proper sequence.
 
     public Object[] toArray() {
         Object[] array = new Object[];
         int i = 0;
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             array[i++] = elt.value();
         }
         return array;
     }

    
Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the java.util.Collection.toArray() method.

Parameters:
a the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this list.
Throws:
java.lang.ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
 
     @SuppressWarnings("unchecked")
     public <T> T[] toArray(T a[]) {
         if(a.length < ) {
             a = (T[])Array.newInstance(a.getClass().getComponentType(), );
         }
         int i = 0;
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             a[i++] = (T) elt.value();
         }
         if(a.length > ) {
             a[] = null// should we null out the rest of the array also? java.util.LinkedList doesn't
         }
         return a;
     }

    
Returns a java.lang.String representation of this list, suitable for debugging.

Returns:
a java.lang.String representation of this list, suitable for debugging.
 
     @Override
     public String toString() {
         StringBuffer buf = new StringBuffer();
         buf.append("[");
         for(Listable<E> elt = .next(), past = nullnull != elt && past != .prev(); elt = (past = elt).next()) {
             if(.next() != elt) {
                 buf.append(", ");
             }
             buf.append(elt.value());
         }
         buf.append("]");
         return buf.toString();
     }

    
Returns a fail-fast sublist.

 
     public List<E> subList(int iint j) {
         if(i < 0 || j >  || i > j) {
             throw new IndexOutOfBoundsException();
         } else if(i == 0 && j == ) {
             return this;
         } else {
             return new CursorableSubList<E>(this,i,j);
         }
     }
 
     //--- protected methods ------------------------------------------
 
    
Inserts a new value into my list, after the specified before element, and before the specified after element

Returns:
the newly created org.apache.commons.collections.CursorableLinkedList.Listable
 
     protected Listable<E> insertListable(Listable<E> beforeListable<E> after, E value) {
         ++;
         ++;
         Listable<E> elt = new Listable<E>(before,after,value);
         if(null != before) {
             before.setNext(elt);
         } else {
             .setNext(elt);
         }
 
         if(null != after) {
             after.setPrev(elt);
         } else {
             .setPrev(elt);
         }
         broadcastListableInserted(elt);
         return elt;
     }

    
Removes the given org.apache.commons.collections.CursorableLinkedList.Listable from my list.
 
     protected void removeListable(Listable<E> elt) {
         ++;
         --;
         if(.next() == elt) {
             .setNext(elt.next());
         }
         if(null != elt.next()) {
             elt.next().setPrev(elt.prev());
         }
         if(.prev() == elt) {
             .setPrev(elt.prev());
         }
         if(null != elt.prev()) {
             elt.prev().setNext(elt.next());
         }
         broadcastListableRemoved(elt);
     }

    
Returns the org.apache.commons.collections.CursorableLinkedList.Listable at the specified index.

Throws:
java.lang.IndexOutOfBoundsException if index is less than zero or greater than or equal to the size of this list.
 
     protected Listable<E> getListableAt(int index) {
         if(index < 0 || index >= ) {
             throw new IndexOutOfBoundsException(String.valueOf(index) + " < 0 or " + String.valueOf(index) + " >= " + );
         }
         if(index <=/2) {
             Listable<E> elt = .next();
             for(int i = 0; i < indexi++) {
                 elt = elt.next();
             }
             return elt;
         } else {
             Listable<E> elt = .prev();
             for(int i = (-1); i > indexi--) {
                 elt = elt.prev();
             }
             return elt;
         }
     }

    
Registers a CursorableLinkedList.Cursor to be notified of changes to this list.
 
     protected void registerCursor(Cursor cur) {
         // We take this opportunity to clean the _cursors list
         // of WeakReference objects to garbage-collected cursors.
         for (Iterator<WeakReference<Cursor>> it = .iterator(); it.hasNext(); ) {
             WeakReference<Cursorref = it.next();
             if (ref.get() == null) {
                 it.remove();
             }
         }
 
         .addnew WeakReference<Cursor>(cur) );
     }

    
Removes a CursorableLinkedList.Cursor from the set of cursors to be notified of changes to this list.
 
     protected void unregisterCursor(Cursor cur) {
         for (Iterator<WeakReference<Cursor>> it = .iterator(); it.hasNext(); ) {
             WeakReference<Cursorref = it.next();
             Cursor cursor = ref.get();
             if (cursor == null) {
                 // some other unrelated cursor object has been
                 // garbage-collected; let's take the opportunity to
                 // clean up the cursors list anyway..
                 it.remove();
 
             } else if (cursor == cur) {
                 ref.clear();
                 it.remove();
                 break;
             }
         }
     }

    
Informs all of my registered cursors that they are now invalid.
 
     protected void invalidateCursors() {
         Iterator<WeakReference<Cursor>> it = .iterator();
         while (it.hasNext()) {
             WeakReference<Cursorref = it.next();
             Cursor cursor = ref.get();
             if (cursor != null) {
                 // cursor is null if object has been garbage-collected
                 cursor.invalidate();
                 ref.clear();
             }
             it.remove();
         }
     }

    
Informs all of my registered cursors that the specified element was changed.

 
     protected void broadcastListableChanged(Listable<E> elt) {
         Iterator<WeakReference<Cursor>> it = .iterator();
         while (it.hasNext()) {
             WeakReference<Cursorref = it.next();
             Cursor cursor = ref.get();
             if (cursor == null) {
                 it.remove(); // clean up list
             } else {
                 cursor.listableChanged(elt);
             }
         }
     }

    
Informs all of my registered cursors that the specified element was just removed from my list.
 
     protected void broadcastListableRemoved(Listable<E> elt) {
         Iterator<WeakReference<Cursor>> it = .iterator();
         while (it.hasNext()) {
             WeakReference<Cursorref = it.next();
             Cursor cursor = ref.get();
             if (cursor == null) {
                 it.remove(); // clean up list
             } else {
                 cursor.listableRemoved(elt);
             }
         }
     }

    
Informs all of my registered cursors that the specified element was just added to my list.
 
     protected void broadcastListableInserted(Listable<E> elt) {
         Iterator<WeakReference<Cursor>> it = .iterator();
         while (it.hasNext()) {
             WeakReference<Cursorref = it.next();
             Cursor cursor = ref.get();
             if (cursor == null) {
                 it.remove();  // clean up list
             } else {
                 cursor.listableInserted(elt);
             }
         }
     }
 
     private void writeObject(ObjectOutputStream outthrows IOException {
         out.defaultWriteObject();
         out.writeInt();
         Listable<E> cur = .next();
         while (cur != null) {
             out.writeObject(cur.value());
             cur = cur.next();
         }
     }
 
     @SuppressWarnings("unchecked")
     private void readObject(ObjectInputStream inthrows IOExceptionClassNotFoundException {
         in.defaultReadObject();
          = 0;
          = 0;
          = new ArrayList<WeakReference<Cursor>>();
          = new Listable<E>(null,null,null);
         int size = in.readInt();
         for (int i=0;i<size;i++) {
             this.add((E)in.readObject());
         }
     }
 
     //--- protected attributes ---------------------------------------
 
    
The number of elements in me.
 
     protected transient int _size = 0;

    
A sentry node.

_head.next() points to the first element in the list, _head.prev() to the last. Note that it is possible for _head.next().prev() and _head.prev().next() to be non-null, as when I am a sublist for some larger list. Use == _head.next() and == _head.prev() to determine if a given org.apache.commons.collections.CursorableLinkedList.Listable is the first or last element in the list.

 
     protected transient Listable<E> _head = new Listable<E>(null,null,null);

    
Tracks the number of structural modifications to me.
 
     protected transient int _modCount = 0;

    
A list of the currently CursorableLinkedList.Cursors currently open in this list.
 
     protected transient List<WeakReference<Cursor>> _cursors = new ArrayList<WeakReference<Cursor>>();
 
     //--- inner classes ----------------------------------------------
 
     static class Listable<E> implements Serializable {
         private Listable<E> _prev = null;
         private Listable<E> _next = null;
         private E _val = null;
 
         Listable(Listable<E> prevListable<E> next, E val) {
              = prev;
              = next;
              = val;
         }
 
         Listable<E> next() {
             return ;
         }
 
         Listable<E> prev() {
             return ;
         }
 
         E value() {
             return ;
         }
 
         void setNext(Listable<E> next) {
              = next;
         }
 
         void setPrev(Listable<E> prev) {
             = prev;
        }
        E setValue(E val) {
            E temp = ;
             = val;
            return temp;
        }
    }
    class ListIter implements ListIterator<E> {
        Listable<E> _cur = null;
        Listable<E> _lastReturned = null;
        int _expectedModCount = ;
        int _nextIndex = 0;
        ListIter(int index) {
            if(index == 0) {
                 = new Listable<E>(null,.next(),null);
                 = 0;
            } else if(index == ) {
                 = new Listable<E>(.prev(),null,null);
                 = ;
            } else {
                Listable<E> temp = getListableAt(index);
                 = new Listable<E>(temp.prev(),temp,null);
                 = index;
            }
        }
        public E previous() {
            checkForComod();
            if(!hasPrevious()) {
                throw new NoSuchElementException();
            } else {
                E ret = .prev().value();
                 = .prev();
                .setNext(.prev());
                .setPrev(.prev().prev());
                --;
                return ret;
            }
        }
        public boolean hasNext() {
            checkForComod();
            return(null != .next() && .prev() != .prev());
        }
        public E next() {
            checkForComod();
            if(!hasNext()) {
                throw new NoSuchElementException();
            } else {
                E ret = .next().value();
                 = .next();
                .setPrev(.next());
                .setNext(.next().next());
                ++;
                return ret;
            }
        }
        public int previousIndex() {
            checkForComod();
            if(!hasPrevious()) {
                return -1;
            }
            return -1;
        }
        public boolean hasPrevious() {
            checkForComod();
            return(null != .prev() && .next() != .next());
        }
        public void set(E o) {
            checkForComod();
            try {
                .setValue(o);
            } catch(NullPointerException e) {
                throw new IllegalStateException();
            }
        }
        public int nextIndex() {
            checkForComod();
            if(!hasNext()) {
                return size();
            }
            return ;
        }
        public void remove() {
            checkForComod();
            if(null == ) {
                throw new IllegalStateException();
            } else {
                .setNext( == .prev() ? null : .next());
                .setPrev( == .next() ? null : .prev());
                removeListable();
                 = null;
                --;
                ++;
            }
        }
        public void add(E o) {
            checkForComod();
            .setPrev(insertListable(.prev(),.next(),o));
             = null;
            ++;
            ++;
        }
        protected void checkForComod() {
            if( != ) {
                throw new ConcurrentModificationException();
            }
        }
    }
    public class Cursor extends ListIter implements ListIterator<E> {
        boolean _valid = false;
        Cursor(int index) {
            super(index);
             = true;
            registerCursor(this);
        }
        @Override
        public int previousIndex() {
            throw new UnsupportedOperationException();
        }
        @Override
        public int nextIndex() {
            throw new UnsupportedOperationException();
        }
        @Override
        public void add(E o) {
            checkForComod();
            Listable<E> elt = insertListable(.prev(),.next(),o);
            .setPrev(elt);
            .setNext(elt.next());
             = null;
            ++;
            ++;
        }
        protected void listableRemoved(Listable<E> elt) {
            if(null == .prev()) {
                .setNext(null);
            } else if(.next() == elt) {
                .setNext(elt.next());
            }
            if(null == .next()) {
                .setPrev(null);
            } else if(.prev() == elt) {
                .setPrev(elt.prev());
            }
            if( == elt) {
                 = null;
            }
        }
        protected void listableInserted(Listable<E> elt) {
            if(null == .next() && null == .prev()) {
                .setNext(elt);
            } else if(.prev() == elt.prev()) {
                .setNext(elt);
            }
            if(.next() == elt.next()) {
                .setPrev(elt);
            }
            if( == elt) {
                 = null;
            }
        }
        protected void listableChanged(Listable<E> elt) {
            if( == elt) {
                 = null;
            }
        }
        @Override
        protected void checkForComod() {
            if(!) {
                throw new ConcurrentModificationException();
            }
        }
        protected void invalidate() {
             = false;
        }

        
Mark this cursor as no longer being needed. Any resources associated with this cursor are immediately released. In previous versions of this class, it was mandatory to close all cursor objects to avoid memory leaks. It is no longer necessary to call this close method; an instance of this class can now be treated exactly like a normal iterator.
        public void close() {
            if() {
                 = false;
                unregisterCursor(this);
            }
        }
    }
class CursorableSubList<E> extends CursorableLinkedList<E> implements List<E> {
    //--- constructors -----------------------------------------------
    CursorableSubList(CursorableLinkedList<E> listint fromint to) {
        if(0 > from || list.size() < to) {
            throw new IndexOutOfBoundsException();
        } else if(from > to) {
            throw new IllegalArgumentException();
        }
         = list;
        if(from < list.size()) {
            .setNext(.getListableAt(from));
             = (null == .next()) ? null : .next().prev();
        } else {
             = .getListableAt(from-1);
        }
        if(from == to) {
            .setNext(null);
            .setPrev(null);
            if(to < list.size()) {
                 = .getListableAt(to);
            } else {
                 = null;
            }
        } else {
            .setPrev(.getListableAt(to-1));
             = .prev().next();
        }
         = to - from;
         = .;
    }
    //--- public methods ------------------------------------------
    @Override
    public void clear() {
        checkForComod();
        Iterator<E> it = iterator();
        while(it.hasNext()) {
            it.next();
            it.remove();
        }
    }
    @Override
    public Iterator<E> iterator() {
        checkForComod();
        return super.iterator();
    }
    @Override
    public int size() {
        checkForComod();
        return super.size();
    }
    @Override
    public boolean isEmpty() {
        checkForComod();
        return super.isEmpty();
    }
    @Override
    public Object[] toArray() {
        checkForComod();
        return super.toArray();
    }
    @Override
    public <T> T[] toArray(T a[]) {
        checkForComod();
        return super.toArray(a);
    }
    @Override
    public boolean contains(Object o) {
        checkForComod();
        return super.contains(o);
    }
    @Override
    public boolean remove(Object o) {
        checkForComod();
        return super.remove(o);
    }
    @Override
    public E removeFirst() {
        checkForComod();
        return super.removeFirst();
    }
    @Override
    public E removeLast() {
        checkForComod();
        return super.removeLast();
    }
    @Override
    public boolean addAll(Collection<? extends E> c) {
        checkForComod();
        return super.addAll(c);
    }
    @Override
    public boolean add(E o) {
        checkForComod();
        return super.add(o);
    }
    @Override
    public boolean addFirst(E o) {
        checkForComod();
        return super.addFirst(o);
    }
    @Override
    public boolean addLast(E o) {
        checkForComod();
        return super.addLast(o);
    }
    @Override
    public boolean removeAll(Collection<?> c) {
        checkForComod();
        return super.removeAll(c);
    }
    @Override
    public boolean containsAll(Collection<?> c) {
        checkForComod();
        return super.containsAll(c);
    }