Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/java.base/share/classes/java/util/stream/SortedOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,10 @@ private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
* {@code Comparable}.
*/
OfRef(AbstractPipeline<?, T, ?> upstream) {
super(upstream, StreamShape.REFERENCE,
StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED);
this.isNaturalSort = true;
// Will throw CCE when we try to sort if T is not Comparable
@SuppressWarnings("unchecked")
Comparator<? super T> comp = (Comparator<? super T>) Comparator.naturalOrder();
this.comparator = comp;
this(upstream, comp);
}

/**
Expand All @@ -123,10 +120,13 @@ private static final class OfRef<T> extends ReferencePipeline.StatefulOp<T, T> {
* @param comparator The comparator to be used to evaluate ordering.
*/
OfRef(AbstractPipeline<?, T, ?> upstream, Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
boolean isNaturalSort = Comparator.naturalOrder().equals(comparator);
super(upstream, StreamShape.REFERENCE,
StreamOpFlag.IS_ORDERED | StreamOpFlag.NOT_SORTED);
this.isNaturalSort = false;
this.comparator = Objects.requireNonNull(comparator);
StreamOpFlag.IS_ORDERED |
(isNaturalSort ? StreamOpFlag.IS_SORTED : StreamOpFlag.NOT_SORTED));
this.isNaturalSort = isNaturalSort;
this.comparator = comparator;
Comment on lines 125 to +129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are calling super explicitly, we prefer putting field assignments before the super constructor call in anticipation for more robust object construction.

}

@Override
Expand Down
22 changes: 12 additions & 10 deletions src/java.base/share/classes/java/util/stream/StreamOpFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package java.util.stream;

import java.util.Comparator;
import java.util.EnumMap;
import java.util.Map;
import java.util.Spliterator;
Expand Down Expand Up @@ -738,24 +739,25 @@ static int toCharacteristics(int streamFlags) {
*
* @implSpec
* If the spliterator is naturally {@code SORTED} (the associated
* {@code Comparator} is {@code null}) then the characteristic is converted
* to the {@link #SORTED} flag, otherwise the characteristic is not
* converted.
* {@code Comparator} is {@code null} or {@code Comparator.naturalOrder()}) then
* the characteristic is converted to the {@link #SORTED} flag, otherwise
* the characteristic is not converted.
*
* @param spliterator the spliterator from which to obtain characteristic
* bit set.
* @return the stream flags.
*/
static int fromCharacteristics(Spliterator<?> spliterator) {
int characteristics = spliterator.characteristics();
if ((characteristics & Spliterator.SORTED) != 0 && spliterator.getComparator() != null) {
// Do not propagate the SORTED characteristic if it does not correspond
// to a natural sort order
return characteristics & SPLITERATOR_CHARACTERISTICS_MASK & ~Spliterator.SORTED;
}
else {
return characteristics & SPLITERATOR_CHARACTERISTICS_MASK;
if ((characteristics & Spliterator.SORTED) != 0) {
Comparator<?> comparator = spliterator.getComparator();
if (comparator != null && !Comparator.naturalOrder().equals(comparator)) {
// Do not propagate the SORTED characteristic if it does not correspond
// to a natural sort order
return characteristics & SPLITERATOR_CHARACTERISTICS_MASK & ~Spliterator.SORTED;
}
}
return characteristics & SPLITERATOR_CHARACTERISTICS_MASK;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,10 @@ public Comparator<? super Object> getComparator() {
int flags = StreamOpFlag.fromCharacteristics(new SortedEmptySpliterator((a, b) -> 0));
assertEquals(flags, 0);
}

{
int flags = StreamOpFlag.fromCharacteristics(new SortedEmptySpliterator(Comparator.naturalOrder()));
assertEquals(flags, StreamOpFlag.IS_SORTED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.testng.annotations.Test;

import java.util.*;
import java.util.Comparator;
import java.util.Spliterators;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -115,10 +116,14 @@ public void testSorted() {

Collections.reverse(to10);
assertSorted(to10.stream().sorted().iterator());
assertSorted(to10.stream().sorted(Comparator.naturalOrder()).iterator());

Spliterator<Integer> s = to10.stream().sorted().spliterator();
assertTrue(s.hasCharacteristics(Spliterator.SORTED));

s = to10.stream().sorted(Comparator.naturalOrder()).spliterator();
assertTrue(s.hasCharacteristics(Spliterator.SORTED));

s = to10.stream().sorted(cInteger.reversed()).spliterator();
assertFalse(s.hasCharacteristics(Spliterator.SORTED));
}
Expand Down