Java Code Examples for java.util.ListIterator#forEachRemaining()

The following examples show how to use java.util.ListIterator#forEachRemaining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForEachRemainingTester.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public static <T> void test_forEachRemaining_list(List<T> collection, T[] initialData)
        throws Exception {
    test_forEachRemaining(collection, initialData);

    ArrayList<T> recorder = new ArrayList<>();
    ListIterator<T> lit = collection.listIterator(1);

    lit.forEachRemaining((T i) -> recorder.add(i));
    if (initialData.length > 0) {
        assertEquals(initialData.length - 1, recorder.size());
        for (int i = 1; i < initialData.length; i++){
            assertEquals(initialData[i], recorder.get(i - 1));
        }
    }
}
 
Example 2
Source File: IteratorDefaults.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 3
Source File: IteratorDefaults.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 4
Source File: IteratorDefaults.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 5
Source File: IteratorDefaults.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 6
Source File: IteratorDefaults.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 7
Source File: IteratorDefaults.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 8
Source File: IteratorDefaults.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 9
Source File: IteratorDefaults.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 10
Source File: IteratorDefaults.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 11
Source File: IteratorDefaults.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 12
Source File: IteratorDefaults.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 13
Source File: IteratorDefaults.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}
 
Example 14
Source File: IteratorDefaults.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testOptimizedForEach() throws Exception {
    final Integer[] data = new Integer[1000 * 1000];
    for (int i=0; i < data.length; i++) {
        data[i] = i;
    }
    final List<Integer> source = Arrays.asList(data);

    final String[] listClasses = {
            "java.util.ArrayList",
            "java.util.LinkedList",
            "java.util.Vector",
            "java.util.concurrent.CopyOnWriteArrayList"
    };

    final int OFFSET = 3;
    final List<Integer> target = new ArrayList<>(source);
    for (final String listClass : listClasses) {
        final List<Integer> list =
                (List<Integer>) Class.forName(listClass).newInstance();
        list.addAll(source);
        final ListIterator<Integer> iterator = list.listIterator();
        assertFalse(iterator.hasPrevious());
        for (int i=0; i < OFFSET; i++) {
            iterator.next();
        }
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), OFFSET);
        assertEquals(iterator.previousIndex(), OFFSET - 1);

        iterator.forEachRemaining(e -> {
            target.set(e, e + 1);
        });
        for (int i=OFFSET; i < data.length; i++) {
            assertEquals(target.get(i).intValue(), source.get(i)+1);
        }

        assertFalse(iterator.hasNext());
        assertTrue(iterator.hasPrevious());
        assertEquals(iterator.nextIndex(), data.length);
        assertEquals(iterator.previousIndex(), data.length - 1);

        // CopyOnWriteArrayList.listIterator().remove() is unsupported
        if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
            for (int i = data.length - 1; i >= 0; i--) {
                iterator.remove(); // must not throw
                if (i > 0) {
                    iterator.previous();
                }
            }
            assertTrue(list.isEmpty());
        }

        try {
            iterator.next();
            fail(listClass + " iterator advanced beyond end");
        } catch (NoSuchElementException ignore) {
        }
    }
}