emul/compact/src/main/java/java/util/LinkedList.java
changeset 772 d382dacfd73f
parent 771 4252bfc396fc
child 773 406faa8bc64f
     1.1 --- a/emul/compact/src/main/java/java/util/LinkedList.java	Tue Feb 26 14:55:55 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1100 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.  Oracle designates this
    1.11 - * particular file as subject to the "Classpath" exception as provided
    1.12 - * by Oracle in the LICENSE file that accompanied this code.
    1.13 - *
    1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 - * version 2 for more details (a copy is included in the LICENSE file that
    1.18 - * accompanied this code).
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License version
    1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 - *
    1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 - * or visit www.oracle.com if you need additional information or have any
    1.26 - * questions.
    1.27 - */
    1.28 -
    1.29 -package java.util;
    1.30 -
    1.31 -/**
    1.32 - * Doubly-linked list implementation of the {@code List} and {@code Deque}
    1.33 - * interfaces.  Implements all optional list operations, and permits all
    1.34 - * elements (including {@code null}).
    1.35 - *
    1.36 - * <p>All of the operations perform as could be expected for a doubly-linked
    1.37 - * list.  Operations that index into the list will traverse the list from
    1.38 - * the beginning or the end, whichever is closer to the specified index.
    1.39 - *
    1.40 - * <p><strong>Note that this implementation is not synchronized.</strong>
    1.41 - * If multiple threads access a linked list concurrently, and at least
    1.42 - * one of the threads modifies the list structurally, it <i>must</i> be
    1.43 - * synchronized externally.  (A structural modification is any operation
    1.44 - * that adds or deletes one or more elements; merely setting the value of
    1.45 - * an element is not a structural modification.)  This is typically
    1.46 - * accomplished by synchronizing on some object that naturally
    1.47 - * encapsulates the list.
    1.48 - *
    1.49 - * If no such object exists, the list should be "wrapped" using the
    1.50 - * {@link Collections#synchronizedList Collections.synchronizedList}
    1.51 - * method.  This is best done at creation time, to prevent accidental
    1.52 - * unsynchronized access to the list:<pre>
    1.53 - *   List list = Collections.synchronizedList(new LinkedList(...));</pre>
    1.54 - *
    1.55 - * <p>The iterators returned by this class's {@code iterator} and
    1.56 - * {@code listIterator} methods are <i>fail-fast</i>: if the list is
    1.57 - * structurally modified at any time after the iterator is created, in
    1.58 - * any way except through the Iterator's own {@code remove} or
    1.59 - * {@code add} methods, the iterator will throw a {@link
    1.60 - * ConcurrentModificationException}.  Thus, in the face of concurrent
    1.61 - * modification, the iterator fails quickly and cleanly, rather than
    1.62 - * risking arbitrary, non-deterministic behavior at an undetermined
    1.63 - * time in the future.
    1.64 - *
    1.65 - * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
    1.66 - * as it is, generally speaking, impossible to make any hard guarantees in the
    1.67 - * presence of unsynchronized concurrent modification.  Fail-fast iterators
    1.68 - * throw {@code ConcurrentModificationException} on a best-effort basis.
    1.69 - * Therefore, it would be wrong to write a program that depended on this
    1.70 - * exception for its correctness:   <i>the fail-fast behavior of iterators
    1.71 - * should be used only to detect bugs.</i>
    1.72 - *
    1.73 - * <p>This class is a member of the
    1.74 - * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    1.75 - * Java Collections Framework</a>.
    1.76 - *
    1.77 - * @author  Josh Bloch
    1.78 - * @see     List
    1.79 - * @see     ArrayList
    1.80 - * @since 1.2
    1.81 - * @param <E> the type of elements held in this collection
    1.82 - */
    1.83 -
    1.84 -public class LinkedList<E>
    1.85 -    extends AbstractSequentialList<E>
    1.86 -    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
    1.87 -{
    1.88 -    transient int size = 0;
    1.89 -
    1.90 -    /**
    1.91 -     * Pointer to first node.
    1.92 -     * Invariant: (first == null && last == null) ||
    1.93 -     *            (first.prev == null && first.item != null)
    1.94 -     */
    1.95 -    transient Node<E> first;
    1.96 -
    1.97 -    /**
    1.98 -     * Pointer to last node.
    1.99 -     * Invariant: (first == null && last == null) ||
   1.100 -     *            (last.next == null && last.item != null)
   1.101 -     */
   1.102 -    transient Node<E> last;
   1.103 -
   1.104 -    /**
   1.105 -     * Constructs an empty list.
   1.106 -     */
   1.107 -    public LinkedList() {
   1.108 -    }
   1.109 -
   1.110 -    /**
   1.111 -     * Constructs a list containing the elements of the specified
   1.112 -     * collection, in the order they are returned by the collection's
   1.113 -     * iterator.
   1.114 -     *
   1.115 -     * @param  c the collection whose elements are to be placed into this list
   1.116 -     * @throws NullPointerException if the specified collection is null
   1.117 -     */
   1.118 -    public LinkedList(Collection<? extends E> c) {
   1.119 -        this();
   1.120 -        addAll(c);
   1.121 -    }
   1.122 -
   1.123 -    /**
   1.124 -     * Links e as first element.
   1.125 -     */
   1.126 -    private void linkFirst(E e) {
   1.127 -        final Node<E> f = first;
   1.128 -        final Node<E> newNode = new Node<>(null, e, f);
   1.129 -        first = newNode;
   1.130 -        if (f == null)
   1.131 -            last = newNode;
   1.132 -        else
   1.133 -            f.prev = newNode;
   1.134 -        size++;
   1.135 -        modCount++;
   1.136 -    }
   1.137 -
   1.138 -    /**
   1.139 -     * Links e as last element.
   1.140 -     */
   1.141 -    void linkLast(E e) {
   1.142 -        final Node<E> l = last;
   1.143 -        final Node<E> newNode = new Node<>(l, e, null);
   1.144 -        last = newNode;
   1.145 -        if (l == null)
   1.146 -            first = newNode;
   1.147 -        else
   1.148 -            l.next = newNode;
   1.149 -        size++;
   1.150 -        modCount++;
   1.151 -    }
   1.152 -
   1.153 -    /**
   1.154 -     * Inserts element e before non-null Node succ.
   1.155 -     */
   1.156 -    void linkBefore(E e, Node<E> succ) {
   1.157 -        // assert succ != null;
   1.158 -        final Node<E> pred = succ.prev;
   1.159 -        final Node<E> newNode = new Node<>(pred, e, succ);
   1.160 -        succ.prev = newNode;
   1.161 -        if (pred == null)
   1.162 -            first = newNode;
   1.163 -        else
   1.164 -            pred.next = newNode;
   1.165 -        size++;
   1.166 -        modCount++;
   1.167 -    }
   1.168 -
   1.169 -    /**
   1.170 -     * Unlinks non-null first node f.
   1.171 -     */
   1.172 -    private E unlinkFirst(Node<E> f) {
   1.173 -        // assert f == first && f != null;
   1.174 -        final E element = f.item;
   1.175 -        final Node<E> next = f.next;
   1.176 -        f.item = null;
   1.177 -        f.next = null; // help GC
   1.178 -        first = next;
   1.179 -        if (next == null)
   1.180 -            last = null;
   1.181 -        else
   1.182 -            next.prev = null;
   1.183 -        size--;
   1.184 -        modCount++;
   1.185 -        return element;
   1.186 -    }
   1.187 -
   1.188 -    /**
   1.189 -     * Unlinks non-null last node l.
   1.190 -     */
   1.191 -    private E unlinkLast(Node<E> l) {
   1.192 -        // assert l == last && l != null;
   1.193 -        final E element = l.item;
   1.194 -        final Node<E> prev = l.prev;
   1.195 -        l.item = null;
   1.196 -        l.prev = null; // help GC
   1.197 -        last = prev;
   1.198 -        if (prev == null)
   1.199 -            first = null;
   1.200 -        else
   1.201 -            prev.next = null;
   1.202 -        size--;
   1.203 -        modCount++;
   1.204 -        return element;
   1.205 -    }
   1.206 -
   1.207 -    /**
   1.208 -     * Unlinks non-null node x.
   1.209 -     */
   1.210 -    E unlink(Node<E> x) {
   1.211 -        // assert x != null;
   1.212 -        final E element = x.item;
   1.213 -        final Node<E> next = x.next;
   1.214 -        final Node<E> prev = x.prev;
   1.215 -
   1.216 -        if (prev == null) {
   1.217 -            first = next;
   1.218 -        } else {
   1.219 -            prev.next = next;
   1.220 -            x.prev = null;
   1.221 -        }
   1.222 -
   1.223 -        if (next == null) {
   1.224 -            last = prev;
   1.225 -        } else {
   1.226 -            next.prev = prev;
   1.227 -            x.next = null;
   1.228 -        }
   1.229 -
   1.230 -        x.item = null;
   1.231 -        size--;
   1.232 -        modCount++;
   1.233 -        return element;
   1.234 -    }
   1.235 -
   1.236 -    /**
   1.237 -     * Returns the first element in this list.
   1.238 -     *
   1.239 -     * @return the first element in this list
   1.240 -     * @throws NoSuchElementException if this list is empty
   1.241 -     */
   1.242 -    public E getFirst() {
   1.243 -        final Node<E> f = first;
   1.244 -        if (f == null)
   1.245 -            throw new NoSuchElementException();
   1.246 -        return f.item;
   1.247 -    }
   1.248 -
   1.249 -    /**
   1.250 -     * Returns the last element in this list.
   1.251 -     *
   1.252 -     * @return the last element in this list
   1.253 -     * @throws NoSuchElementException if this list is empty
   1.254 -     */
   1.255 -    public E getLast() {
   1.256 -        final Node<E> l = last;
   1.257 -        if (l == null)
   1.258 -            throw new NoSuchElementException();
   1.259 -        return l.item;
   1.260 -    }
   1.261 -
   1.262 -    /**
   1.263 -     * Removes and returns the first element from this list.
   1.264 -     *
   1.265 -     * @return the first element from this list
   1.266 -     * @throws NoSuchElementException if this list is empty
   1.267 -     */
   1.268 -    public E removeFirst() {
   1.269 -        final Node<E> f = first;
   1.270 -        if (f == null)
   1.271 -            throw new NoSuchElementException();
   1.272 -        return unlinkFirst(f);
   1.273 -    }
   1.274 -
   1.275 -    /**
   1.276 -     * Removes and returns the last element from this list.
   1.277 -     *
   1.278 -     * @return the last element from this list
   1.279 -     * @throws NoSuchElementException if this list is empty
   1.280 -     */
   1.281 -    public E removeLast() {
   1.282 -        final Node<E> l = last;
   1.283 -        if (l == null)
   1.284 -            throw new NoSuchElementException();
   1.285 -        return unlinkLast(l);
   1.286 -    }
   1.287 -
   1.288 -    /**
   1.289 -     * Inserts the specified element at the beginning of this list.
   1.290 -     *
   1.291 -     * @param e the element to add
   1.292 -     */
   1.293 -    public void addFirst(E e) {
   1.294 -        linkFirst(e);
   1.295 -    }
   1.296 -
   1.297 -    /**
   1.298 -     * Appends the specified element to the end of this list.
   1.299 -     *
   1.300 -     * <p>This method is equivalent to {@link #add}.
   1.301 -     *
   1.302 -     * @param e the element to add
   1.303 -     */
   1.304 -    public void addLast(E e) {
   1.305 -        linkLast(e);
   1.306 -    }
   1.307 -
   1.308 -    /**
   1.309 -     * Returns {@code true} if this list contains the specified element.
   1.310 -     * More formally, returns {@code true} if and only if this list contains
   1.311 -     * at least one element {@code e} such that
   1.312 -     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
   1.313 -     *
   1.314 -     * @param o element whose presence in this list is to be tested
   1.315 -     * @return {@code true} if this list contains the specified element
   1.316 -     */
   1.317 -    public boolean contains(Object o) {
   1.318 -        return indexOf(o) != -1;
   1.319 -    }
   1.320 -
   1.321 -    /**
   1.322 -     * Returns the number of elements in this list.
   1.323 -     *
   1.324 -     * @return the number of elements in this list
   1.325 -     */
   1.326 -    public int size() {
   1.327 -        return size;
   1.328 -    }
   1.329 -
   1.330 -    /**
   1.331 -     * Appends the specified element to the end of this list.
   1.332 -     *
   1.333 -     * <p>This method is equivalent to {@link #addLast}.
   1.334 -     *
   1.335 -     * @param e element to be appended to this list
   1.336 -     * @return {@code true} (as specified by {@link Collection#add})
   1.337 -     */
   1.338 -    public boolean add(E e) {
   1.339 -        linkLast(e);
   1.340 -        return true;
   1.341 -    }
   1.342 -
   1.343 -    /**
   1.344 -     * Removes the first occurrence of the specified element from this list,
   1.345 -     * if it is present.  If this list does not contain the element, it is
   1.346 -     * unchanged.  More formally, removes the element with the lowest index
   1.347 -     * {@code i} such that
   1.348 -     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
   1.349 -     * (if such an element exists).  Returns {@code true} if this list
   1.350 -     * contained the specified element (or equivalently, if this list
   1.351 -     * changed as a result of the call).
   1.352 -     *
   1.353 -     * @param o element to be removed from this list, if present
   1.354 -     * @return {@code true} if this list contained the specified element
   1.355 -     */
   1.356 -    public boolean remove(Object o) {
   1.357 -        if (o == null) {
   1.358 -            for (Node<E> x = first; x != null; x = x.next) {
   1.359 -                if (x.item == null) {
   1.360 -                    unlink(x);
   1.361 -                    return true;
   1.362 -                }
   1.363 -            }
   1.364 -        } else {
   1.365 -            for (Node<E> x = first; x != null; x = x.next) {
   1.366 -                if (o.equals(x.item)) {
   1.367 -                    unlink(x);
   1.368 -                    return true;
   1.369 -                }
   1.370 -            }
   1.371 -        }
   1.372 -        return false;
   1.373 -    }
   1.374 -
   1.375 -    /**
   1.376 -     * Appends all of the elements in the specified collection to the end of
   1.377 -     * this list, in the order that they are returned by the specified
   1.378 -     * collection's iterator.  The behavior of this operation is undefined if
   1.379 -     * the specified collection is modified while the operation is in
   1.380 -     * progress.  (Note that this will occur if the specified collection is
   1.381 -     * this list, and it's nonempty.)
   1.382 -     *
   1.383 -     * @param c collection containing elements to be added to this list
   1.384 -     * @return {@code true} if this list changed as a result of the call
   1.385 -     * @throws NullPointerException if the specified collection is null
   1.386 -     */
   1.387 -    public boolean addAll(Collection<? extends E> c) {
   1.388 -        return addAll(size, c);
   1.389 -    }
   1.390 -
   1.391 -    /**
   1.392 -     * Inserts all of the elements in the specified collection into this
   1.393 -     * list, starting at the specified position.  Shifts the element
   1.394 -     * currently at that position (if any) and any subsequent elements to
   1.395 -     * the right (increases their indices).  The new elements will appear
   1.396 -     * in the list in the order that they are returned by the
   1.397 -     * specified collection's iterator.
   1.398 -     *
   1.399 -     * @param index index at which to insert the first element
   1.400 -     *              from the specified collection
   1.401 -     * @param c collection containing elements to be added to this list
   1.402 -     * @return {@code true} if this list changed as a result of the call
   1.403 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.404 -     * @throws NullPointerException if the specified collection is null
   1.405 -     */
   1.406 -    public boolean addAll(int index, Collection<? extends E> c) {
   1.407 -        checkPositionIndex(index);
   1.408 -
   1.409 -        Object[] a = c.toArray();
   1.410 -        int numNew = a.length;
   1.411 -        if (numNew == 0)
   1.412 -            return false;
   1.413 -
   1.414 -        Node<E> pred, succ;
   1.415 -        if (index == size) {
   1.416 -            succ = null;
   1.417 -            pred = last;
   1.418 -        } else {
   1.419 -            succ = node(index);
   1.420 -            pred = succ.prev;
   1.421 -        }
   1.422 -
   1.423 -        for (Object o : a) {
   1.424 -            @SuppressWarnings("unchecked") E e = (E) o;
   1.425 -            Node<E> newNode = new Node<>(pred, e, null);
   1.426 -            if (pred == null)
   1.427 -                first = newNode;
   1.428 -            else
   1.429 -                pred.next = newNode;
   1.430 -            pred = newNode;
   1.431 -        }
   1.432 -
   1.433 -        if (succ == null) {
   1.434 -            last = pred;
   1.435 -        } else {
   1.436 -            pred.next = succ;
   1.437 -            succ.prev = pred;
   1.438 -        }
   1.439 -
   1.440 -        size += numNew;
   1.441 -        modCount++;
   1.442 -        return true;
   1.443 -    }
   1.444 -
   1.445 -    /**
   1.446 -     * Removes all of the elements from this list.
   1.447 -     * The list will be empty after this call returns.
   1.448 -     */
   1.449 -    public void clear() {
   1.450 -        // Clearing all of the links between nodes is "unnecessary", but:
   1.451 -        // - helps a generational GC if the discarded nodes inhabit
   1.452 -        //   more than one generation
   1.453 -        // - is sure to free memory even if there is a reachable Iterator
   1.454 -        for (Node<E> x = first; x != null; ) {
   1.455 -            Node<E> next = x.next;
   1.456 -            x.item = null;
   1.457 -            x.next = null;
   1.458 -            x.prev = null;
   1.459 -            x = next;
   1.460 -        }
   1.461 -        first = last = null;
   1.462 -        size = 0;
   1.463 -        modCount++;
   1.464 -    }
   1.465 -
   1.466 -
   1.467 -    // Positional Access Operations
   1.468 -
   1.469 -    /**
   1.470 -     * Returns the element at the specified position in this list.
   1.471 -     *
   1.472 -     * @param index index of the element to return
   1.473 -     * @return the element at the specified position in this list
   1.474 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.475 -     */
   1.476 -    public E get(int index) {
   1.477 -        checkElementIndex(index);
   1.478 -        return node(index).item;
   1.479 -    }
   1.480 -
   1.481 -    /**
   1.482 -     * Replaces the element at the specified position in this list with the
   1.483 -     * specified element.
   1.484 -     *
   1.485 -     * @param index index of the element to replace
   1.486 -     * @param element element to be stored at the specified position
   1.487 -     * @return the element previously at the specified position
   1.488 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.489 -     */
   1.490 -    public E set(int index, E element) {
   1.491 -        checkElementIndex(index);
   1.492 -        Node<E> x = node(index);
   1.493 -        E oldVal = x.item;
   1.494 -        x.item = element;
   1.495 -        return oldVal;
   1.496 -    }
   1.497 -
   1.498 -    /**
   1.499 -     * Inserts the specified element at the specified position in this list.
   1.500 -     * Shifts the element currently at that position (if any) and any
   1.501 -     * subsequent elements to the right (adds one to their indices).
   1.502 -     *
   1.503 -     * @param index index at which the specified element is to be inserted
   1.504 -     * @param element element to be inserted
   1.505 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.506 -     */
   1.507 -    public void add(int index, E element) {
   1.508 -        checkPositionIndex(index);
   1.509 -
   1.510 -        if (index == size)
   1.511 -            linkLast(element);
   1.512 -        else
   1.513 -            linkBefore(element, node(index));
   1.514 -    }
   1.515 -
   1.516 -    /**
   1.517 -     * Removes the element at the specified position in this list.  Shifts any
   1.518 -     * subsequent elements to the left (subtracts one from their indices).
   1.519 -     * Returns the element that was removed from the list.
   1.520 -     *
   1.521 -     * @param index the index of the element to be removed
   1.522 -     * @return the element previously at the specified position
   1.523 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.524 -     */
   1.525 -    public E remove(int index) {
   1.526 -        checkElementIndex(index);
   1.527 -        return unlink(node(index));
   1.528 -    }
   1.529 -
   1.530 -    /**
   1.531 -     * Tells if the argument is the index of an existing element.
   1.532 -     */
   1.533 -    private boolean isElementIndex(int index) {
   1.534 -        return index >= 0 && index < size;
   1.535 -    }
   1.536 -
   1.537 -    /**
   1.538 -     * Tells if the argument is the index of a valid position for an
   1.539 -     * iterator or an add operation.
   1.540 -     */
   1.541 -    private boolean isPositionIndex(int index) {
   1.542 -        return index >= 0 && index <= size;
   1.543 -    }
   1.544 -
   1.545 -    /**
   1.546 -     * Constructs an IndexOutOfBoundsException detail message.
   1.547 -     * Of the many possible refactorings of the error handling code,
   1.548 -     * this "outlining" performs best with both server and client VMs.
   1.549 -     */
   1.550 -    private String outOfBoundsMsg(int index) {
   1.551 -        return "Index: "+index+", Size: "+size;
   1.552 -    }
   1.553 -
   1.554 -    private void checkElementIndex(int index) {
   1.555 -        if (!isElementIndex(index))
   1.556 -            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
   1.557 -    }
   1.558 -
   1.559 -    private void checkPositionIndex(int index) {
   1.560 -        if (!isPositionIndex(index))
   1.561 -            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
   1.562 -    }
   1.563 -
   1.564 -    /**
   1.565 -     * Returns the (non-null) Node at the specified element index.
   1.566 -     */
   1.567 -    Node<E> node(int index) {
   1.568 -        // assert isElementIndex(index);
   1.569 -
   1.570 -        if (index < (size >> 1)) {
   1.571 -            Node<E> x = first;
   1.572 -            for (int i = 0; i < index; i++)
   1.573 -                x = x.next;
   1.574 -            return x;
   1.575 -        } else {
   1.576 -            Node<E> x = last;
   1.577 -            for (int i = size - 1; i > index; i--)
   1.578 -                x = x.prev;
   1.579 -            return x;
   1.580 -        }
   1.581 -    }
   1.582 -
   1.583 -    // Search Operations
   1.584 -
   1.585 -    /**
   1.586 -     * Returns the index of the first occurrence of the specified element
   1.587 -     * in this list, or -1 if this list does not contain the element.
   1.588 -     * More formally, returns the lowest index {@code i} such that
   1.589 -     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
   1.590 -     * or -1 if there is no such index.
   1.591 -     *
   1.592 -     * @param o element to search for
   1.593 -     * @return the index of the first occurrence of the specified element in
   1.594 -     *         this list, or -1 if this list does not contain the element
   1.595 -     */
   1.596 -    public int indexOf(Object o) {
   1.597 -        int index = 0;
   1.598 -        if (o == null) {
   1.599 -            for (Node<E> x = first; x != null; x = x.next) {
   1.600 -                if (x.item == null)
   1.601 -                    return index;
   1.602 -                index++;
   1.603 -            }
   1.604 -        } else {
   1.605 -            for (Node<E> x = first; x != null; x = x.next) {
   1.606 -                if (o.equals(x.item))
   1.607 -                    return index;
   1.608 -                index++;
   1.609 -            }
   1.610 -        }
   1.611 -        return -1;
   1.612 -    }
   1.613 -
   1.614 -    /**
   1.615 -     * Returns the index of the last occurrence of the specified element
   1.616 -     * in this list, or -1 if this list does not contain the element.
   1.617 -     * More formally, returns the highest index {@code i} such that
   1.618 -     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
   1.619 -     * or -1 if there is no such index.
   1.620 -     *
   1.621 -     * @param o element to search for
   1.622 -     * @return the index of the last occurrence of the specified element in
   1.623 -     *         this list, or -1 if this list does not contain the element
   1.624 -     */
   1.625 -    public int lastIndexOf(Object o) {
   1.626 -        int index = size;
   1.627 -        if (o == null) {
   1.628 -            for (Node<E> x = last; x != null; x = x.prev) {
   1.629 -                index--;
   1.630 -                if (x.item == null)
   1.631 -                    return index;
   1.632 -            }
   1.633 -        } else {
   1.634 -            for (Node<E> x = last; x != null; x = x.prev) {
   1.635 -                index--;
   1.636 -                if (o.equals(x.item))
   1.637 -                    return index;
   1.638 -            }
   1.639 -        }
   1.640 -        return -1;
   1.641 -    }
   1.642 -
   1.643 -    // Queue operations.
   1.644 -
   1.645 -    /**
   1.646 -     * Retrieves, but does not remove, the head (first element) of this list.
   1.647 -     *
   1.648 -     * @return the head of this list, or {@code null} if this list is empty
   1.649 -     * @since 1.5
   1.650 -     */
   1.651 -    public E peek() {
   1.652 -        final Node<E> f = first;
   1.653 -        return (f == null) ? null : f.item;
   1.654 -    }
   1.655 -
   1.656 -    /**
   1.657 -     * Retrieves, but does not remove, the head (first element) of this list.
   1.658 -     *
   1.659 -     * @return the head of this list
   1.660 -     * @throws NoSuchElementException if this list is empty
   1.661 -     * @since 1.5
   1.662 -     */
   1.663 -    public E element() {
   1.664 -        return getFirst();
   1.665 -    }
   1.666 -
   1.667 -    /**
   1.668 -     * Retrieves and removes the head (first element) of this list.
   1.669 -     *
   1.670 -     * @return the head of this list, or {@code null} if this list is empty
   1.671 -     * @since 1.5
   1.672 -     */
   1.673 -    public E poll() {
   1.674 -        final Node<E> f = first;
   1.675 -        return (f == null) ? null : unlinkFirst(f);
   1.676 -    }
   1.677 -
   1.678 -    /**
   1.679 -     * Retrieves and removes the head (first element) of this list.
   1.680 -     *
   1.681 -     * @return the head of this list
   1.682 -     * @throws NoSuchElementException if this list is empty
   1.683 -     * @since 1.5
   1.684 -     */
   1.685 -    public E remove() {
   1.686 -        return removeFirst();
   1.687 -    }
   1.688 -
   1.689 -    /**
   1.690 -     * Adds the specified element as the tail (last element) of this list.
   1.691 -     *
   1.692 -     * @param e the element to add
   1.693 -     * @return {@code true} (as specified by {@link Queue#offer})
   1.694 -     * @since 1.5
   1.695 -     */
   1.696 -    public boolean offer(E e) {
   1.697 -        return add(e);
   1.698 -    }
   1.699 -
   1.700 -    // Deque operations
   1.701 -    /**
   1.702 -     * Inserts the specified element at the front of this list.
   1.703 -     *
   1.704 -     * @param e the element to insert
   1.705 -     * @return {@code true} (as specified by {@link Deque#offerFirst})
   1.706 -     * @since 1.6
   1.707 -     */
   1.708 -    public boolean offerFirst(E e) {
   1.709 -        addFirst(e);
   1.710 -        return true;
   1.711 -    }
   1.712 -
   1.713 -    /**
   1.714 -     * Inserts the specified element at the end of this list.
   1.715 -     *
   1.716 -     * @param e the element to insert
   1.717 -     * @return {@code true} (as specified by {@link Deque#offerLast})
   1.718 -     * @since 1.6
   1.719 -     */
   1.720 -    public boolean offerLast(E e) {
   1.721 -        addLast(e);
   1.722 -        return true;
   1.723 -    }
   1.724 -
   1.725 -    /**
   1.726 -     * Retrieves, but does not remove, the first element of this list,
   1.727 -     * or returns {@code null} if this list is empty.
   1.728 -     *
   1.729 -     * @return the first element of this list, or {@code null}
   1.730 -     *         if this list is empty
   1.731 -     * @since 1.6
   1.732 -     */
   1.733 -    public E peekFirst() {
   1.734 -        final Node<E> f = first;
   1.735 -        return (f == null) ? null : f.item;
   1.736 -     }
   1.737 -
   1.738 -    /**
   1.739 -     * Retrieves, but does not remove, the last element of this list,
   1.740 -     * or returns {@code null} if this list is empty.
   1.741 -     *
   1.742 -     * @return the last element of this list, or {@code null}
   1.743 -     *         if this list is empty
   1.744 -     * @since 1.6
   1.745 -     */
   1.746 -    public E peekLast() {
   1.747 -        final Node<E> l = last;
   1.748 -        return (l == null) ? null : l.item;
   1.749 -    }
   1.750 -
   1.751 -    /**
   1.752 -     * Retrieves and removes the first element of this list,
   1.753 -     * or returns {@code null} if this list is empty.
   1.754 -     *
   1.755 -     * @return the first element of this list, or {@code null} if
   1.756 -     *     this list is empty
   1.757 -     * @since 1.6
   1.758 -     */
   1.759 -    public E pollFirst() {
   1.760 -        final Node<E> f = first;
   1.761 -        return (f == null) ? null : unlinkFirst(f);
   1.762 -    }
   1.763 -
   1.764 -    /**
   1.765 -     * Retrieves and removes the last element of this list,
   1.766 -     * or returns {@code null} if this list is empty.
   1.767 -     *
   1.768 -     * @return the last element of this list, or {@code null} if
   1.769 -     *     this list is empty
   1.770 -     * @since 1.6
   1.771 -     */
   1.772 -    public E pollLast() {
   1.773 -        final Node<E> l = last;
   1.774 -        return (l == null) ? null : unlinkLast(l);
   1.775 -    }
   1.776 -
   1.777 -    /**
   1.778 -     * Pushes an element onto the stack represented by this list.  In other
   1.779 -     * words, inserts the element at the front of this list.
   1.780 -     *
   1.781 -     * <p>This method is equivalent to {@link #addFirst}.
   1.782 -     *
   1.783 -     * @param e the element to push
   1.784 -     * @since 1.6
   1.785 -     */
   1.786 -    public void push(E e) {
   1.787 -        addFirst(e);
   1.788 -    }
   1.789 -
   1.790 -    /**
   1.791 -     * Pops an element from the stack represented by this list.  In other
   1.792 -     * words, removes and returns the first element of this list.
   1.793 -     *
   1.794 -     * <p>This method is equivalent to {@link #removeFirst()}.
   1.795 -     *
   1.796 -     * @return the element at the front of this list (which is the top
   1.797 -     *         of the stack represented by this list)
   1.798 -     * @throws NoSuchElementException if this list is empty
   1.799 -     * @since 1.6
   1.800 -     */
   1.801 -    public E pop() {
   1.802 -        return removeFirst();
   1.803 -    }
   1.804 -
   1.805 -    /**
   1.806 -     * Removes the first occurrence of the specified element in this
   1.807 -     * list (when traversing the list from head to tail).  If the list
   1.808 -     * does not contain the element, it is unchanged.
   1.809 -     *
   1.810 -     * @param o element to be removed from this list, if present
   1.811 -     * @return {@code true} if the list contained the specified element
   1.812 -     * @since 1.6
   1.813 -     */
   1.814 -    public boolean removeFirstOccurrence(Object o) {
   1.815 -        return remove(o);
   1.816 -    }
   1.817 -
   1.818 -    /**
   1.819 -     * Removes the last occurrence of the specified element in this
   1.820 -     * list (when traversing the list from head to tail).  If the list
   1.821 -     * does not contain the element, it is unchanged.
   1.822 -     *
   1.823 -     * @param o element to be removed from this list, if present
   1.824 -     * @return {@code true} if the list contained the specified element
   1.825 -     * @since 1.6
   1.826 -     */
   1.827 -    public boolean removeLastOccurrence(Object o) {
   1.828 -        if (o == null) {
   1.829 -            for (Node<E> x = last; x != null; x = x.prev) {
   1.830 -                if (x.item == null) {
   1.831 -                    unlink(x);
   1.832 -                    return true;
   1.833 -                }
   1.834 -            }
   1.835 -        } else {
   1.836 -            for (Node<E> x = last; x != null; x = x.prev) {
   1.837 -                if (o.equals(x.item)) {
   1.838 -                    unlink(x);
   1.839 -                    return true;
   1.840 -                }
   1.841 -            }
   1.842 -        }
   1.843 -        return false;
   1.844 -    }
   1.845 -
   1.846 -    /**
   1.847 -     * Returns a list-iterator of the elements in this list (in proper
   1.848 -     * sequence), starting at the specified position in the list.
   1.849 -     * Obeys the general contract of {@code List.listIterator(int)}.<p>
   1.850 -     *
   1.851 -     * The list-iterator is <i>fail-fast</i>: if the list is structurally
   1.852 -     * modified at any time after the Iterator is created, in any way except
   1.853 -     * through the list-iterator's own {@code remove} or {@code add}
   1.854 -     * methods, the list-iterator will throw a
   1.855 -     * {@code ConcurrentModificationException}.  Thus, in the face of
   1.856 -     * concurrent modification, the iterator fails quickly and cleanly, rather
   1.857 -     * than risking arbitrary, non-deterministic behavior at an undetermined
   1.858 -     * time in the future.
   1.859 -     *
   1.860 -     * @param index index of the first element to be returned from the
   1.861 -     *              list-iterator (by a call to {@code next})
   1.862 -     * @return a ListIterator of the elements in this list (in proper
   1.863 -     *         sequence), starting at the specified position in the list
   1.864 -     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.865 -     * @see List#listIterator(int)
   1.866 -     */
   1.867 -    public ListIterator<E> listIterator(int index) {
   1.868 -        checkPositionIndex(index);
   1.869 -        return new ListItr(index);
   1.870 -    }
   1.871 -
   1.872 -    private class ListItr implements ListIterator<E> {
   1.873 -        private Node<E> lastReturned = null;
   1.874 -        private Node<E> next;
   1.875 -        private int nextIndex;
   1.876 -        private int expectedModCount = modCount;
   1.877 -
   1.878 -        ListItr(int index) {
   1.879 -            // assert isPositionIndex(index);
   1.880 -            next = (index == size) ? null : node(index);
   1.881 -            nextIndex = index;
   1.882 -        }
   1.883 -
   1.884 -        public boolean hasNext() {
   1.885 -            return nextIndex < size;
   1.886 -        }
   1.887 -
   1.888 -        public E next() {
   1.889 -            checkForComodification();
   1.890 -            if (!hasNext())
   1.891 -                throw new NoSuchElementException();
   1.892 -
   1.893 -            lastReturned = next;
   1.894 -            next = next.next;
   1.895 -            nextIndex++;
   1.896 -            return lastReturned.item;
   1.897 -        }
   1.898 -
   1.899 -        public boolean hasPrevious() {
   1.900 -            return nextIndex > 0;
   1.901 -        }
   1.902 -
   1.903 -        public E previous() {
   1.904 -            checkForComodification();
   1.905 -            if (!hasPrevious())
   1.906 -                throw new NoSuchElementException();
   1.907 -
   1.908 -            lastReturned = next = (next == null) ? last : next.prev;
   1.909 -            nextIndex--;
   1.910 -            return lastReturned.item;
   1.911 -        }
   1.912 -
   1.913 -        public int nextIndex() {
   1.914 -            return nextIndex;
   1.915 -        }
   1.916 -
   1.917 -        public int previousIndex() {
   1.918 -            return nextIndex - 1;
   1.919 -        }
   1.920 -
   1.921 -        public void remove() {
   1.922 -            checkForComodification();
   1.923 -            if (lastReturned == null)
   1.924 -                throw new IllegalStateException();
   1.925 -
   1.926 -            Node<E> lastNext = lastReturned.next;
   1.927 -            unlink(lastReturned);
   1.928 -            if (next == lastReturned)
   1.929 -                next = lastNext;
   1.930 -            else
   1.931 -                nextIndex--;
   1.932 -            lastReturned = null;
   1.933 -            expectedModCount++;
   1.934 -        }
   1.935 -
   1.936 -        public void set(E e) {
   1.937 -            if (lastReturned == null)
   1.938 -                throw new IllegalStateException();
   1.939 -            checkForComodification();
   1.940 -            lastReturned.item = e;
   1.941 -        }
   1.942 -
   1.943 -        public void add(E e) {
   1.944 -            checkForComodification();
   1.945 -            lastReturned = null;
   1.946 -            if (next == null)
   1.947 -                linkLast(e);
   1.948 -            else
   1.949 -                linkBefore(e, next);
   1.950 -            nextIndex++;
   1.951 -            expectedModCount++;
   1.952 -        }
   1.953 -
   1.954 -        final void checkForComodification() {
   1.955 -            if (modCount != expectedModCount)
   1.956 -                throw new ConcurrentModificationException();
   1.957 -        }
   1.958 -    }
   1.959 -
   1.960 -    private static class Node<E> {
   1.961 -        E item;
   1.962 -        Node<E> next;
   1.963 -        Node<E> prev;
   1.964 -
   1.965 -        Node(Node<E> prev, E element, Node<E> next) {
   1.966 -            this.item = element;
   1.967 -            this.next = next;
   1.968 -            this.prev = prev;
   1.969 -        }
   1.970 -    }
   1.971 -
   1.972 -    /**
   1.973 -     * @since 1.6
   1.974 -     */
   1.975 -    public Iterator<E> descendingIterator() {
   1.976 -        return new DescendingIterator();
   1.977 -    }
   1.978 -
   1.979 -    /**
   1.980 -     * Adapter to provide descending iterators via ListItr.previous
   1.981 -     */
   1.982 -    private class DescendingIterator implements Iterator<E> {
   1.983 -        private final ListItr itr = new ListItr(size());
   1.984 -        public boolean hasNext() {
   1.985 -            return itr.hasPrevious();
   1.986 -        }
   1.987 -        public E next() {
   1.988 -            return itr.previous();
   1.989 -        }
   1.990 -        public void remove() {
   1.991 -            itr.remove();
   1.992 -        }
   1.993 -    }
   1.994 -
   1.995 -    @SuppressWarnings("unchecked")
   1.996 -    private LinkedList<E> superClone() {
   1.997 -        try {
   1.998 -            return (LinkedList<E>) super.clone();
   1.999 -        } catch (CloneNotSupportedException e) {
  1.1000 -            throw new InternalError();
  1.1001 -        }
  1.1002 -    }
  1.1003 -
  1.1004 -    /**
  1.1005 -     * Returns a shallow copy of this {@code LinkedList}. (The elements
  1.1006 -     * themselves are not cloned.)
  1.1007 -     *
  1.1008 -     * @return a shallow copy of this {@code LinkedList} instance
  1.1009 -     */
  1.1010 -    public Object clone() {
  1.1011 -        LinkedList<E> clone = superClone();
  1.1012 -
  1.1013 -        // Put clone into "virgin" state
  1.1014 -        clone.first = clone.last = null;
  1.1015 -        clone.size = 0;
  1.1016 -        clone.modCount = 0;
  1.1017 -
  1.1018 -        // Initialize clone with our elements
  1.1019 -        for (Node<E> x = first; x != null; x = x.next)
  1.1020 -            clone.add(x.item);
  1.1021 -
  1.1022 -        return clone;
  1.1023 -    }
  1.1024 -
  1.1025 -    /**
  1.1026 -     * Returns an array containing all of the elements in this list
  1.1027 -     * in proper sequence (from first to last element).
  1.1028 -     *
  1.1029 -     * <p>The returned array will be "safe" in that no references to it are
  1.1030 -     * maintained by this list.  (In other words, this method must allocate
  1.1031 -     * a new array).  The caller is thus free to modify the returned array.
  1.1032 -     *
  1.1033 -     * <p>This method acts as bridge between array-based and collection-based
  1.1034 -     * APIs.
  1.1035 -     *
  1.1036 -     * @return an array containing all of the elements in this list
  1.1037 -     *         in proper sequence
  1.1038 -     */
  1.1039 -    public Object[] toArray() {
  1.1040 -        Object[] result = new Object[size];
  1.1041 -        int i = 0;
  1.1042 -        for (Node<E> x = first; x != null; x = x.next)
  1.1043 -            result[i++] = x.item;
  1.1044 -        return result;
  1.1045 -    }
  1.1046 -
  1.1047 -    /**
  1.1048 -     * Returns an array containing all of the elements in this list in
  1.1049 -     * proper sequence (from first to last element); the runtime type of
  1.1050 -     * the returned array is that of the specified array.  If the list fits
  1.1051 -     * in the specified array, it is returned therein.  Otherwise, a new
  1.1052 -     * array is allocated with the runtime type of the specified array and
  1.1053 -     * the size of this list.
  1.1054 -     *
  1.1055 -     * <p>If the list fits in the specified array with room to spare (i.e.,
  1.1056 -     * the array has more elements than the list), the element in the array
  1.1057 -     * immediately following the end of the list is set to {@code null}.
  1.1058 -     * (This is useful in determining the length of the list <i>only</i> if
  1.1059 -     * the caller knows that the list does not contain any null elements.)
  1.1060 -     *
  1.1061 -     * <p>Like the {@link #toArray()} method, this method acts as bridge between
  1.1062 -     * array-based and collection-based APIs.  Further, this method allows
  1.1063 -     * precise control over the runtime type of the output array, and may,
  1.1064 -     * under certain circumstances, be used to save allocation costs.
  1.1065 -     *
  1.1066 -     * <p>Suppose {@code x} is a list known to contain only strings.
  1.1067 -     * The following code can be used to dump the list into a newly
  1.1068 -     * allocated array of {@code String}:
  1.1069 -     *
  1.1070 -     * <pre>
  1.1071 -     *     String[] y = x.toArray(new String[0]);</pre>
  1.1072 -     *
  1.1073 -     * Note that {@code toArray(new Object[0])} is identical in function to
  1.1074 -     * {@code toArray()}.
  1.1075 -     *
  1.1076 -     * @param a the array into which the elements of the list are to
  1.1077 -     *          be stored, if it is big enough; otherwise, a new array of the
  1.1078 -     *          same runtime type is allocated for this purpose.
  1.1079 -     * @return an array containing the elements of the list
  1.1080 -     * @throws ArrayStoreException if the runtime type of the specified array
  1.1081 -     *         is not a supertype of the runtime type of every element in
  1.1082 -     *         this list
  1.1083 -     * @throws NullPointerException if the specified array is null
  1.1084 -     */
  1.1085 -    @SuppressWarnings("unchecked")
  1.1086 -    public <T> T[] toArray(T[] a) {
  1.1087 -        if (a.length < size)
  1.1088 -            a = (T[])java.lang.reflect.Array.newInstance(
  1.1089 -                                a.getClass().getComponentType(), size);
  1.1090 -        int i = 0;
  1.1091 -        Object[] result = a;
  1.1092 -        for (Node<E> x = first; x != null; x = x.next)
  1.1093 -            result[i++] = x.item;
  1.1094 -
  1.1095 -        if (a.length > size)
  1.1096 -            a[size] = null;
  1.1097 -
  1.1098 -        return a;
  1.1099 -    }
  1.1100 -
  1.1101 -    private static final long serialVersionUID = 876323262645176354L;
  1.1102 -
  1.1103 -}