Skip to content

Commit f12ed06

Browse files
author
Viktor Klang
committed
8048691: Spliterator.SORTED characteristics gets cleared for BaseStream.spliterator
Reviewed-by: psandoz, alanb
1 parent 8d78e8c commit f12ed06

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/java.base/share/classes/java/util/stream/StreamSpliterators.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -248,6 +248,11 @@ public final int characteristics() {
248248
c |= (spliterator.characteristics() & (Spliterator.SIZED | Spliterator.SUBSIZED));
249249
}
250250

251+
// It's not allowed for a Spliterator to report SORTED if not also ORDERED
252+
if ((c & Spliterator.SORTED) != 0 && (c & Spliterator.ORDERED) == 0) {
253+
c &= ~(Spliterator.SORTED);
254+
}
255+
251256
return c;
252257
}
253258

test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamSpliteratorTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -605,6 +605,27 @@ public void testDoubleParSpliterators(String name, TestData.OfDouble data) {
605605
}
606606
}
607607

608+
@Test
609+
public void testCharacteristicsForSortedUnorderedStreamSpliterators() {
610+
assertValidCombinationOfSortedAndOrdered(
611+
DoubleStream.of(3d,2d,4d,1d,5d).sorted().unordered().spliterator()
612+
);
613+
assertValidCombinationOfSortedAndOrdered(
614+
IntStream.of(3,2,4,1,5).sorted().unordered().spliterator()
615+
);
616+
assertValidCombinationOfSortedAndOrdered(
617+
LongStream.of(3L,2L,4L,1L,5L).sorted().unordered().spliterator()
618+
);
619+
assertValidCombinationOfSortedAndOrdered(
620+
Stream.of(3,2,4,1,5).sorted().unordered().spliterator()
621+
);
622+
}
623+
624+
void assertValidCombinationOfSortedAndOrdered(Spliterator<?> s) {
625+
if (s.hasCharacteristics(Spliterator.SORTED))
626+
Assert.assertTrue(s.hasCharacteristics(Spliterator.ORDERED));
627+
}
628+
608629
private List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions;
609630

610631
List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions() {

0 commit comments

Comments
 (0)