OpenJDK / jdk9 / jdk9 / jdk
changeset 13937:45281ceba474
8148748: ArrayList.subList().spliterator() is not late-binding
Reviewed-by: psandoz
author | tvaleev |
---|---|
date | Tue, 22 Mar 2016 16:28:52 +0100 |
parents | 28db8a64363b |
children | 8ca730a3db4c |
files | src/java.base/share/classes/java/util/ArrayList.java test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java |
diffstat | 2 files changed, 72 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/ArrayList.java Tue Mar 22 16:28:50 2016 +0100 +++ b/src/java.base/share/classes/java/util/ArrayList.java Tue Mar 22 16:28:52 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1281,8 +1281,74 @@ public Spliterator<E> spliterator() { checkForComodification(); - return new ArrayListSpliterator<>(ArrayList.this, offset, - offset + this.size, this.modCount); + + return new Spliterator<>() { + private int index = offset; // current index, modified on advance/split + private int fence = -1; // -1 until used; then one past last index + private int expectedModCount; // initialized when fence set + + private int getFence() { // initialize fence to size on first use + int hi; // (a specialized variant appears in method forEach) + if ((hi = fence) < 0) { + expectedModCount = modCount; + hi = fence = offset + size; + } + return hi; + } + + public ArrayListSpliterator<E> trySplit() { + int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; + return (lo >= mid) ? null : // divide range in half unless too small + new ArrayListSpliterator<>(ArrayList.this, lo, index = mid, + expectedModCount); + } + + public boolean tryAdvance(Consumer<? super E> action) { + Objects.requireNonNull(action); + int hi = getFence(), i = index; + if (i < hi) { + index = i + 1; + @SuppressWarnings("unchecked") E e = (E)elementData[i]; + action.accept(e); + if (ArrayList.this.modCount != expectedModCount) + throw new ConcurrentModificationException(); + return true; + } + return false; + } + + public void forEachRemaining(Consumer<? super E> action) { + Objects.requireNonNull(action); + int i, hi, mc; // hoist accesses and checks from loop + ArrayList<E> lst = ArrayList.this; + Object[] a; + if ((a = lst.elementData) != null) { + if ((hi = fence) < 0) { + mc = modCount; + hi = offset + size; + } + else + mc = expectedModCount; + if ((i = index) >= 0 && (index = hi) <= a.length) { + for (; i < hi; ++i) { + @SuppressWarnings("unchecked") E e = (E) a[i]; + action.accept(e); + } + if (lst.modCount == mc) + return; + } + } + throw new ConcurrentModificationException(); + } + + public long estimateSize() { + return (long) (getFence() - index); + } + + public int characteristics() { + return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; + } + }; } }
--- a/test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java Tue Mar 22 16:28:50 2016 +0100 +++ b/test/java/util/Spliterator/SpliteratorLateBindingFailFastTest.java Tue Mar 22 16:28:52 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -52,6 +52,7 @@ /** * @test + * @bug 8148748 * @summary Spliterator last-binding and fail-fast tests * @run testng SpliteratorLateBindingFailFastTest */ @@ -120,8 +121,8 @@ } void addList(Function<Collection<T>, ? extends List<T>> l) { - // @@@ If collection is instance of List then add sub-list tests addCollection(l); + addCollection(l.andThen(list -> list.subList(0, list.size()))); } void addMap(Function<Map<T, T>, ? extends Map<T, T>> mapConstructor) {