jdk/src/share/classes/java/util/LinkedList.java
author sherman
Wed, 04 Mar 2009 09:26:41 -0800
changeset 2178 5a730fff4d0d
parent 2 90ce3da70b43
child 4184 66ef929d58f2
permissions -rw-r--r--
6812879: Excess code line in ArrayList method Summary: Removed the line of "oldData" which is no longer used. Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Linked list implementation of the <tt>List</tt> interface.  Implements all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * optional list operations, and permits all elements (including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <tt>null</tt>).  In addition to implementing the <tt>List</tt> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the <tt>LinkedList</tt> class provides uniformly named methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <tt>get</tt>, <tt>remove</tt> and <tt>insert</tt> an element at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * beginning and end of the list.  These operations allow linked lists to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * double-ended queue}. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The class implements the <tt>Deque</tt> interface, providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * first-in-first-out queue operations for <tt>add</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <tt>poll</tt>, along with other stack and deque operations.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * All of the operations perform as could be expected for a doubly-linked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * list.  Operations that index into the list will traverse the list from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the beginning or the end, whichever is closer to the specified index.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * If multiple threads access a linked list concurrently, and at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * one of the threads modifies the list structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that adds or deletes one or more elements; merely setting the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * an element is not a structural modification.)  This is typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * accomplished by synchronizing on some object that naturally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * encapsulates the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * If no such object exists, the list should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link Collections#synchronizedList Collections.synchronizedList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * unsynchronized access to the list:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   List list = Collections.synchronizedList(new LinkedList(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>The iterators returned by this class's <tt>iterator</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * structurally modified at any time after the iterator is created, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * any way except through the Iterator's own <tt>remove</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <tt>add</tt> methods, the iterator will throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * modification, the iterator fails quickly and cleanly, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see     List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see     ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see     Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public class LinkedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    extends AbstractSequentialList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private transient Entry<E> header = new Entry<E>(null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private transient int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Constructs an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public LinkedList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        header.next = header.previous = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param  c the collection whose elements are to be placed into this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public LinkedList(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns the first element in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the first element in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public E getFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return header.next.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Returns the last element in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return the last element in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public E getLast()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return header.previous.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Removes and returns the first element from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the first element from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public E removeFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return remove(header.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Removes and returns the last element from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the last element from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public E removeLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return remove(header.previous);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Inserts the specified element at the beginning of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void addFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        addBefore(e, header.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>This method is equivalent to {@link #add}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void addLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        addBefore(e, header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns <tt>true</tt> if this list contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * More formally, returns <tt>true</tt> if and only if this list contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * at least one element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param o element whose presence in this list is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return <tt>true</tt> if this list contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return indexOf(o) != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param e element to be appended to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        addBefore(e, header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * if it is present.  If this list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * unchanged.  More formally, removes the element with the lowest index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * (if such an element exists).  Returns <tt>true</tt> if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param o element to be removed from this list, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return <tt>true</tt> if this list contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            for (Entry<E> e = header.next; e != header; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (e.element==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    remove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            for (Entry<E> e = header.next; e != header; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (o.equals(e.element)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    remove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Appends all of the elements in the specified collection to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * collection's iterator.  The behavior of this operation is undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the specified collection is modified while the operation is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * progress.  (Note that this will occur if the specified collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * this list, and it's nonempty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return addAll(size, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * in the list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *              from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param c collection containing elements to be added to this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return <tt>true</tt> if this list changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (index < 0 || index > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                                ", Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int numNew = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (numNew==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        Entry<E> successor = (index==size ? header : entry(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        Entry<E> predecessor = successor.previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        for (int i=0; i<numNew; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            predecessor.next = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            predecessor = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        successor.previous = predecessor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        size += numNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Removes all of the elements from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Entry<E> e = header.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        while (e != header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Entry<E> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            e.next = e.previous = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            e.element = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        header.next = header.previous = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param index index of the element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the element at the specified position in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return entry(index).element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Replaces the element at the specified position in this list with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param index index of the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param element element to be stored at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        Entry<E> e = entry(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        E oldVal = e.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        e.element = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return oldVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Inserts the specified element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Shifts the element currently at that position (if any) and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param index index at which the specified element is to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param element element to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        addBefore(element, (index==size ? header : entry(index)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Removes the element at the specified position in this list.  Shifts any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * subsequent elements to the left (subtracts one from their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param index the index of the element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return remove(entry(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns the indexed entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private Entry<E> entry(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (index < 0 || index >= size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                                ", Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        Entry<E> e = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (index < (size >> 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            for (int i = 0; i <= index; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                e = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            for (int i = size; i > index; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                e = e.previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    // Search Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Returns the index of the first occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * in this list, or -1 if this list does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * More formally, returns the lowest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *         this list, or -1 if this list does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            for (Entry e = header.next; e != header; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                if (e.element==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            for (Entry e = header.next; e != header; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (o.equals(e.element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Returns the index of the last occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * in this list, or -1 if this list does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * More formally, returns the highest index <tt>i</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         this list, or -1 if this list does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        int index = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            for (Entry e = header.previous; e != header; e = e.previous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if (e.element==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            for (Entry e = header.previous; e != header; e = e.previous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (o.equals(e.element))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    // Queue operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Retrieves, but does not remove, the head (first element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @return the head of this list, or <tt>null</tt> if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Retrieves, but does not remove, the head (first element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return the head of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Retrieves and removes the head (first element) of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @return the head of this list, or <tt>null</tt> if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Retrieves and removes the head (first element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @return the head of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Adds the specified element as the tail (last element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return <tt>true</tt> (as specified by {@link Queue#offer})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // Deque operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Inserts the specified element at the front of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param e the element to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Inserts the specified element at the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param e the element to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Retrieves, but does not remove, the first element of this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * or returns <tt>null</tt> if this list is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the first element of this list, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *         if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public E peekFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Retrieves, but does not remove, the last element of this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * or returns <tt>null</tt> if this list is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @return the last element of this list, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *         if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public E peekLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Retrieves and removes the first element of this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * or returns <tt>null</tt> if this list is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return the first element of this list, or <tt>null</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *     this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Retrieves and removes the last element of this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * or returns <tt>null</tt> if this list is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return the last element of this list, or <tt>null</tt> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *     this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (size==0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return removeLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Pushes an element onto the stack represented by this list.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * words, inserts the element at the front of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Pops an element from the stack represented by this list.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * words, removes and returns the first element of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @return the element at the front of this list (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *         of the stack represented by this list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * list (when traversing the list from head to tail).  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param o element to be removed from this list, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @return <tt>true</tt> if the list contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public boolean removeFirstOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * list (when traversing the list from head to tail).  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param o element to be removed from this list, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return <tt>true</tt> if the list contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public boolean removeLastOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (o==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            for (Entry<E> e = header.previous; e != header; e = e.previous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if (e.element==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    remove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            for (Entry<E> e = header.previous; e != header; e = e.previous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                if (o.equals(e.element)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    remove(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Returns a list-iterator of the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * sequence), starting at the specified position in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * The list-iterator is <i>fail-fast</i>: if the list is structurally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * modified at any time after the Iterator is created, in any way except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * methods, the list-iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * concurrent modification, the iterator fails quickly and cleanly, rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * than risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param index index of the first element to be returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *              list-iterator (by a call to <tt>next</tt>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return a ListIterator of the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *         sequence), starting at the specified position in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @see List#listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private class ListItr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        private Entry<E> lastReturned = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        private Entry<E> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        private int nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        private int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (index < 0 || index > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                throw new IndexOutOfBoundsException("Index: "+index+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                                    ", Size: "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (index < (size >> 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                next = header.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                for (nextIndex=0; nextIndex<index; nextIndex++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    next = next.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                next = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                for (nextIndex=size; nextIndex>index; nextIndex--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    next = next.previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return nextIndex != size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (nextIndex == size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            lastReturned = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            next = next.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            nextIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            return lastReturned.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return nextIndex != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            if (nextIndex == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            lastReturned = next = next.previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            nextIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            return lastReturned.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return nextIndex-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            Entry<E> lastNext = lastReturned.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                LinkedList.this.remove(lastReturned);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } catch (NoSuchElementException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (next==lastReturned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                next = lastNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                nextIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            lastReturned = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (lastReturned == header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            lastReturned.element = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            lastReturned = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            addBefore(e, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            nextIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    private static class Entry<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        E element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        Entry<E> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        Entry<E> previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        Entry(E element, Entry<E> next, Entry<E> previous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            this.element = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            this.previous = previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private Entry<E> addBefore(E e, Entry<E> entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        newEntry.previous.next = newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        newEntry.next.previous = newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    private E remove(Entry<E> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (e == header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        E result = e.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        e.previous.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        e.next.previous = e.previous;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        e.next = e.previous = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        e.element = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /** Adapter to provide descending iterators via ListItr.previous */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    private class DescendingIterator implements Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        final ListItr itr = new ListItr(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return itr.hasPrevious();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return itr.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            itr.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * Returns a shallow copy of this <tt>LinkedList</tt>. (The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @return a shallow copy of this <tt>LinkedList</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        LinkedList<E> clone = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            clone = (LinkedList<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        // Put clone into "virgin" state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        clone.header = new Entry<E>(null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        clone.header.next = clone.header.previous = clone.header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        clone.size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        clone.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // Initialize clone with our elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        for (Entry<E> e = header.next; e != header; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            clone.add(e.element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @return an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *         in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        Object[] result = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        for (Entry<E> e = header.next; e != header; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            result[i++] = e.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * <p>If the list fits in the specified array with room to spare (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * the array has more elements than the list), the element in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * immediately following the end of the list is set to <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * (This is useful in determining the length of the list <i>only</i> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * the caller knows that the list does not contain any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <p>Suppose <tt>x</tt> is a list known to contain only strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * The following code can be used to dump the list into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * allocated array of <tt>String</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return an array containing the elements of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            a = (T[])java.lang.reflect.Array.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                                a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        Object[] result = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        for (Entry<E> e = header.next; e != header; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            result[i++] = e.element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    private static final long serialVersionUID = 876323262645176354L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Save the state of this <tt>LinkedList</tt> instance to a stream (that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * is, serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @serialData The size of the list (the number of elements it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *             contains) is emitted (int), followed by all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *             elements (each an Object) in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        // Write out any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // Write out all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        for (Entry e = header.next; e != header; e = e.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            s.writeObject(e.element);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Reconstitute this <tt>LinkedList</tt> instance from a stream (that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        // Read in any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        // Read in size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        // Initialize header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        header = new Entry<E>(null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        header.next = header.previous = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            addBefore((E)s.readObject(), header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
}