Skip to content

Commit

Permalink
Annotate the parameter of List.sort as @Nullable.
Browse files Browse the repository at this point in the history
* Annotate the parameter of `List.sort` as `@Nullable`.

This has always struck me as a weird feature, but people use it, and it
"works" in the sense of "does not cause NPE" (though it may cause
CCE...). I see both calls to `sort(null)` and calls like
`sort(priorityQueue.comparator())` (which _might_ be `null`;
[example](https://github.com/google/guava/blob/e82e2a2c07c68108f318958ee0355cc835c97743/guava-testlib/src/com/google/common/collect/testing/testers/SortedSetNavigationTester.java#L57)).
And while I prefer a world in which methods like `comparator()` never
return `null`, as we arranged for [in
`SortedMultiset`](https://guava.dev/SortedMultiset#comparator()), there
are apparently [downsides](google/guava#6187)
to using a `Comparator` that implements natural order rather than using
`null`.

Also, Werner points us to `Arrays.sort`.

(#13)
  • Loading branch information
cpovirk committed Jun 7, 2023
1 parent f9395fb commit a227f7d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ private void replaceAllRange(UnaryOperator<E> operator, int i, int end) {

@Override
@SuppressWarnings("unchecked")
public void sort(Comparator<? super E> c) {
public void sort(@Nullable Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size, c);
if (modCount != expectedModCount)
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ default void replaceAll(UnaryOperator<E> operator) {
* @since 1.8
*/
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
default void sort(@Nullable Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ public synchronized void replaceAll(UnaryOperator<E> operator) {

@SuppressWarnings("unchecked")
@Override
public synchronized void sort(Comparator<? super E> c) {
public synchronized void sort(@Nullable Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, elementCount, c);
if (modCount != expectedModCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ void replaceAllRange(UnaryOperator<E> operator, int i, int end) {
setArray(es);
}

public void sort(Comparator<? super E> c) {
public void sort(@Nullable Comparator<? super E> c) {
synchronized (lock) {
sortRange(c, 0, getArray().length);
}
Expand Down

0 comments on commit a227f7d

Please sign in to comment.