jdk/test/java/util/Iterator/IteratorDefaults.java
author kvn
Thu, 05 Dec 2013 15:13:12 -0800 (2013-12-05)
changeset 22858 f4a6f0eba875
parent 17432 efdf6eb85a17
child 23010 6dadb192ad81
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17432
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     1
/*
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     4
 *
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     8
 *
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    13
 * accompanied this code).
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    14
 *
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    18
 *
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    21
 * questions.
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    22
 */
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    23
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    24
import org.testng.annotations.Test;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    25
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    26
import java.lang.reflect.Constructor;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    27
import java.util.ArrayList;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    28
import java.util.Arrays;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    29
import java.util.Collection;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    30
import java.util.Collections;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    31
import java.util.Iterator;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    32
import java.util.List;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    33
import java.util.ListIterator;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    34
import java.util.NoSuchElementException;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    35
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    36
import static org.testng.Assert.assertEquals;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    37
import static org.testng.Assert.assertFalse;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    38
import static org.testng.Assert.assertTrue;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    39
import static org.testng.Assert.fail;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    40
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    41
/**
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    42
 * @test
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    43
 * @run testng IteratorDefaults
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    44
 * @summary test extension methods on Iterator
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    45
 */
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    46
@Test
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    47
public class IteratorDefaults {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    48
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    49
    private static interface Callback {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    50
        void call(List<Integer> list);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    51
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    52
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    53
    // call the callback for each recursive subList
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    54
    private void trimmedSubList(final List<Integer> list, final Callback callback) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    55
        int size = list.size();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    56
        if (size > 1) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    57
            // trim 1 element from both ends
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    58
            final List<Integer> subList = list.subList(1, size - 1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    59
            callback.call(subList);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    60
            trimmedSubList(subList, callback);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    61
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    62
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    63
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    64
    public void testRemoveUnsupported() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    65
        final Iterator iterator = new Iterator() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    66
            @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    67
            public boolean hasNext() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    68
                return false;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    69
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    70
            @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    71
            public Object next() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    72
                return null;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    73
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    74
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    75
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    76
        try {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    77
            iterator.remove();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    78
            fail("expected UnsupportedOperationException from remove not thrown");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    79
        } catch (UnsupportedOperationException ignore) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    80
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    81
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    82
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    83
    public void testRemoveOverride() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    84
        final IteratorWithRemove iterator = new IteratorWithRemove();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    85
        iterator.remove();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    86
        assertTrue(iterator.removed);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    87
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    88
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    89
    public void testForEach() throws Exception {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    90
        final Integer[] data = new Integer[1000];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    91
        for (int i=0; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    92
            data[i] = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    93
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    94
        final List<Integer> source = Arrays.asList(data);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    95
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    96
        final String[] iterableCollectionClasses = {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    97
                "java.util.ArrayDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    98
                "java.util.ArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
    99
                "java.util.HashSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   100
                "java.util.LinkedHashSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   101
                "java.util.LinkedList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   102
                "java.util.PriorityQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   103
                "java.util.TreeSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   104
                "java.util.Vector",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   105
                "java.util.concurrent.ConcurrentLinkedDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   106
                "java.util.concurrent.ConcurrentLinkedQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   107
                "java.util.concurrent.ConcurrentSkipListSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   108
                "java.util.concurrent.CopyOnWriteArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   109
                "java.util.concurrent.CopyOnWriteArraySet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   110
                "java.util.concurrent.LinkedBlockingDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   111
                "java.util.concurrent.LinkedBlockingQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   112
                "java.util.concurrent.LinkedTransferQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   113
                "java.util.concurrent.PriorityBlockingQueue"
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   114
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   115
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   116
        for (final String iterableClass : iterableCollectionClasses) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   117
            final Iterable<Integer> iterable =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   118
                    (Iterable<Integer>) Class.forName(iterableClass).newInstance();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   119
            ((Collection<Integer>) iterable).addAll(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   120
            final Iterator<Integer> iterator = iterable.iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   121
            final List<Integer> target = new ArrayList<>(source.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   122
            iterator.forEachRemaining(target::add);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   123
            if ("java.util.HashSet".equals(iterableClass)) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   124
                target.sort((x, y) -> x - y);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   125
                assertEquals(target, source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   126
            } else {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   127
                assertEquals(target, source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   128
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   129
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   130
            // verify that for an iterator that has been advanced via next(),
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   131
            // forEach starts from the current location, not zero
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   132
            final int OFFSET = 5;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   133
            final List<Integer> reference2 = new ArrayList<>(source).subList(OFFSET, source.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   134
            final List<Integer> removed2 = new ArrayList<>(OFFSET);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   135
            final Iterator<Integer> iterator2 = iterable.iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   136
            for (int i=0; i < OFFSET; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   137
                // advance the iterator by OFFSET, saving iterated elements
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   138
                removed2.add(iterator2.next());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   139
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   140
            final List<Integer> target2 = new ArrayList<>(reference2.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   141
            iterator2.forEachRemaining(target2::add);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   142
            if ("java.util.HashSet".equals(iterableClass)) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   143
                assertEquals(target2.size(), reference2.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   144
                target2.addAll(removed2);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   145
                target2.sort((x, y) -> x - y);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   146
                assertEquals(target2, source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   147
                assertEquals(target2.subList(OFFSET, source.size()), reference2);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   148
            } else {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   149
                assertEquals(target2, reference2);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   150
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   151
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   152
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   153
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   154
    public void testForEachSubList() throws Exception {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   155
        final Integer[] data = new Integer[100];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   156
        for (int i = 0; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   157
            data[i] = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   158
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   159
        final List<Integer> source = Arrays.asList(data);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   160
        final String[] listClasses = {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   161
                "java.util.ArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   162
                "java.util.LinkedList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   163
                "java.util.Vector",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   164
                "java.util.concurrent.CopyOnWriteArrayList"
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   165
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   166
        for (final String listClass : listClasses) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   167
            final List<Integer> list =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   168
                    (List<Integer>) Class.forName(listClass).newInstance();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   169
            list.addAll(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   170
            trimmedSubList(list, new Callback() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   171
                @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   172
                public void call(final List<Integer> list) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   173
                    if (list.size() < 1) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   174
                        return;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   175
                    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   176
                    final List<Integer> target = new ArrayList<>(list.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   177
                    final ListIterator<Integer> iterator = list.listIterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   178
                    assertTrue(iterator.hasNext());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   179
                    assertFalse(iterator.hasPrevious());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   180
                    assertEquals(iterator.nextIndex(), 0);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   181
                    assertEquals(iterator.previousIndex(), -1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   182
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   183
                    iterator.forEachRemaining(target::add);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   184
                    assertEquals(target, list);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   185
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   186
                    assertFalse(iterator.hasNext());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   187
                    assertTrue(iterator.hasPrevious());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   188
                    assertEquals(iterator.nextIndex(), list.size());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   189
                    assertEquals(iterator.previousIndex(), list.size() - 1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   190
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   191
                    try {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   192
                        iterator.next();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   193
                        fail(listClass + " iterator advanced beyond end");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   194
                    } catch (NoSuchElementException ignore) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   195
                    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   196
                }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   197
            });
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   198
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   199
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   200
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   201
    public void testOptimizedForEach() throws Exception {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   202
        final Integer[] data = new Integer[1000 * 1000];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   203
        for (int i=0; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   204
            data[i] = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   205
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   206
        final List<Integer> source = Arrays.asList(data);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   207
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   208
        final String[] listClasses = {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   209
                "java.util.ArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   210
                "java.util.LinkedList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   211
                "java.util.Vector",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   212
                "java.util.concurrent.CopyOnWriteArrayList"
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   213
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   214
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   215
        final int OFFSET = 3;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   216
        final List<Integer> target = new ArrayList<>(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   217
        for (final String listClass : listClasses) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   218
            final List<Integer> list =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   219
                    (List<Integer>) Class.forName(listClass).newInstance();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   220
            list.addAll(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   221
            final ListIterator<Integer> iterator = list.listIterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   222
            assertFalse(iterator.hasPrevious());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   223
            for (int i=0; i < OFFSET; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   224
                iterator.next();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   225
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   226
            assertTrue(iterator.hasNext());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   227
            assertTrue(iterator.hasPrevious());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   228
            assertEquals(iterator.nextIndex(), OFFSET);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   229
            assertEquals(iterator.previousIndex(), OFFSET - 1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   230
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   231
            iterator.forEachRemaining(e -> {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   232
                target.set(e, e + 1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   233
            });
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   234
            for (int i=OFFSET; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   235
                assertEquals(target.get(i).intValue(), source.get(i)+1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   236
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   237
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   238
            assertFalse(iterator.hasNext());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   239
            assertTrue(iterator.hasPrevious());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   240
            assertEquals(iterator.nextIndex(), data.length);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   241
            assertEquals(iterator.previousIndex(), data.length - 1);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   242
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   243
            // CopyOnWriteArrayList.listIterator().remove() is unsupported
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   244
            if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   245
                for (int i = data.length - 1; i >= 0; i--) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   246
                    iterator.remove(); // must not throw
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   247
                    if (i > 0) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   248
                        iterator.previous();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   249
                    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   250
                }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   251
                assertTrue(list.isEmpty());
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   252
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   253
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   254
            try {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   255
                iterator.next();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   256
                fail(listClass + " iterator advanced beyond end");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   257
            } catch (NoSuchElementException ignore) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   258
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   259
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   260
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   261
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   262
    @Test(enabled = false)
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   263
    public void compareForEachPerformance() throws Exception {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   264
        final Integer[] data = new Integer[1000 * 100];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   265
        for (int i=0; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   266
            data[i] = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   267
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   268
        final List<Integer> source = Arrays.asList(data);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   269
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   270
        final String[] iterableCollectionClasses = {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   271
                "java.util.ArrayList", // warmup, results discarded
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   272
                "java.util.ArrayDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   273
                "java.util.ArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   274
                "java.util.HashSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   275
                "java.util.LinkedHashSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   276
                "java.util.LinkedList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   277
                "java.util.PriorityQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   278
                "java.util.TreeSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   279
                "java.util.Vector",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   280
                "java.util.concurrent.ConcurrentLinkedDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   281
                "java.util.concurrent.ConcurrentLinkedQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   282
                "java.util.concurrent.ConcurrentSkipListSet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   283
                "java.util.concurrent.CopyOnWriteArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   284
                "java.util.concurrent.CopyOnWriteArraySet",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   285
                "java.util.concurrent.LinkedBlockingDeque",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   286
                "java.util.concurrent.LinkedBlockingQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   287
                "java.util.concurrent.LinkedTransferQueue",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   288
                "java.util.concurrent.PriorityBlockingQueue"
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   289
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   290
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   291
        boolean warmup = true;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   292
        final int ITERATIONS = 10;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   293
        final Integer[] target = new Integer[source.size()];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   294
        for (final String iterableClass : iterableCollectionClasses) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   295
            final Class<? extends Collection<Integer>> type =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   296
                    (Class<? extends Collection<Integer>>) Class.forName(iterableClass);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   297
            final Constructor<? extends Collection<Integer>> copyConstructor =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   298
                    type.getConstructor(Collection.class);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   299
            final Iterable<Integer> iterable = copyConstructor.newInstance(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   300
            final Iterable<Integer> reference =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   301
                    Collections.unmodifiableCollection((Collection<Integer>) iterable);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   302
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   303
            for (int i=0; i < ITERATIONS; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   304
                final Iterator<Integer> iterator = reference.iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   305
                final long forEachStart = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   306
                iterator.forEachRemaining(x -> {target[x.intValue()] = x;});
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   307
                final long forEachEnd = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   308
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   309
                final Iterator<Integer> iterator2 = reference.iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   310
                Integer x;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   311
                final long iteratorStart = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   312
                while (iterator2.hasNext()) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   313
                    x = iterator2.next();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   314
                    target[x.intValue()] = x;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   315
                }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   316
                final long iteratorEnd = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   317
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   318
                if (warmup) { continue; } // warmup, discard results
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   319
                final long forEachTime = forEachEnd - forEachStart;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   320
                final long iteratorTime = iteratorEnd - iteratorStart;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   321
                final long speedup = iteratorTime - forEachTime;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   322
                System.out.print(iterableClass);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   323
                System.out.print(" iterator: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   324
                System.out.print(iteratorTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   325
                System.out.print(", forEach: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   326
                System.out.print(forEachTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   327
                System.out.print(", speedup: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   328
                System.out.print(speedup);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   329
                System.out.print(" (");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   330
                System.out.print((speedup * 100) / iteratorTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   331
                System.out.print("%)\n");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   332
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   333
            if (warmup) { warmup = false; }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   334
            System.out.println();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   335
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   336
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   337
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   338
    @Test(enabled = false)
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   339
    public void compareSubListForEachPerformance() throws Exception {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   340
        final Integer[] data = new Integer[1000 * 100];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   341
        for (int i = 0; i < data.length; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   342
            data[i] = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   343
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   344
        final List<Integer> source = Arrays.asList(data);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   345
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   346
        final String[] listClasses = {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   347
                "java.util.ArrayList", // warmup, results discarded
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   348
                "java.util.ArrayList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   349
                "java.util.LinkedList",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   350
                "java.util.Vector",
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   351
                "java.util.concurrent.CopyOnWriteArrayList"
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   352
        };
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   353
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   354
        boolean warmup = true;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   355
        final int ITERATIONS = 10;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   356
        final Integer[] target = new Integer[source.size()];
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   357
        for (final String listClass : listClasses) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   358
            final Class<? extends List<Integer >> type =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   359
                    (Class<? extends List<Integer>>) Class.forName(listClass);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   360
            final Constructor<? extends List<Integer >> copyConstructor =
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   361
                    type.getConstructor(Collection.class);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   362
            final List<Integer> iterable = copyConstructor.newInstance(source);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   363
            final List<Integer> reference = Collections.unmodifiableList(iterable);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   364
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   365
            for (int i = 0; i < ITERATIONS; i++) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   366
                final Iterator<Integer> iterator = reference.subList(42, reference.size() - 37).iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   367
                final long forEachStart = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   368
                iterator.forEachRemaining(x -> {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   369
                    target[x.intValue()] = x;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   370
                });
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   371
                final long forEachEnd = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   372
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   373
                final Iterator<Integer> iterator2 = reference.iterator();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   374
                Integer x;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   375
                final long iteratorStart = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   376
                while (iterator2.hasNext()) {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   377
                    x = iterator2.next();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   378
                    target[x.intValue()] = x;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   379
                }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   380
                final long iteratorEnd = System.nanoTime();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   381
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   382
                if (warmup) { continue; } // warmup, discard results
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   383
                final long forEachTime = forEachEnd - forEachStart;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   384
                final long iteratorTime = iteratorEnd - iteratorStart;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   385
                final long speedup = iteratorTime - forEachTime;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   386
                System.out.print(listClass);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   387
                System.out.print(" iterator: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   388
                System.out.print(iteratorTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   389
                System.out.print(", forEach: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   390
                System.out.print(forEachTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   391
                System.out.print(", speedup: ");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   392
                System.out.print(speedup);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   393
                System.out.print(" (");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   394
                System.out.print((speedup * 100) / iteratorTime);
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   395
                System.out.print("%)\n");
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   396
            }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   397
            if (warmup) { warmup = false; }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   398
            System.out.println();
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   399
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   400
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   401
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   402
    static class IteratorWithRemove implements Iterator {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   403
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   404
        public boolean removed;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   405
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   406
        IteratorWithRemove() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   407
            removed = false;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   408
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   409
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   410
        @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   411
        public boolean hasNext() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   412
            return false;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   413
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   414
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   415
        @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   416
        public Object next() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   417
            return null;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   418
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   419
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   420
        @Override
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   421
        public void remove() {
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   422
            removed = true;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   423
        }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   424
    }
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents:
diff changeset
   425
}