rt/emul/compact/src/main/java/java/util/AbstractCollection.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/AbstractCollection.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 <tt>Collection</tt>
jaroslav@557
    30
 * interface, to minimize the effort required to implement this interface. <p>
jaroslav@557
    31
 *
jaroslav@557
    32
 * To implement an unmodifiable collection, the programmer needs only to
jaroslav@557
    33
 * extend this class and provide implementations for the <tt>iterator</tt> and
jaroslav@557
    34
 * <tt>size</tt> methods.  (The iterator returned by the <tt>iterator</tt>
jaroslav@557
    35
 * method must implement <tt>hasNext</tt> and <tt>next</tt>.)<p>
jaroslav@557
    36
 *
jaroslav@557
    37
 * To implement a modifiable collection, the programmer must additionally
jaroslav@557
    38
 * override this class's <tt>add</tt> method (which otherwise throws an
jaroslav@557
    39
 * <tt>UnsupportedOperationException</tt>), and the iterator returned by the
jaroslav@557
    40
 * <tt>iterator</tt> method must additionally implement its <tt>remove</tt>
jaroslav@557
    41
 * method.<p>
jaroslav@557
    42
 *
jaroslav@557
    43
 * The programmer should generally provide a void (no argument) and
jaroslav@557
    44
 * <tt>Collection</tt> constructor, as per the recommendation in the
jaroslav@557
    45
 * <tt>Collection</tt> interface specification.<p>
jaroslav@557
    46
 *
jaroslav@557
    47
 * The documentation for each non-abstract method in this class describes its
jaroslav@557
    48
 * implementation in detail.  Each of these methods may be overridden if
jaroslav@557
    49
 * the collection being implemented admits a more efficient implementation.<p>
jaroslav@557
    50
 *
jaroslav@557
    51
 * This class is a member of the
jaroslav@557
    52
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@557
    53
 * Java Collections Framework</a>.
jaroslav@557
    54
 *
jaroslav@557
    55
 * @author  Josh Bloch
jaroslav@557
    56
 * @author  Neal Gafter
jaroslav@557
    57
 * @see Collection
jaroslav@557
    58
 * @since 1.2
jaroslav@557
    59
 */
jaroslav@557
    60
jaroslav@557
    61
public abstract class AbstractCollection<E> implements Collection<E> {
jaroslav@557
    62
    /**
jaroslav@557
    63
     * Sole constructor.  (For invocation by subclass constructors, typically
jaroslav@557
    64
     * implicit.)
jaroslav@557
    65
     */
jaroslav@557
    66
    protected AbstractCollection() {
jaroslav@557
    67
    }
jaroslav@557
    68
jaroslav@557
    69
    // Query Operations
jaroslav@557
    70
jaroslav@557
    71
    /**
jaroslav@557
    72
     * Returns an iterator over the elements contained in this collection.
jaroslav@557
    73
     *
jaroslav@557
    74
     * @return an iterator over the elements contained in this collection
jaroslav@557
    75
     */
jaroslav@557
    76
    public abstract Iterator<E> iterator();
jaroslav@557
    77
jaroslav@557
    78
    public abstract int size();
jaroslav@557
    79
jaroslav@557
    80
    /**
jaroslav@557
    81
     * {@inheritDoc}
jaroslav@557
    82
     *
jaroslav@557
    83
     * <p>This implementation returns <tt>size() == 0</tt>.
jaroslav@557
    84
     */
jaroslav@557
    85
    public boolean isEmpty() {
jaroslav@557
    86
        return size() == 0;
jaroslav@557
    87
    }
jaroslav@557
    88
jaroslav@557
    89
    /**
jaroslav@557
    90
     * {@inheritDoc}
jaroslav@557
    91
     *
jaroslav@557
    92
     * <p>This implementation iterates over the elements in the collection,
jaroslav@557
    93
     * checking each element in turn for equality with the specified element.
jaroslav@557
    94
     *
jaroslav@557
    95
     * @throws ClassCastException   {@inheritDoc}
jaroslav@557
    96
     * @throws NullPointerException {@inheritDoc}
jaroslav@557
    97
     */
jaroslav@557
    98
    public boolean contains(Object o) {
jaroslav@557
    99
        Iterator<E> it = iterator();
jaroslav@557
   100
        if (o==null) {
jaroslav@557
   101
            while (it.hasNext())
jaroslav@557
   102
                if (it.next()==null)
jaroslav@557
   103
                    return true;
jaroslav@557
   104
        } else {
jaroslav@557
   105
            while (it.hasNext())
jaroslav@557
   106
                if (o.equals(it.next()))
jaroslav@557
   107
                    return true;
jaroslav@557
   108
        }
jaroslav@557
   109
        return false;
jaroslav@557
   110
    }
jaroslav@557
   111
jaroslav@557
   112
    /**
jaroslav@557
   113
     * {@inheritDoc}
jaroslav@557
   114
     *
jaroslav@557
   115
     * <p>This implementation returns an array containing all the elements
jaroslav@557
   116
     * returned by this collection's iterator, in the same order, stored in
jaroslav@557
   117
     * consecutive elements of the array, starting with index {@code 0}.
jaroslav@557
   118
     * The length of the returned array is equal to the number of elements
jaroslav@557
   119
     * returned by the iterator, even if the size of this collection changes
jaroslav@557
   120
     * during iteration, as might happen if the collection permits
jaroslav@557
   121
     * concurrent modification during iteration.  The {@code size} method is
jaroslav@557
   122
     * called only as an optimization hint; the correct result is returned
jaroslav@557
   123
     * even if the iterator returns a different number of elements.
jaroslav@557
   124
     *
jaroslav@557
   125
     * <p>This method is equivalent to:
jaroslav@557
   126
     *
jaroslav@557
   127
     *  <pre> {@code
jaroslav@557
   128
     * List<E> list = new ArrayList<E>(size());
jaroslav@557
   129
     * for (E e : this)
jaroslav@557
   130
     *     list.add(e);
jaroslav@557
   131
     * return list.toArray();
jaroslav@557
   132
     * }</pre>
jaroslav@557
   133
     */
jaroslav@557
   134
    public Object[] toArray() {
jaroslav@557
   135
        // Estimate size of array; be prepared to see more or fewer elements
jaroslav@557
   136
        Object[] r = new Object[size()];
jaroslav@557
   137
        Iterator<E> it = iterator();
jaroslav@557
   138
        for (int i = 0; i < r.length; i++) {
jaroslav@557
   139
            if (! it.hasNext()) // fewer elements than expected
jaroslav@557
   140
                return Arrays.copyOf(r, i);
jaroslav@557
   141
            r[i] = it.next();
jaroslav@557
   142
        }
jaroslav@557
   143
        return it.hasNext() ? finishToArray(r, it) : r;
jaroslav@557
   144
    }
jaroslav@557
   145
jaroslav@557
   146
    /**
jaroslav@557
   147
     * {@inheritDoc}
jaroslav@557
   148
     *
jaroslav@557
   149
     * <p>This implementation returns an array containing all the elements
jaroslav@557
   150
     * returned by this collection's iterator in the same order, stored in
jaroslav@557
   151
     * consecutive elements of the array, starting with index {@code 0}.
jaroslav@557
   152
     * If the number of elements returned by the iterator is too large to
jaroslav@557
   153
     * fit into the specified array, then the elements are returned in a
jaroslav@557
   154
     * newly allocated array with length equal to the number of elements
jaroslav@557
   155
     * returned by the iterator, even if the size of this collection
jaroslav@557
   156
     * changes during iteration, as might happen if the collection permits
jaroslav@557
   157
     * concurrent modification during iteration.  The {@code size} method is
jaroslav@557
   158
     * called only as an optimization hint; the correct result is returned
jaroslav@557
   159
     * even if the iterator returns a different number of elements.
jaroslav@557
   160
     *
jaroslav@557
   161
     * <p>This method is equivalent to:
jaroslav@557
   162
     *
jaroslav@557
   163
     *  <pre> {@code
jaroslav@557
   164
     * List<E> list = new ArrayList<E>(size());
jaroslav@557
   165
     * for (E e : this)
jaroslav@557
   166
     *     list.add(e);
jaroslav@557
   167
     * return list.toArray(a);
jaroslav@557
   168
     * }</pre>
jaroslav@557
   169
     *
jaroslav@557
   170
     * @throws ArrayStoreException  {@inheritDoc}
jaroslav@557
   171
     * @throws NullPointerException {@inheritDoc}
jaroslav@557
   172
     */
jaroslav@557
   173
    public <T> T[] toArray(T[] a) {
jaroslav@557
   174
        // Estimate size of array; be prepared to see more or fewer elements
jaroslav@557
   175
        int size = size();
jaroslav@557
   176
        T[] r = a.length >= size ? a :
jaroslav@557
   177
                  (T[])java.lang.reflect.Array
jaroslav@557
   178
                  .newInstance(a.getClass().getComponentType(), size);
jaroslav@557
   179
        Iterator<E> it = iterator();
jaroslav@557
   180
jaroslav@557
   181
        for (int i = 0; i < r.length; i++) {
jaroslav@557
   182
            if (! it.hasNext()) { // fewer elements than expected
jaroslav@557
   183
                if (a != r)
jaroslav@557
   184
                    return Arrays.copyOf(r, i);
jaroslav@557
   185
                r[i] = null; // null-terminate
jaroslav@557
   186
                return r;
jaroslav@557
   187
            }
jaroslav@557
   188
            r[i] = (T)it.next();
jaroslav@557
   189
        }
jaroslav@557
   190
        return it.hasNext() ? finishToArray(r, it) : r;
jaroslav@557
   191
    }
jaroslav@557
   192
jaroslav@557
   193
    /**
jaroslav@557
   194
     * The maximum size of array to allocate.
jaroslav@557
   195
     * Some VMs reserve some header words in an array.
jaroslav@557
   196
     * Attempts to allocate larger arrays may result in
jaroslav@557
   197
     * OutOfMemoryError: Requested array size exceeds VM limit
jaroslav@557
   198
     */
jaroslav@557
   199
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
jaroslav@557
   200
jaroslav@557
   201
    /**
jaroslav@557
   202
     * Reallocates the array being used within toArray when the iterator
jaroslav@557
   203
     * returned more elements than expected, and finishes filling it from
jaroslav@557
   204
     * the iterator.
jaroslav@557
   205
     *
jaroslav@557
   206
     * @param r the array, replete with previously stored elements
jaroslav@557
   207
     * @param it the in-progress iterator over this collection
jaroslav@557
   208
     * @return array containing the elements in the given array, plus any
jaroslav@557
   209
     *         further elements returned by the iterator, trimmed to size
jaroslav@557
   210
     */
jaroslav@557
   211
    private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
jaroslav@557
   212
        int i = r.length;
jaroslav@557
   213
        while (it.hasNext()) {
jaroslav@557
   214
            int cap = r.length;
jaroslav@557
   215
            if (i == cap) {
jaroslav@557
   216
                int newCap = cap + (cap >> 1) + 1;
jaroslav@557
   217
                // overflow-conscious code
jaroslav@557
   218
                if (newCap - MAX_ARRAY_SIZE > 0)
jaroslav@557
   219
                    newCap = hugeCapacity(cap + 1);
jaroslav@557
   220
                r = Arrays.copyOf(r, newCap);
jaroslav@557
   221
            }
jaroslav@557
   222
            r[i++] = (T)it.next();
jaroslav@557
   223
        }
jaroslav@557
   224
        // trim if overallocated
jaroslav@557
   225
        return (i == r.length) ? r : Arrays.copyOf(r, i);
jaroslav@557
   226
    }
jaroslav@557
   227
jaroslav@557
   228
    private static int hugeCapacity(int minCapacity) {
jaroslav@557
   229
        if (minCapacity < 0) // overflow
jaroslav@557
   230
            throw new OutOfMemoryError
jaroslav@557
   231
                ("Required array size too large");
jaroslav@557
   232
        return (minCapacity > MAX_ARRAY_SIZE) ?
jaroslav@557
   233
            Integer.MAX_VALUE :
jaroslav@557
   234
            MAX_ARRAY_SIZE;
jaroslav@557
   235
    }
jaroslav@557
   236
jaroslav@557
   237
    // Modification Operations
jaroslav@557
   238
jaroslav@557
   239
    /**
jaroslav@557
   240
     * {@inheritDoc}
jaroslav@557
   241
     *
jaroslav@557
   242
     * <p>This implementation always throws an
jaroslav@557
   243
     * <tt>UnsupportedOperationException</tt>.
jaroslav@557
   244
     *
jaroslav@557
   245
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   246
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   247
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   248
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@557
   249
     * @throws IllegalStateException         {@inheritDoc}
jaroslav@557
   250
     */
jaroslav@557
   251
    public boolean add(E e) {
jaroslav@557
   252
        throw new UnsupportedOperationException();
jaroslav@557
   253
    }
jaroslav@557
   254
jaroslav@557
   255
    /**
jaroslav@557
   256
     * {@inheritDoc}
jaroslav@557
   257
     *
jaroslav@557
   258
     * <p>This implementation iterates over the collection looking for the
jaroslav@557
   259
     * specified element.  If it finds the element, it removes the element
jaroslav@557
   260
     * from the collection using the iterator's remove method.
jaroslav@557
   261
     *
jaroslav@557
   262
     * <p>Note that this implementation throws an
jaroslav@557
   263
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
jaroslav@557
   264
     * collection's iterator method does not implement the <tt>remove</tt>
jaroslav@557
   265
     * method and this collection contains the specified object.
jaroslav@557
   266
     *
jaroslav@557
   267
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   268
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   269
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   270
     */
jaroslav@557
   271
    public boolean remove(Object o) {
jaroslav@557
   272
        Iterator<E> it = iterator();
jaroslav@557
   273
        if (o==null) {
jaroslav@557
   274
            while (it.hasNext()) {
jaroslav@557
   275
                if (it.next()==null) {
jaroslav@557
   276
                    it.remove();
jaroslav@557
   277
                    return true;
jaroslav@557
   278
                }
jaroslav@557
   279
            }
jaroslav@557
   280
        } else {
jaroslav@557
   281
            while (it.hasNext()) {
jaroslav@557
   282
                if (o.equals(it.next())) {
jaroslav@557
   283
                    it.remove();
jaroslav@557
   284
                    return true;
jaroslav@557
   285
                }
jaroslav@557
   286
            }
jaroslav@557
   287
        }
jaroslav@557
   288
        return false;
jaroslav@557
   289
    }
jaroslav@557
   290
jaroslav@557
   291
jaroslav@557
   292
    // Bulk Operations
jaroslav@557
   293
jaroslav@557
   294
    /**
jaroslav@557
   295
     * {@inheritDoc}
jaroslav@557
   296
     *
jaroslav@557
   297
     * <p>This implementation iterates over the specified collection,
jaroslav@557
   298
     * checking each element returned by the iterator in turn to see
jaroslav@557
   299
     * if it's contained in this collection.  If all elements are so
jaroslav@557
   300
     * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
jaroslav@557
   301
     *
jaroslav@557
   302
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   303
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   304
     * @see #contains(Object)
jaroslav@557
   305
     */
jaroslav@557
   306
    public boolean containsAll(Collection<?> c) {
jaroslav@557
   307
        for (Object e : c)
jaroslav@557
   308
            if (!contains(e))
jaroslav@557
   309
                return false;
jaroslav@557
   310
        return true;
jaroslav@557
   311
    }
jaroslav@557
   312
jaroslav@557
   313
    /**
jaroslav@557
   314
     * {@inheritDoc}
jaroslav@557
   315
     *
jaroslav@557
   316
     * <p>This implementation iterates over the specified collection, and adds
jaroslav@557
   317
     * each object returned by the iterator to this collection, in turn.
jaroslav@557
   318
     *
jaroslav@557
   319
     * <p>Note that this implementation will throw an
jaroslav@557
   320
     * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
jaroslav@557
   321
     * overridden (assuming the specified collection is non-empty).
jaroslav@557
   322
     *
jaroslav@557
   323
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   324
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   325
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   326
     * @throws IllegalArgumentException      {@inheritDoc}
jaroslav@557
   327
     * @throws IllegalStateException         {@inheritDoc}
jaroslav@557
   328
     *
jaroslav@557
   329
     * @see #add(Object)
jaroslav@557
   330
     */
jaroslav@557
   331
    public boolean addAll(Collection<? extends E> c) {
jaroslav@557
   332
        boolean modified = false;
jaroslav@557
   333
        for (E e : c)
jaroslav@557
   334
            if (add(e))
jaroslav@557
   335
                modified = true;
jaroslav@557
   336
        return modified;
jaroslav@557
   337
    }
jaroslav@557
   338
jaroslav@557
   339
    /**
jaroslav@557
   340
     * {@inheritDoc}
jaroslav@557
   341
     *
jaroslav@557
   342
     * <p>This implementation iterates over this collection, checking each
jaroslav@557
   343
     * element returned by the iterator in turn to see if it's contained
jaroslav@557
   344
     * in the specified collection.  If it's so contained, it's removed from
jaroslav@557
   345
     * this collection with the iterator's <tt>remove</tt> method.
jaroslav@557
   346
     *
jaroslav@557
   347
     * <p>Note that this implementation will throw an
jaroslav@557
   348
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
jaroslav@557
   349
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
jaroslav@557
   350
     * and this collection contains one or more elements in common with the
jaroslav@557
   351
     * specified collection.
jaroslav@557
   352
     *
jaroslav@557
   353
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   354
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   355
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   356
     *
jaroslav@557
   357
     * @see #remove(Object)
jaroslav@557
   358
     * @see #contains(Object)
jaroslav@557
   359
     */
jaroslav@557
   360
    public boolean removeAll(Collection<?> c) {
jaroslav@557
   361
        boolean modified = false;
jaroslav@557
   362
        Iterator<?> it = iterator();
jaroslav@557
   363
        while (it.hasNext()) {
jaroslav@557
   364
            if (c.contains(it.next())) {
jaroslav@557
   365
                it.remove();
jaroslav@557
   366
                modified = true;
jaroslav@557
   367
            }
jaroslav@557
   368
        }
jaroslav@557
   369
        return modified;
jaroslav@557
   370
    }
jaroslav@557
   371
jaroslav@557
   372
    /**
jaroslav@557
   373
     * {@inheritDoc}
jaroslav@557
   374
     *
jaroslav@557
   375
     * <p>This implementation iterates over this collection, checking each
jaroslav@557
   376
     * element returned by the iterator in turn to see if it's contained
jaroslav@557
   377
     * in the specified collection.  If it's not so contained, it's removed
jaroslav@557
   378
     * from this collection with the iterator's <tt>remove</tt> method.
jaroslav@557
   379
     *
jaroslav@557
   380
     * <p>Note that this implementation will throw an
jaroslav@557
   381
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
jaroslav@557
   382
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
jaroslav@557
   383
     * and this collection contains one or more elements not present in the
jaroslav@557
   384
     * specified collection.
jaroslav@557
   385
     *
jaroslav@557
   386
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   387
     * @throws ClassCastException            {@inheritDoc}
jaroslav@557
   388
     * @throws NullPointerException          {@inheritDoc}
jaroslav@557
   389
     *
jaroslav@557
   390
     * @see #remove(Object)
jaroslav@557
   391
     * @see #contains(Object)
jaroslav@557
   392
     */
jaroslav@557
   393
    public boolean retainAll(Collection<?> c) {
jaroslav@557
   394
        boolean modified = false;
jaroslav@557
   395
        Iterator<E> it = iterator();
jaroslav@557
   396
        while (it.hasNext()) {
jaroslav@557
   397
            if (!c.contains(it.next())) {
jaroslav@557
   398
                it.remove();
jaroslav@557
   399
                modified = true;
jaroslav@557
   400
            }
jaroslav@557
   401
        }
jaroslav@557
   402
        return modified;
jaroslav@557
   403
    }
jaroslav@557
   404
jaroslav@557
   405
    /**
jaroslav@557
   406
     * {@inheritDoc}
jaroslav@557
   407
     *
jaroslav@557
   408
     * <p>This implementation iterates over this collection, removing each
jaroslav@557
   409
     * element using the <tt>Iterator.remove</tt> operation.  Most
jaroslav@557
   410
     * implementations will probably choose to override this method for
jaroslav@557
   411
     * efficiency.
jaroslav@557
   412
     *
jaroslav@557
   413
     * <p>Note that this implementation will throw an
jaroslav@557
   414
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
jaroslav@557
   415
     * collection's <tt>iterator</tt> method does not implement the
jaroslav@557
   416
     * <tt>remove</tt> method and this collection is non-empty.
jaroslav@557
   417
     *
jaroslav@557
   418
     * @throws UnsupportedOperationException {@inheritDoc}
jaroslav@557
   419
     */
jaroslav@557
   420
    public void clear() {
jaroslav@557
   421
        Iterator<E> it = iterator();
jaroslav@557
   422
        while (it.hasNext()) {
jaroslav@557
   423
            it.next();
jaroslav@557
   424
            it.remove();
jaroslav@557
   425
        }
jaroslav@557
   426
    }
jaroslav@557
   427
jaroslav@557
   428
jaroslav@557
   429
    //  String conversion
jaroslav@557
   430
jaroslav@557
   431
    /**
jaroslav@557
   432
     * Returns a string representation of this collection.  The string
jaroslav@557
   433
     * representation consists of a list of the collection's elements in the
jaroslav@557
   434
     * order they are returned by its iterator, enclosed in square brackets
jaroslav@557
   435
     * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
jaroslav@557
   436
     * <tt>", "</tt> (comma and space).  Elements are converted to strings as
jaroslav@557
   437
     * by {@link String#valueOf(Object)}.
jaroslav@557
   438
     *
jaroslav@557
   439
     * @return a string representation of this collection
jaroslav@557
   440
     */
jaroslav@557
   441
    public String toString() {
jaroslav@557
   442
        Iterator<E> it = iterator();
jaroslav@557
   443
        if (! it.hasNext())
jaroslav@557
   444
            return "[]";
jaroslav@557
   445
jaroslav@557
   446
        StringBuilder sb = new StringBuilder();
jaroslav@557
   447
        sb.append('[');
jaroslav@557
   448
        for (;;) {
jaroslav@557
   449
            E e = it.next();
jaroslav@557
   450
            sb.append(e == this ? "(this Collection)" : e);
jaroslav@557
   451
            if (! it.hasNext())
jaroslav@557
   452
                return sb.append(']').toString();
jaroslav@557
   453
            sb.append(',').append(' ');
jaroslav@557
   454
        }
jaroslav@557
   455
    }
jaroslav@557
   456
jaroslav@557
   457
}