8267452: Delegate forEachRemaining in Spliterators.iterator()
Reviewed-by: psandoz
This commit is contained in:
parent
d0d2ddccaf
commit
ac36b7d3e2
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2021, 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
|
||||
@ -688,9 +688,23 @@ public final class Spliterators {
|
||||
throw new NoSuchElementException();
|
||||
else {
|
||||
valueReady = false;
|
||||
return nextElement;
|
||||
T t = nextElement;
|
||||
nextElement = null;
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(Consumer<? super T> action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (valueReady) {
|
||||
valueReady = false;
|
||||
T t = nextElement;
|
||||
nextElement = null;
|
||||
action.accept(t);
|
||||
}
|
||||
spliterator.forEachRemaining(action);
|
||||
}
|
||||
}
|
||||
|
||||
return new Adapter();
|
||||
@ -736,6 +750,16 @@ public final class Spliterators {
|
||||
return nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(IntConsumer action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (valueReady) {
|
||||
valueReady = false;
|
||||
action.accept(nextElement);
|
||||
}
|
||||
spliterator.forEachRemaining(action);
|
||||
}
|
||||
}
|
||||
|
||||
return new Adapter();
|
||||
@ -781,6 +805,16 @@ public final class Spliterators {
|
||||
return nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(LongConsumer action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (valueReady) {
|
||||
valueReady = false;
|
||||
action.accept(nextElement);
|
||||
}
|
||||
spliterator.forEachRemaining(action);
|
||||
}
|
||||
}
|
||||
|
||||
return new Adapter();
|
||||
@ -826,6 +860,16 @@ public final class Spliterators {
|
||||
return nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(DoubleConsumer action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (valueReady) {
|
||||
valueReady = false;
|
||||
action.accept(nextElement);
|
||||
}
|
||||
spliterator.forEachRemaining(action);
|
||||
}
|
||||
}
|
||||
|
||||
return new Adapter();
|
||||
@ -1843,7 +1887,7 @@ public final class Spliterators {
|
||||
static final class IntIteratorSpliterator implements Spliterator.OfInt {
|
||||
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
||||
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
||||
private PrimitiveIterator.OfInt it;
|
||||
private final PrimitiveIterator.OfInt it;
|
||||
private final int characteristics;
|
||||
private long est; // size estimate
|
||||
private int batch; // batch size for splits
|
||||
@ -1937,7 +1981,7 @@ public final class Spliterators {
|
||||
static final class LongIteratorSpliterator implements Spliterator.OfLong {
|
||||
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
||||
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
||||
private PrimitiveIterator.OfLong it;
|
||||
private final PrimitiveIterator.OfLong it;
|
||||
private final int characteristics;
|
||||
private long est; // size estimate
|
||||
private int batch; // batch size for splits
|
||||
@ -2031,7 +2075,7 @@ public final class Spliterators {
|
||||
static final class DoubleIteratorSpliterator implements Spliterator.OfDouble {
|
||||
static final int BATCH_UNIT = IteratorSpliterator.BATCH_UNIT;
|
||||
static final int MAX_BATCH = IteratorSpliterator.MAX_BATCH;
|
||||
private PrimitiveIterator.OfDouble it;
|
||||
private final PrimitiveIterator.OfDouble it;
|
||||
private final int characteristics;
|
||||
private long est; // size estimate
|
||||
private int batch; // batch size for splits
|
||||
|
149
test/jdk/java/util/Spliterator/IteratorFromSpliteratorTest.java
Normal file
149
test/jdk/java/util/Spliterator/IteratorFromSpliteratorTest.java
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (c) 2021, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.PrimitiveIterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.DoubleConsumer;
|
||||
import java.util.function.IntConsumer;
|
||||
import java.util.function.LongConsumer;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertFalse;
|
||||
import static org.testng.Assert.assertThrows;
|
||||
import static org.testng.Assert.fail;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @summary Spliterator.iterator traversing tests
|
||||
* @library /lib/testlibrary/bootlib
|
||||
* @run testng IteratorFromSpliteratorTest
|
||||
* @bug 8267452
|
||||
*/
|
||||
public class IteratorFromSpliteratorTest {
|
||||
@Test
|
||||
public void testIteratorFromSpliterator() {
|
||||
List<Integer> input = List.of(1, 2, 3, 4, 5);
|
||||
for (int i = 0; i < input.size(); i++) {
|
||||
Iterator<Integer> iterator = Spliterators.iterator(input.spliterator());
|
||||
List<Integer> result = new ArrayList<>();
|
||||
int j = i;
|
||||
while (j++ < input.size() && iterator.hasNext()) {
|
||||
result.add(iterator.next());
|
||||
}
|
||||
// While SpliteratorTraversingAndSplittingTest tests some scenarios with Spliterators.iterator
|
||||
// it always wraps the resulting iterator into spliterator again, and this limits the use patterns.
|
||||
// In particular, calling hasNext() right before forEachRemaining() is not tested.
|
||||
// Here we cover such a scenario.
|
||||
assertEquals(iterator.hasNext(), result.size() < input.size());
|
||||
iterator.forEachRemaining(result::add);
|
||||
iterator.forEachRemaining(x -> fail("Should not be called"));
|
||||
assertFalse(iterator.hasNext());
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
iterator.forEachRemaining(x -> fail("Should not be called"));
|
||||
assertEquals(result, input);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIteratorFromSpliteratorInt() {
|
||||
int[] input = {1, 2, 3, 4, 5};
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
PrimitiveIterator.OfInt iterator = Spliterators.iterator(Arrays.spliterator(input));
|
||||
List<Integer> result = new ArrayList<>();
|
||||
int j = i;
|
||||
while (j++ < input.length && iterator.hasNext()) {
|
||||
result.add(iterator.nextInt());
|
||||
}
|
||||
assertEquals(iterator.hasNext(), result.size() < input.length);
|
||||
iterator.forEachRemaining((IntConsumer) result::add);
|
||||
iterator.forEachRemaining((IntConsumer) (x -> fail("Should not be called")));
|
||||
assertFalse(iterator.hasNext());
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
iterator.forEachRemaining((IntConsumer) (x -> fail("Should not be called")));
|
||||
assertEquals(result.stream().mapToInt(x -> x).toArray(), input);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIteratorFromSpliteratorLong() {
|
||||
long[] input = {1, 2, 3, 4, 5};
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
PrimitiveIterator.OfLong iterator = Spliterators.iterator(Arrays.spliterator(input));
|
||||
List<Long> result = new ArrayList<>();
|
||||
int j = i;
|
||||
while (j++ < input.length && iterator.hasNext()) {
|
||||
result.add(iterator.nextLong());
|
||||
}
|
||||
assertEquals(iterator.hasNext(), result.size() < input.length);
|
||||
iterator.forEachRemaining((LongConsumer) result::add);
|
||||
iterator.forEachRemaining((LongConsumer) (x -> fail("Should not be called")));
|
||||
assertFalse(iterator.hasNext());
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
iterator.forEachRemaining((LongConsumer) (x -> fail("Should not be called")));
|
||||
assertEquals(result.stream().mapToLong(x -> x).toArray(), input);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIteratorFromSpliteratorDouble() {
|
||||
double[] input = {1, 2, 3, 4, 5};
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
PrimitiveIterator.OfDouble iterator = Spliterators.iterator(Arrays.spliterator(input));
|
||||
List<Double> result = new ArrayList<>();
|
||||
int j = i;
|
||||
while (j++ < input.length && iterator.hasNext()) {
|
||||
result.add(iterator.nextDouble());
|
||||
}
|
||||
assertEquals(iterator.hasNext(), result.size() < input.length);
|
||||
iterator.forEachRemaining((DoubleConsumer) result::add);
|
||||
iterator.forEachRemaining((DoubleConsumer) (x -> fail("Should not be called")));
|
||||
assertFalse(iterator.hasNext());
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
iterator.forEachRemaining((DoubleConsumer) (x -> fail("Should not be called")));
|
||||
assertEquals(result.stream().mapToDouble(x -> x).toArray(), input);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIteratorFromSpliteratorEmpty() {
|
||||
Iterator<?>[] iterators = {
|
||||
Spliterators.iterator(Spliterators.emptySpliterator()),
|
||||
Spliterators.iterator(Spliterators.emptyIntSpliterator()),
|
||||
Spliterators.iterator(Spliterators.emptyLongSpliterator()),
|
||||
Spliterators.iterator(Spliterators.emptyDoubleSpliterator())
|
||||
};
|
||||
for (Iterator<?> iterator : iterators) {
|
||||
iterator.forEachRemaining(x -> fail("Should not be called"));
|
||||
assertFalse(iterator.hasNext());
|
||||
iterator.forEachRemaining(x -> fail("Should not be called"));
|
||||
assertThrows(NoSuchElementException.class, iterator::next);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user