Skip to content

Commit

Permalink
Update nullness annotations after cl/662127972, and prepare for the f…
Browse files Browse the repository at this point in the history
…orthcoming version of our nullness checker.

The headliner in this CL is the addition of missing annotations in `Immutable*.Builder` (though the annotations don't reveal any danger of NPE). This CL also works around a type-inference problem in `Multisets`. Finally, it has a simplification in `SortedLists` that used to work around a problem but that is no longer necessary. Still, a simplification is a simplification, so I've kept it as part of the CL anyway.

RELNOTES=n/a
PiperOrigin-RevId: 663810873
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Aug 16, 2024
1 parent a4a7f6b commit a94ff8b
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ abstract static class ArrayBasedBuilder<E> extends ImmutableCollection.Builder<E
* current array, create a new array.
*/
private void ensureRoomFor(int newElements) {
Object[] contents = this.contents;
@Nullable Object[] contents = this.contents;
int newCapacity = expandedCapacity(contents.length, size + newElements);
// expandedCapacity handles the overflow case
if (newCapacity > contents.length || forceCopy) {
Expand Down
3 changes: 2 additions & 1 deletion android/guava/src/com/google/common/collect/SortedLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -278,7 +279,7 @@ public static <E extends Comparable> int binarySearch(
checkNotNull(presentBehavior);
checkNotNull(absentBehavior);
if (!(list instanceof RandomAccess)) {
list = Lists.newArrayList(list);
list = new ArrayList<>(list);
}
// TODO(lowasser): benchmark when it's best to do a linear search

Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/ImmutableList.java
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public Builder() {
}

private void ensureRoomFor(int newElements) {
Object[] contents = this.contents;
@Nullable Object[] contents = this.contents;
int newCapacity = expandedCapacity(contents.length, size + newElements);
// expandedCapacity handles the overflow case
if (contents.length < newCapacity || copyOnWrite) {
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/Multisets.java
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ public void remove() {
Spliterator<Entry<E>> entrySpliterator = multiset.entrySet().spliterator();
return CollectSpliterators.flatMap(
entrySpliterator,
entry -> Collections.nCopies(entry.getCount(), entry.getElement()).spliterator(),
(Entry<E> entry) -> Collections.nCopies(entry.getCount(), entry.getElement()).spliterator(),
Spliterator.SIZED
| (entrySpliterator.characteristics()
& (Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE)),
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/SortedLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -278,7 +279,7 @@ public static <E extends Comparable> int binarySearch(
checkNotNull(presentBehavior);
checkNotNull(absentBehavior);
if (!(list instanceof RandomAccess)) {
list = Lists.newArrayList(list);
list = new ArrayList<>(list);
}
// TODO(lowasser): benchmark when it's best to do a linear search

Expand Down

0 comments on commit a94ff8b

Please sign in to comment.