rt/emul/compact/src/main/java/java/util/AbstractList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 557 emul/compact/src/main/java/java/util/AbstractList.java@5be31d9fa455
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.util;
jaroslav@557
    27
jaroslav@557
    28
/**
jaroslav@557
    29
 * This class provides a skeletal implementation of the {@link List}
jaroslav@557
    30
 * interface to minimize the effort required to implement this interface
jaroslav@557
    31
 * backed by a "random access" data store (such as an array).  For sequential
jaroslav@557
    32
 * access data (such as a linked list), {@link AbstractSequentialList} should
jaroslav@557
    33
 * be used in preference to this class.
jaroslav@557
    34
 *
jaroslav@557
    35
 * <p>To implement an unmodifiable list, the programmer needs only to extend
jaroslav@557
    36
 * this class and provide implementations for the {@link #get(int)} and
jaroslav@557
    37
 * {@link List#size() size()} methods.
jaroslav@557
    38
 *
jaroslav@557
    39
 * <p>To implement a modifiable list, the programmer must additionally
jaroslav@557
    40
 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
jaroslav@557
    41
 * throws an {@code UnsupportedOperationException}).  If the list is
jaroslav@557
    42
 * variable-size the programmer must additionally override the
jaroslav@557
    43
 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
jaroslav@557
    44
 *
jaroslav@557
    45
 * <p>The programmer should generally provide a void (no argument) and collection
jaroslav@557
    46
 * constructor, as per the recommendation in the {@link Collection} interface
jaroslav@557
    47
 * specification.
jaroslav@557
    48
 *
jaroslav@557
    49
 * <p>Unlike the other abstract collection implementations, the programmer does
jaroslav@557
    50
 * <i>not</i> have to provide an iterator implementation; the iterator and
jaroslav@557
    51
 * list iterator are implemented by this class, on top of the "random access"
jaroslav@557
    52
 * methods:
jaroslav@557
    53
 * {@link #get(int)},
jaroslav@557
    54
 * {@link #set(int, Object) set(int, E)},
jaroslav@557
    55
 * {@link #add(int, Object) add(int, E)} and
jaroslav@557
    56
 * {@link #remove(int)}.
jaroslav@557
    57
 *
jaroslav@557
    58
 * <p>The documentation for each non-abstract method in this class describes its
jaroslav@557
    59
 * implementation in detail.  Each of these methods may be overridden if the
jaroslav@557
    60
 * collection being implemented admits a more efficient implementation.
jaroslav@557
    61
 *
jaroslav@557
    62
 * <p>This class is a member of the
jaroslav@557
    63
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@557
    64
 * Java Collections Framework</a>.
jaroslav@557
    65
 *
jaroslav@557
    66
 * @author  Josh Bloch
jaroslav@557
    67
 * @author  Neal Gafter
jaroslav@557
    68
 * @since 1.2
jaroslav@557
    69
 */
jaroslav@557
    70
jaroslav@557
    71
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
jaroslav@557
    72
    /**
jaroslav@557
    73
     * Sole constructor.  (For invocation by subclass constructors, typically
jaroslav@557
    74
     * implicit.)
jaroslav@557
    75
     */
jaroslav@557
    76
    protected AbstractList() {
jaroslav@557
    77
    }
jaroslav@557
    78
jaroslav@557
    79
    /**
jaroslav@557
    80
     * Appends the specified element to the end of this list (optional
jaroslav@557
    81
     * operation).
jaroslav@557
    82
     *
jaroslav@557
    83
     * <p>Lists that support this operation may place limitations on what
jaroslav@557
    84
     * elements may be added to this list.  In particular, some
jaroslav@557
    85
     * lists will refuse to add null elements, and others will impose
jaroslav@557
    86
     * restrictions on the type of elements that may be added.  List
jaroslav@557
    87
     * classes should clearly specify in their documentation any restrictions
jaroslav@557
    88
     * on what elements may be added.
jaroslav@557
    89
     *
jaroslav@557
    90
     * <p>This implementation calls {@code add(size(), e)}.
jaroslav@557
    91
     *
jaroslav@557
    92
     * <p>Note that this implementation throws an
jaroslav@557
    93
     * {@code UnsupportedOperationException} unless
jaroslav@557
    94
     * {@link #add(int, Object) add(int, E)} is overridden.
jaroslav@557
    95
     *
jaroslav@557
    96
     * @param e element to be appended to this list
jaroslav@557
    97
     * @return {@code true} (as specified by {@link Collection#add})
jaroslav@557
    98
     * @throws UnsupportedOperationException if the {@code add} operation
jaroslav@557
    99
     *         is not supported by this list
jaroslav@557
   100
     * @throws ClassCastException if the class of the specified element
jaroslav@557
   101
     *         prevents it from being added to this list
jaroslav@557
   102
     * @throws NullPointerException if the specified element is null and this
jaroslav@557
   103
     *         list does not permit null elements
jaroslav@557
   104
     * @throws IllegalArgumentException if some property of this element
jaroslav@557
   105
     *         prevents it from being added to this list
jaroslav@557
   106
     */
jaroslav@557
   107
    public boolean add(E e) {
jaroslav@557
   108
        add(size(), e);
jaroslav@557
   109
        return true;
jaroslav@557
   110
    }
jaroslav@557
   111
jaroslav@557
   112
    /**
jaroslav@557
   113
     * {@inheritDoc}
jaroslav@557
   114
     *
jaroslav@557
   115
     * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@557
   116
     */
jaroslav@557
   117
    abstract public E get(int index);
jaroslav@557
   118
jaroslav@557
   119
    /**
jaroslav@557
   120
     * {@inheritDoc}
jaroslav@557
   121
     *
jaroslav@557
   122
     * <p>This implementation always throws an
jaroslav@557
   123
     * {@code UnsupportedOperationException}.
jaroslav@557
   124
     *
jaroslav@557
   125
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   126
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   127
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   128
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@557
   129
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@557
   130
     */
jaroslav@557
   131
    public E set(int index, E element) {
jaroslav@557
   132
        throw new UnsupportedOperationException();
jaroslav@557
   133
    }
jaroslav@557
   134
jaroslav@557
   135
    /**
jaroslav@557
   136
     * {@inheritDoc}
jaroslav@557
   137
     *
jaroslav@557
   138
     * <p>This implementation always throws an
jaroslav@557
   139
     * {@code UnsupportedOperationException}.
jaroslav@557
   140
     *
jaroslav@557
   141
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   142
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   143
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   144
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@557
   145
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@557
   146
     */
jaroslav@557
   147
    public void add(int index, E element) {
jaroslav@557
   148
        throw new UnsupportedOperationException();
jaroslav@557
   149
    }
jaroslav@557
   150
jaroslav@557
   151
    /**
jaroslav@557
   152
     * {@inheritDoc}
jaroslav@557
   153
     *
jaroslav@557
   154
     * <p>This implementation always throws an
jaroslav@557
   155
     * {@code UnsupportedOperationException}.
jaroslav@557
   156
     *
jaroslav@557
   157
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   158
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@557
   159
     */
jaroslav@557
   160
    public E remove(int index) {
jaroslav@557
   161
        throw new UnsupportedOperationException();
jaroslav@557
   162
    }
jaroslav@557
   163
jaroslav@557
   164
jaroslav@557
   165
    // Search Operations
jaroslav@557
   166
jaroslav@557
   167
    /**
jaroslav@557
   168
     * {@inheritDoc}
jaroslav@557
   169
     *
jaroslav@557
   170
     * <p>This implementation first gets a list iterator (with
jaroslav@557
   171
     * {@code listIterator()}).  Then, it iterates over the list until the
jaroslav@557
   172
     * specified element is found or the end of the list is reached.
jaroslav@557
   173
     *
jaroslav@557
   174
     * @throws ClassCastException   {@inheritDoc}
jaroslav@557
   175
     * @throws NullPointerException {@inheritDoc}
jaroslav@557
   176
     */
jaroslav@557
   177
    public int indexOf(Object o) {
jaroslav@557
   178
        ListIterator<E> it = listIterator();
jaroslav@557
   179
        if (o==null) {
jaroslav@557
   180
            while (it.hasNext())
jaroslav@557
   181
                if (it.next()==null)
jaroslav@557
   182
                    return it.previousIndex();
jaroslav@557
   183
        } else {
jaroslav@557
   184
            while (it.hasNext())
jaroslav@557
   185
                if (o.equals(it.next()))
jaroslav@557
   186
                    return it.previousIndex();
jaroslav@557
   187
        }
jaroslav@557
   188
        return -1;
jaroslav@557
   189
    }
jaroslav@557
   190
jaroslav@557
   191
    /**
jaroslav@557
   192
     * {@inheritDoc}
jaroslav@557
   193
     *
jaroslav@557
   194
     * <p>This implementation first gets a list iterator that points to the end
jaroslav@557
   195
     * of the list (with {@code listIterator(size())}).  Then, it iterates
jaroslav@557
   196
     * backwards over the list until the specified element is found, or the
jaroslav@557
   197
     * beginning of the list is reached.
jaroslav@557
   198
     *
jaroslav@557
   199
     * @throws ClassCastException   {@inheritDoc}
jaroslav@557
   200
     * @throws NullPointerException {@inheritDoc}
jaroslav@557
   201
     */
jaroslav@557
   202
    public int lastIndexOf(Object o) {
jaroslav@557
   203
        ListIterator<E> it = listIterator(size());
jaroslav@557
   204
        if (o==null) {
jaroslav@557
   205
            while (it.hasPrevious())
jaroslav@557
   206
                if (it.previous()==null)
jaroslav@557
   207
                    return it.nextIndex();
jaroslav@557
   208
        } else {
jaroslav@557
   209
            while (it.hasPrevious())
jaroslav@557
   210
                if (o.equals(it.previous()))
jaroslav@557
   211
                    return it.nextIndex();
jaroslav@557
   212
        }
jaroslav@557
   213
        return -1;
jaroslav@557
   214
    }
jaroslav@557
   215
jaroslav@557
   216
jaroslav@557
   217
    // Bulk Operations
jaroslav@557
   218
jaroslav@557
   219
    /**
jaroslav@557
   220
     * Removes all of the elements from this list (optional operation).
jaroslav@557
   221
     * The list will be empty after this call returns.
jaroslav@557
   222
     *
jaroslav@557
   223
     * <p>This implementation calls {@code removeRange(0, size())}.
jaroslav@557
   224
     *
jaroslav@557
   225
     * <p>Note that this implementation throws an
jaroslav@557
   226
     * {@code UnsupportedOperationException} unless {@code remove(int
jaroslav@557
   227
     * index)} or {@code removeRange(int fromIndex, int toIndex)} is
jaroslav@557
   228
     * overridden.
jaroslav@557
   229
     *
jaroslav@557
   230
     * @throws UnsupportedOperationException if the {@code clear} operation
jaroslav@557
   231
     *         is not supported by this list
jaroslav@557
   232
     */
jaroslav@557
   233
    public void clear() {
jaroslav@557
   234
        removeRange(0, size());
jaroslav@557
   235
    }
jaroslav@557
   236
jaroslav@557
   237
    /**
jaroslav@557
   238
     * {@inheritDoc}
jaroslav@557
   239
     *
jaroslav@557
   240
     * <p>This implementation gets an iterator over the specified collection
jaroslav@557
   241
     * and iterates over it, inserting the elements obtained from the
jaroslav@557
   242
     * iterator into this list at the appropriate position, one at a time,
jaroslav@557
   243
     * using {@code add(int, E)}.
jaroslav@557
   244
     * Many implementations will override this method for efficiency.
jaroslav@557
   245
     *
jaroslav@557
   246
     * <p>Note that this implementation throws an
jaroslav@557
   247
     * {@code UnsupportedOperationException} unless
jaroslav@557
   248
     * {@link #add(int, Object) add(int, E)} is overridden.
jaroslav@557
   249
     *
jaroslav@557
   250
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   251
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   252
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   253
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@557
   254
     * @throws IndexOutOfBoundsException     {@inheritDoc}
jaroslav@557
   255
     */
jaroslav@557
   256
    public boolean addAll(int index, Collection<? extends E> c) {
jaroslav@557
   257
        rangeCheckForAdd(index);
jaroslav@557
   258
        boolean modified = false;
jaroslav@557
   259
        for (E e : c) {
jaroslav@557
   260
            add(index++, e);
jaroslav@557
   261
            modified = true;
jaroslav@557
   262
        }
jaroslav@557
   263
        return modified;
jaroslav@557
   264
    }
jaroslav@557
   265
jaroslav@557
   266
jaroslav@557
   267
    // Iterators
jaroslav@557
   268
jaroslav@557
   269
    /**
jaroslav@557
   270
     * Returns an iterator over the elements in this list in proper sequence.
jaroslav@557
   271
     *
jaroslav@557
   272
     * <p>This implementation returns a straightforward implementation of the
jaroslav@557
   273
     * iterator interface, relying on the backing list's {@code size()},
jaroslav@557
   274
     * {@code get(int)}, and {@code remove(int)} methods.
jaroslav@557
   275
     *
jaroslav@557
   276
     * <p>Note that the iterator returned by this method will throw an
jaroslav@557
   277
     * {@link UnsupportedOperationException} in response to its
jaroslav@557
   278
     * {@code remove} method unless the list's {@code remove(int)} method is
jaroslav@557
   279
     * overridden.
jaroslav@557
   280
     *
jaroslav@557
   281
     * <p>This implementation can be made to throw runtime exceptions in the
jaroslav@557
   282
     * face of concurrent modification, as described in the specification
jaroslav@557
   283
     * for the (protected) {@link #modCount} field.
jaroslav@557
   284
     *
jaroslav@557
   285
     * @return an iterator over the elements in this list in proper sequence
jaroslav@557
   286
     */
jaroslav@557
   287
    public Iterator<E> iterator() {
jaroslav@557
   288
        return new Itr();
jaroslav@557
   289
    }
jaroslav@557
   290
jaroslav@557
   291
    /**
jaroslav@557
   292
     * {@inheritDoc}
jaroslav@557
   293
     *
jaroslav@557
   294
     * <p>This implementation returns {@code listIterator(0)}.
jaroslav@557
   295
     *
jaroslav@557
   296
     * @see #listIterator(int)
jaroslav@557
   297
     */
jaroslav@557
   298
    public ListIterator<E> listIterator() {
jaroslav@557
   299
        return listIterator(0);
jaroslav@557
   300
    }
jaroslav@557
   301
jaroslav@557
   302
    /**
jaroslav@557
   303
     * {@inheritDoc}
jaroslav@557
   304
     *
jaroslav@557
   305
     * <p>This implementation returns a straightforward implementation of the
jaroslav@557
   306
     * {@code ListIterator} interface that extends the implementation of the
jaroslav@557
   307
     * {@code Iterator} interface returned by the {@code iterator()} method.
jaroslav@557
   308
     * The {@code ListIterator} implementation relies on the backing list's
jaroslav@557
   309
     * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
jaroslav@557
   310
     * and {@code remove(int)} methods.
jaroslav@557
   311
     *
jaroslav@557
   312
     * <p>Note that the list iterator returned by this implementation will
jaroslav@557
   313
     * throw an {@link UnsupportedOperationException} in response to its
jaroslav@557
   314
     * {@code remove}, {@code set} and {@code add} methods unless the
jaroslav@557
   315
     * list's {@code remove(int)}, {@code set(int, E)}, and
jaroslav@557
   316
     * {@code add(int, E)} methods are overridden.
jaroslav@557
   317
     *
jaroslav@557
   318
     * <p>This implementation can be made to throw runtime exceptions in the
jaroslav@557
   319
     * face of concurrent modification, as described in the specification for
jaroslav@557
   320
     * the (protected) {@link #modCount} field.
jaroslav@557
   321
     *
jaroslav@557
   322
     * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@557
   323
     */
jaroslav@557
   324
    public ListIterator<E> listIterator(final int index) {
jaroslav@557
   325
        rangeCheckForAdd(index);
jaroslav@557
   326
jaroslav@557
   327
        return new ListItr(index);
jaroslav@557
   328
    }
jaroslav@557
   329
jaroslav@557
   330
    private class Itr implements Iterator<E> {
jaroslav@557
   331
        /**
jaroslav@557
   332
         * Index of element to be returned by subsequent call to next.
jaroslav@557
   333
         */
jaroslav@557
   334
        int cursor = 0;
jaroslav@557
   335
jaroslav@557
   336
        /**
jaroslav@557
   337
         * Index of element returned by most recent call to next or
jaroslav@557
   338
         * previous.  Reset to -1 if this element is deleted by a call
jaroslav@557
   339
         * to remove.
jaroslav@557
   340
         */
jaroslav@557
   341
        int lastRet = -1;
jaroslav@557
   342
jaroslav@557
   343
        /**
jaroslav@557
   344
         * The modCount value that the iterator believes that the backing
jaroslav@557
   345
         * List should have.  If this expectation is violated, the iterator
jaroslav@557
   346
         * has detected concurrent modification.
jaroslav@557
   347
         */
jaroslav@557
   348
        int expectedModCount = modCount;
jaroslav@557
   349
jaroslav@557
   350
        public boolean hasNext() {
jaroslav@557
   351
            return cursor != size();
jaroslav@557
   352
        }
jaroslav@557
   353
jaroslav@557
   354
        public E next() {
jaroslav@557
   355
            checkForComodification();
jaroslav@557
   356
            try {
jaroslav@557
   357
                int i = cursor;
jaroslav@557
   358
                E next = get(i);
jaroslav@557
   359
                lastRet = i;
jaroslav@557
   360
                cursor = i + 1;
jaroslav@557
   361
                return next;
jaroslav@557
   362
            } catch (IndexOutOfBoundsException e) {
jaroslav@557
   363
                checkForComodification();
jaroslav@557
   364
                throw new NoSuchElementException();
jaroslav@557
   365
            }
jaroslav@557
   366
        }
jaroslav@557
   367
jaroslav@557
   368
        public void remove() {
jaroslav@557
   369
            if (lastRet < 0)
jaroslav@557
   370
                throw new IllegalStateException();
jaroslav@557
   371
            checkForComodification();
jaroslav@557
   372
jaroslav@557
   373
            try {
jaroslav@557
   374
                AbstractList.this.remove(lastRet);
jaroslav@557
   375
                if (lastRet < cursor)
jaroslav@557
   376
                    cursor--;
jaroslav@557
   377
                lastRet = -1;
jaroslav@557
   378
                expectedModCount = modCount;
jaroslav@557
   379
            } catch (IndexOutOfBoundsException e) {
jaroslav@557
   380
                throw new ConcurrentModificationException();
jaroslav@557
   381
            }
jaroslav@557
   382
        }
jaroslav@557
   383
jaroslav@557
   384
        final void checkForComodification() {
jaroslav@557
   385
            if (modCount != expectedModCount)
jaroslav@557
   386
                throw new ConcurrentModificationException();
jaroslav@557
   387
        }
jaroslav@557
   388
    }
jaroslav@557
   389
jaroslav@557
   390
    private class ListItr extends Itr implements ListIterator<E> {
jaroslav@557
   391
        ListItr(int index) {
jaroslav@557
   392
            cursor = index;
jaroslav@557
   393
        }
jaroslav@557
   394
jaroslav@557
   395
        public boolean hasPrevious() {
jaroslav@557
   396
            return cursor != 0;
jaroslav@557
   397
        }
jaroslav@557
   398
jaroslav@557
   399
        public E previous() {
jaroslav@557
   400
            checkForComodification();
jaroslav@557
   401
            try {
jaroslav@557
   402
                int i = cursor - 1;
jaroslav@557
   403
                E previous = get(i);
jaroslav@557
   404
                lastRet = cursor = i;
jaroslav@557
   405
                return previous;
jaroslav@557
   406
            } catch (IndexOutOfBoundsException e) {
jaroslav@557
   407
                checkForComodification();
jaroslav@557
   408
                throw new NoSuchElementException();
jaroslav@557
   409
            }
jaroslav@557
   410
        }
jaroslav@557
   411
jaroslav@557
   412
        public int nextIndex() {
jaroslav@557
   413
            return cursor;
jaroslav@557
   414
        }
jaroslav@557
   415
jaroslav@557
   416
        public int previousIndex() {
jaroslav@557
   417
            return cursor-1;
jaroslav@557
   418
        }
jaroslav@557
   419
jaroslav@557
   420
        public void set(E e) {
jaroslav@557
   421
            if (lastRet < 0)
jaroslav@557
   422
                throw new IllegalStateException();
jaroslav@557
   423
            checkForComodification();
jaroslav@557
   424
jaroslav@557
   425
            try {
jaroslav@557
   426
                AbstractList.this.set(lastRet, e);
jaroslav@557
   427
                expectedModCount = modCount;
jaroslav@557
   428
            } catch (IndexOutOfBoundsException ex) {
jaroslav@557
   429
                throw new ConcurrentModificationException();
jaroslav@557
   430
            }
jaroslav@557
   431
        }
jaroslav@557
   432
jaroslav@557
   433
        public void add(E e) {
jaroslav@557
   434
            checkForComodification();
jaroslav@557
   435
jaroslav@557
   436
            try {
jaroslav@557
   437
                int i = cursor;
jaroslav@557
   438
                AbstractList.this.add(i, e);
jaroslav@557
   439
                lastRet = -1;
jaroslav@557
   440
                cursor = i + 1;
jaroslav@557
   441
                expectedModCount = modCount;
jaroslav@557
   442
            } catch (IndexOutOfBoundsException ex) {
jaroslav@557
   443
                throw new ConcurrentModificationException();
jaroslav@557
   444
            }
jaroslav@557
   445
        }
jaroslav@557
   446
    }
jaroslav@557
   447
jaroslav@557
   448
    /**
jaroslav@557
   449
     * {@inheritDoc}
jaroslav@557
   450
     *
jaroslav@557
   451
     * <p>This implementation returns a list that subclasses
jaroslav@557
   452
     * {@code AbstractList}.  The subclass stores, in private fields, the
jaroslav@557
   453
     * offset of the subList within the backing list, the size of the subList
jaroslav@557
   454
     * (which can change over its lifetime), and the expected
jaroslav@557
   455
     * {@code modCount} value of the backing list.  There are two variants
jaroslav@557
   456
     * of the subclass, one of which implements {@code RandomAccess}.
jaroslav@557
   457
     * If this list implements {@code RandomAccess} the returned list will
jaroslav@557
   458
     * be an instance of the subclass that implements {@code RandomAccess}.
jaroslav@557
   459
     *
jaroslav@557
   460
     * <p>The subclass's {@code set(int, E)}, {@code get(int)},
jaroslav@557
   461
     * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
jaroslav@557
   462
     * Collection)} and {@code removeRange(int, int)} methods all
jaroslav@557
   463
     * delegate to the corresponding methods on the backing abstract list,
jaroslav@557
   464
     * after bounds-checking the index and adjusting for the offset.  The
jaroslav@557
   465
     * {@code addAll(Collection c)} method merely returns {@code addAll(size,
jaroslav@557
   466
     * c)}.
jaroslav@557
   467
     *
jaroslav@557
   468
     * <p>The {@code listIterator(int)} method returns a "wrapper object"
jaroslav@557
   469
     * over a list iterator on the backing list, which is created with the
jaroslav@557
   470
     * corresponding method on the backing list.  The {@code iterator} method
jaroslav@557
   471
     * merely returns {@code listIterator()}, and the {@code size} method
jaroslav@557
   472
     * merely returns the subclass's {@code size} field.
jaroslav@557
   473
     *
jaroslav@557
   474
     * <p>All methods first check to see if the actual {@code modCount} of
jaroslav@557
   475
     * the backing list is equal to its expected value, and throw a
jaroslav@557
   476
     * {@code ConcurrentModificationException} if it is not.
jaroslav@557
   477
     *
jaroslav@557
   478
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
jaroslav@557
   479
     *         {@code (fromIndex < 0 || toIndex > size)}
jaroslav@557
   480
     * @throws IllegalArgumentException if the endpoint indices are out of order
jaroslav@557
   481
     *         {@code (fromIndex > toIndex)}
jaroslav@557
   482
     */
jaroslav@557
   483
    public List<E> subList(int fromIndex, int toIndex) {
jaroslav@557
   484
        return (this instanceof RandomAccess ?
jaroslav@557
   485
                new RandomAccessSubList<>(this, fromIndex, toIndex) :
jaroslav@557
   486
                new SubList<>(this, fromIndex, toIndex));
jaroslav@557
   487
    }
jaroslav@557
   488
jaroslav@557
   489
    // Comparison and hashing
jaroslav@557
   490
jaroslav@557
   491
    /**
jaroslav@557
   492
     * Compares the specified object with this list for equality.  Returns
jaroslav@557
   493
     * {@code true} if and only if the specified object is also a list, both
jaroslav@557
   494
     * lists have the same size, and all corresponding pairs of elements in
jaroslav@557
   495
     * the two lists are <i>equal</i>.  (Two elements {@code e1} and
jaroslav@557
   496
     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
jaroslav@557
   497
     * e1.equals(e2))}.)  In other words, two lists are defined to be
jaroslav@557
   498
     * equal if they contain the same elements in the same order.<p>
jaroslav@557
   499
     *
jaroslav@557
   500
     * This implementation first checks if the specified object is this
jaroslav@557
   501
     * list. If so, it returns {@code true}; if not, it checks if the
jaroslav@557
   502
     * specified object is a list. If not, it returns {@code false}; if so,
jaroslav@557
   503
     * it iterates over both lists, comparing corresponding pairs of elements.
jaroslav@557
   504
     * If any comparison returns {@code false}, this method returns
jaroslav@557
   505
     * {@code false}.  If either iterator runs out of elements before the
jaroslav@557
   506
     * other it returns {@code false} (as the lists are of unequal length);
jaroslav@557
   507
     * otherwise it returns {@code true} when the iterations complete.
jaroslav@557
   508
     *
jaroslav@557
   509
     * @param o the object to be compared for equality with this list
jaroslav@557
   510
     * @return {@code true} if the specified object is equal to this list
jaroslav@557
   511
     */
jaroslav@557
   512
    public boolean equals(Object o) {
jaroslav@557
   513
        if (o == this)
jaroslav@557
   514
            return true;
jaroslav@557
   515
        if (!(o instanceof List))
jaroslav@557
   516
            return false;
jaroslav@557
   517
jaroslav@557
   518
        ListIterator<E> e1 = listIterator();
jaroslav@557
   519
        ListIterator e2 = ((List) o).listIterator();
jaroslav@557
   520
        while (e1.hasNext() && e2.hasNext()) {
jaroslav@557
   521
            E o1 = e1.next();
jaroslav@557
   522
            Object o2 = e2.next();
jaroslav@557
   523
            if (!(o1==null ? o2==null : o1.equals(o2)))
jaroslav@557
   524
                return false;
jaroslav@557
   525
        }
jaroslav@557
   526
        return !(e1.hasNext() || e2.hasNext());
jaroslav@557
   527
    }
jaroslav@557
   528
jaroslav@557
   529
    /**
jaroslav@557
   530
     * Returns the hash code value for this list.
jaroslav@557
   531
     *
jaroslav@557
   532
     * <p>This implementation uses exactly the code that is used to define the
jaroslav@557
   533
     * list hash function in the documentation for the {@link List#hashCode}
jaroslav@557
   534
     * method.
jaroslav@557
   535
     *
jaroslav@557
   536
     * @return the hash code value for this list
jaroslav@557
   537
     */
jaroslav@557
   538
    public int hashCode() {
jaroslav@557
   539
        int hashCode = 1;
jaroslav@557
   540
        for (E e : this)
jaroslav@557
   541
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
jaroslav@557
   542
        return hashCode;
jaroslav@557
   543
    }
jaroslav@557
   544
jaroslav@557
   545
    /**
jaroslav@557
   546
     * Removes from this list all of the elements whose index is between
jaroslav@557
   547
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
jaroslav@557
   548
     * Shifts any succeeding elements to the left (reduces their index).
jaroslav@557
   549
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
jaroslav@557
   550
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
jaroslav@557
   551
     *
jaroslav@557
   552
     * <p>This method is called by the {@code clear} operation on this list
jaroslav@557
   553
     * and its subLists.  Overriding this method to take advantage of
jaroslav@557
   554
     * the internals of the list implementation can <i>substantially</i>
jaroslav@557
   555
     * improve the performance of the {@code clear} operation on this list
jaroslav@557
   556
     * and its subLists.
jaroslav@557
   557
     *
jaroslav@557
   558
     * <p>This implementation gets a list iterator positioned before
jaroslav@557
   559
     * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
jaroslav@557
   560
     * followed by {@code ListIterator.remove} until the entire range has
jaroslav@557
   561
     * been removed.  <b>Note: if {@code ListIterator.remove} requires linear
jaroslav@557
   562
     * time, this implementation requires quadratic time.</b>
jaroslav@557
   563
     *
jaroslav@557
   564
     * @param fromIndex index of first element to be removed
jaroslav@557
   565
     * @param toIndex index after last element to be removed
jaroslav@557
   566
     */
jaroslav@557
   567
    protected void removeRange(int fromIndex, int toIndex) {
jaroslav@557
   568
        ListIterator<E> it = listIterator(fromIndex);
jaroslav@557
   569
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
jaroslav@557
   570
            it.next();
jaroslav@557
   571
            it.remove();
jaroslav@557
   572
        }
jaroslav@557
   573
    }
jaroslav@557
   574
jaroslav@557
   575
    /**
jaroslav@557
   576
     * The number of times this list has been <i>structurally modified</i>.
jaroslav@557
   577
     * Structural modifications are those that change the size of the
jaroslav@557
   578
     * list, or otherwise perturb it in such a fashion that iterations in
jaroslav@557
   579
     * progress may yield incorrect results.
jaroslav@557
   580
     *
jaroslav@557
   581
     * <p>This field is used by the iterator and list iterator implementation
jaroslav@557
   582
     * returned by the {@code iterator} and {@code listIterator} methods.
jaroslav@557
   583
     * If the value of this field changes unexpectedly, the iterator (or list
jaroslav@557
   584
     * iterator) will throw a {@code ConcurrentModificationException} in
jaroslav@557
   585
     * response to the {@code next}, {@code remove}, {@code previous},
jaroslav@557
   586
     * {@code set} or {@code add} operations.  This provides
jaroslav@557
   587
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
jaroslav@557
   588
     * the face of concurrent modification during iteration.
jaroslav@557
   589
     *
jaroslav@557
   590
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
jaroslav@557
   591
     * wishes to provide fail-fast iterators (and list iterators), then it
jaroslav@557
   592
     * merely has to increment this field in its {@code add(int, E)} and
jaroslav@557
   593
     * {@code remove(int)} methods (and any other methods that it overrides
jaroslav@557
   594
     * that result in structural modifications to the list).  A single call to
jaroslav@557
   595
     * {@code add(int, E)} or {@code remove(int)} must add no more than
jaroslav@557
   596
     * one to this field, or the iterators (and list iterators) will throw
jaroslav@557
   597
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
jaroslav@557
   598
     * does not wish to provide fail-fast iterators, this field may be
jaroslav@557
   599
     * ignored.
jaroslav@557
   600
     */
jaroslav@557
   601
    protected transient int modCount = 0;
jaroslav@557
   602
jaroslav@557
   603
    private void rangeCheckForAdd(int index) {
jaroslav@557
   604
        if (index < 0 || index > size())
jaroslav@557
   605
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
jaroslav@557
   606
    }
jaroslav@557
   607
jaroslav@557
   608
    private String outOfBoundsMsg(int index) {
jaroslav@557
   609
        return "Index: "+index+", Size: "+size();
jaroslav@557
   610
    }
jaroslav@557
   611
}
jaroslav@557
   612
jaroslav@557
   613
class SubList<E> extends AbstractList<E> {
jaroslav@557
   614
    private final AbstractList<E> l;
jaroslav@557
   615
    private final int offset;
jaroslav@557
   616
    private int size;
jaroslav@557
   617
jaroslav@557
   618
    SubList(AbstractList<E> list, int fromIndex, int toIndex) {
jaroslav@557
   619
        if (fromIndex < 0)
jaroslav@557
   620
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
jaroslav@557
   621
        if (toIndex > list.size())
jaroslav@557
   622
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
jaroslav@557
   623
        if (fromIndex > toIndex)
jaroslav@557
   624
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
jaroslav@557
   625
                                               ") > toIndex(" + toIndex + ")");
jaroslav@557
   626
        l = list;
jaroslav@557
   627
        offset = fromIndex;
jaroslav@557
   628
        size = toIndex - fromIndex;
jaroslav@557
   629
        this.modCount = l.modCount;
jaroslav@557
   630
    }
jaroslav@557
   631
jaroslav@557
   632
    public E set(int index, E element) {
jaroslav@557
   633
        rangeCheck(index);
jaroslav@557
   634
        checkForComodification();
jaroslav@557
   635
        return l.set(index+offset, element);
jaroslav@557
   636
    }
jaroslav@557
   637
jaroslav@557
   638
    public E get(int index) {
jaroslav@557
   639
        rangeCheck(index);
jaroslav@557
   640
        checkForComodification();
jaroslav@557
   641
        return l.get(index+offset);
jaroslav@557
   642
    }
jaroslav@557
   643
jaroslav@557
   644
    public int size() {
jaroslav@557
   645
        checkForComodification();
jaroslav@557
   646
        return size;
jaroslav@557
   647
    }
jaroslav@557
   648
jaroslav@557
   649
    public void add(int index, E element) {
jaroslav@557
   650
        rangeCheckForAdd(index);
jaroslav@557
   651
        checkForComodification();
jaroslav@557
   652
        l.add(index+offset, element);
jaroslav@557
   653
        this.modCount = l.modCount;
jaroslav@557
   654
        size++;
jaroslav@557
   655
    }
jaroslav@557
   656
jaroslav@557
   657
    public E remove(int index) {
jaroslav@557
   658
        rangeCheck(index);
jaroslav@557
   659
        checkForComodification();
jaroslav@557
   660
        E result = l.remove(index+offset);
jaroslav@557
   661
        this.modCount = l.modCount;
jaroslav@557
   662
        size--;
jaroslav@557
   663
        return result;
jaroslav@557
   664
    }
jaroslav@557
   665
jaroslav@557
   666
    protected void removeRange(int fromIndex, int toIndex) {
jaroslav@557
   667
        checkForComodification();
jaroslav@557
   668
        l.removeRange(fromIndex+offset, toIndex+offset);
jaroslav@557
   669
        this.modCount = l.modCount;
jaroslav@557
   670
        size -= (toIndex-fromIndex);
jaroslav@557
   671
    }
jaroslav@557
   672
jaroslav@557
   673
    public boolean addAll(Collection<? extends E> c) {
jaroslav@557
   674
        return addAll(size, c);
jaroslav@557
   675
    }
jaroslav@557
   676
jaroslav@557
   677
    public boolean addAll(int index, Collection<? extends E> c) {
jaroslav@557
   678
        rangeCheckForAdd(index);
jaroslav@557
   679
        int cSize = c.size();
jaroslav@557
   680
        if (cSize==0)
jaroslav@557
   681
            return false;
jaroslav@557
   682
jaroslav@557
   683
        checkForComodification();
jaroslav@557
   684
        l.addAll(offset+index, c);
jaroslav@557
   685
        this.modCount = l.modCount;
jaroslav@557
   686
        size += cSize;
jaroslav@557
   687
        return true;
jaroslav@557
   688
    }
jaroslav@557
   689
jaroslav@557
   690
    public Iterator<E> iterator() {
jaroslav@557
   691
        return listIterator();
jaroslav@557
   692
    }
jaroslav@557
   693
jaroslav@557
   694
    public ListIterator<E> listIterator(final int index) {
jaroslav@557
   695
        checkForComodification();
jaroslav@557
   696
        rangeCheckForAdd(index);
jaroslav@557
   697
jaroslav@557
   698
        return new ListIterator<E>() {
jaroslav@557
   699
            private final ListIterator<E> i = l.listIterator(index+offset);
jaroslav@557
   700
jaroslav@557
   701
            public boolean hasNext() {
jaroslav@557
   702
                return nextIndex() < size;
jaroslav@557
   703
            }
jaroslav@557
   704
jaroslav@557
   705
            public E next() {
jaroslav@557
   706
                if (hasNext())
jaroslav@557
   707
                    return i.next();
jaroslav@557
   708
                else
jaroslav@557
   709
                    throw new NoSuchElementException();
jaroslav@557
   710
            }
jaroslav@557
   711
jaroslav@557
   712
            public boolean hasPrevious() {
jaroslav@557
   713
                return previousIndex() >= 0;
jaroslav@557
   714
            }
jaroslav@557
   715
jaroslav@557
   716
            public E previous() {
jaroslav@557
   717
                if (hasPrevious())
jaroslav@557
   718
                    return i.previous();
jaroslav@557
   719
                else
jaroslav@557
   720
                    throw new NoSuchElementException();
jaroslav@557
   721
            }
jaroslav@557
   722
jaroslav@557
   723
            public int nextIndex() {
jaroslav@557
   724
                return i.nextIndex() - offset;
jaroslav@557
   725
            }
jaroslav@557
   726
jaroslav@557
   727
            public int previousIndex() {
jaroslav@557
   728
                return i.previousIndex() - offset;
jaroslav@557
   729
            }
jaroslav@557
   730
jaroslav@557
   731
            public void remove() {
jaroslav@557
   732
                i.remove();
jaroslav@557
   733
                SubList.this.modCount = l.modCount;
jaroslav@557
   734
                size--;
jaroslav@557
   735
            }
jaroslav@557
   736
jaroslav@557
   737
            public void set(E e) {
jaroslav@557
   738
                i.set(e);
jaroslav@557
   739
            }
jaroslav@557
   740
jaroslav@557
   741
            public void add(E e) {
jaroslav@557
   742
                i.add(e);
jaroslav@557
   743
                SubList.this.modCount = l.modCount;
jaroslav@557
   744
                size++;
jaroslav@557
   745
            }
jaroslav@557
   746
        };
jaroslav@557
   747
    }
jaroslav@557
   748
jaroslav@557
   749
    public List<E> subList(int fromIndex, int toIndex) {
jaroslav@557
   750
        return new SubList<>(this, fromIndex, toIndex);
jaroslav@557
   751
    }
jaroslav@557
   752
jaroslav@557
   753
    private void rangeCheck(int index) {
jaroslav@557
   754
        if (index < 0 || index >= size)
jaroslav@557
   755
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
jaroslav@557
   756
    }
jaroslav@557
   757
jaroslav@557
   758
    private void rangeCheckForAdd(int index) {
jaroslav@557
   759
        if (index < 0 || index > size)
jaroslav@557
   760
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
jaroslav@557
   761
    }
jaroslav@557
   762
jaroslav@557
   763
    private String outOfBoundsMsg(int index) {
jaroslav@557
   764
        return "Index: "+index+", Size: "+size;
jaroslav@557
   765
    }
jaroslav@557
   766
jaroslav@557
   767
    private void checkForComodification() {
jaroslav@557
   768
        if (this.modCount != l.modCount)
jaroslav@557
   769
            throw new ConcurrentModificationException();
jaroslav@557
   770
    }
jaroslav@557
   771
}
jaroslav@557
   772
jaroslav@557
   773
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
jaroslav@557
   774
    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
jaroslav@557
   775
        super(list, fromIndex, toIndex);
jaroslav@557
   776
    }
jaroslav@557
   777
jaroslav@557
   778
    public List<E> subList(int fromIndex, int toIndex) {
jaroslav@557
   779
        return new RandomAccessSubList<>(this, fromIndex, toIndex);
jaroslav@557
   780
    }
jaroslav@557
   781
}