Skip to content

Commit

Permalink
Fix, suppress, and/or localize suppressions for unchecked and `rawt…
Browse files Browse the repository at this point in the history
…ypes` warnings in `collect`.

RELNOTES=n/a
PiperOrigin-RevId: 609475939
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Feb 22, 2024
1 parent 12020e2 commit 7e06618
Show file tree
Hide file tree
Showing 85 changed files with 273 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
* @author Louis Wasserman
*/
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
@GwtIncompatible
@ElementTypesAreNonnullByDefault
abstract class AbstractRangeSet<C extends Comparable> implements RangeSet<C> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static <R, C, V> ArrayTable<R, C, V> create(
*
* @throws NullPointerException if {@code table} has a null key
*/
@SuppressWarnings("unchecked") // TODO(cpovirk): Make constructor accept wildcard types?
public static <R, C, V> ArrayTable<R, C, V> create(Table<R, C, ? extends @Nullable V> table) {
return (table instanceof ArrayTable)
? new ArrayTable<R, C, V>((ArrayTable<R, C, V>) table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ final class CompoundOrdering<T extends @Nullable Object> extends Ordering<T>

@SuppressWarnings("unchecked") // Generic array creation
CompoundOrdering(Comparator<? super T> primary, Comparator<? super T> secondary) {
this.comparators = (Comparator<? super T>[]) new Comparator[] {primary, secondary};
this.comparators = (Comparator<? super T>[]) new Comparator<?>[] {primary, secondary};
}

@SuppressWarnings("unchecked") // Generic array creation
CompoundOrdering(Iterable<? extends Comparator<? super T>> comparators) {
this.comparators = Iterables.toArray(comparators, (Comparator<? super T>[]) new Comparator[0]);
this.comparators =
Iterables.toArray(comparators, (Comparator<? super T>[]) new Comparator<?>[0]);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public final class ConcurrentHashMultiset<E> extends AbstractMultiset<E> impleme
// This constant allows the deserialization code to set a final field. This holder class
// makes sure it is not initialized unless an instance is deserialized.
private static class FieldSettersHolder {
static final FieldSetter<ConcurrentHashMultiset> COUNT_MAP_FIELD_SETTER =
static final FieldSetter<? super ConcurrentHashMultiset<?>> COUNT_MAP_FIELD_SETTER =
Serialization.getFieldSetter(ConcurrentHashMultiset.class, "countMap");
}

Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/collect/Cut.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Kevin Bourrillion
*/
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
@GwtCompatible
@ElementTypesAreNonnullByDefault
abstract class Cut<C extends Comparable> implements Comparable<Cut<C>>, Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Kevin Bourrillion
* @since 10.0
*/
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
@GwtCompatible
@ElementTypesAreNonnullByDefault
public abstract class DiscreteDomain<C extends Comparable> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@ElementTypesAreNonnullByDefault
final class GeneralRange<T extends @Nullable Object> implements Serializable {
/** Converts a Range to a GeneralRange. */
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
static <T extends Comparable> GeneralRange<T> from(Range<T> range) {
T lowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null;
BoundType lowerBoundType = range.hasLowerBound() ? range.lowerBoundType() : OPEN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ public static <K, V> ImmutableMultimap<K, V> copyOf(
@GwtIncompatible // java serialization is not supported
@J2ktIncompatible
static class FieldSettersHolder {
static final Serialization.FieldSetter<ImmutableMultimap> MAP_FIELD_SETTER =
static final Serialization.FieldSetter<? super ImmutableMultimap<?, ?>> MAP_FIELD_SETTER =
Serialization.getFieldSetter(ImmutableMultimap.class, "map");
static final Serialization.FieldSetter<ImmutableMultimap> SIZE_FIELD_SETTER =
static final Serialization.FieldSetter<? super ImmutableMultimap<?, ?>> SIZE_FIELD_SETTER =
Serialization.getFieldSetter(ImmutableMultimap.class, "size");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public V get(K key) {
int index =
SortedLists.binarySearch(
ranges,
Range.<K>lowerBoundFn(),
Range::lowerBound,
Cut.belowValue(key),
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_LOWER);
Expand All @@ -201,7 +201,7 @@ public Entry<Range<K>, V> getEntry(K key) {
int index =
SortedLists.binarySearch(
ranges,
Range.<K>lowerBoundFn(),
Range::lowerBound,
Cut.belowValue(key),
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_LOWER);
Expand Down Expand Up @@ -318,14 +318,14 @@ public ImmutableRangeMap<K, V> subRangeMap(final Range<K> range) {
int lowerIndex =
SortedLists.binarySearch(
ranges,
Range.<K>upperBoundFn(),
Range::upperBound,
range.lowerBound,
KeyPresentBehavior.FIRST_AFTER,
KeyAbsentBehavior.NEXT_HIGHER);
int upperIndex =
SortedLists.binarySearch(
ranges,
Range.<K>lowerBoundFn(),
Range::lowerBound,
range.upperBound,
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_HIGHER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* @author Louis Wasserman
* @since 14.0
*/
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
@GwtIncompatible
@ElementTypesAreNonnullByDefault
public final class ImmutableRangeSet<C extends Comparable> extends AbstractRangeSet<C>
Expand Down Expand Up @@ -160,7 +161,7 @@ public boolean intersects(Range<C> otherRange) {
int ceilingIndex =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
Range::lowerBound,
otherRange.lowerBound,
Ordering.natural(),
ANY_PRESENT,
Expand All @@ -180,7 +181,7 @@ public boolean encloses(Range<C> otherRange) {
int index =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
Range::lowerBound,
otherRange.lowerBound,
Ordering.natural(),
ANY_PRESENT,
Expand All @@ -194,7 +195,7 @@ public Range<C> rangeContaining(C value) {
int index =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
Range::lowerBound,
Cut.belowValue(value),
Ordering.natural(),
ANY_PRESENT,
Expand Down Expand Up @@ -451,7 +452,7 @@ private ImmutableList<Range<C>> intersectRanges(final Range<C> range) {
fromIndex =
SortedLists.binarySearch(
ranges,
Range.<C>upperBoundFn(),
Range::upperBound,
range.lowerBound,
KeyPresentBehavior.FIRST_AFTER,
KeyAbsentBehavior.NEXT_HIGHER);
Expand All @@ -464,7 +465,7 @@ private ImmutableList<Range<C>> intersectRanges(final Range<C> range) {
toIndex =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
Range::lowerBound,
range.upperBound,
KeyPresentBehavior.FIRST_PRESENT,
KeyAbsentBehavior.NEXT_HIGHER);
Expand Down
5 changes: 5 additions & 0 deletions android/guava/src/com/google/common/collect/ImmutableSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ static int chooseTableSize(int setSize) {
* @throws NullPointerException if any of {@code elements} is null
* @since 7.0 (source-compatible since 2.0)
*/
// This the best we could do to get copyOfEnumSet to compile in the mainline.
// The suppression also covers the cast to E[], discussed below.
// In the backport, we don't have those cases and thus don't need this suppression.
// We keep it to minimize diffs.
@SuppressWarnings("unchecked")
public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) {
/*
* TODO(lowasser): consider checking for ImmutableAsList here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,9 @@ Comparator<? super V> valueComparator() {
@GwtIncompatible // java serialization
@J2ktIncompatible
private static final class SetFieldSettersHolder {
static final Serialization.FieldSetter<ImmutableSetMultimap> EMPTY_SET_FIELD_SETTER =
Serialization.getFieldSetter(ImmutableSetMultimap.class, "emptySet");
static final Serialization.FieldSetter<? super ImmutableSetMultimap<?, ?>>
EMPTY_SET_FIELD_SETTER =
Serialization.getFieldSetter(ImmutableSetMultimap.class, "emptySet");
}

@GwtIncompatible // java.io.ObjectInputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public final class ImmutableSortedMap<K, V> extends ImmutableMap<K, V>
* TODO(kevinb): Confirm that ImmutableSortedMap is faster to construct and
* uses less memory than TreeMap; then say so in the class Javadoc.
*/
private static final Comparator<Comparable> NATURAL_ORDER = Ordering.natural();
private static final Comparator<?> NATURAL_ORDER = Ordering.natural();

private static final ImmutableSortedMap<Comparable, Object> NATURAL_EMPTY_MAP =
private static final ImmutableSortedMap<Comparable<?>, Object> NATURAL_EMPTY_MAP =
new ImmutableSortedMap<>(
ImmutableSortedSet.emptySet(Ordering.natural()), ImmutableList.<Object>of());

Expand Down Expand Up @@ -154,7 +154,6 @@ private static <K, V> ImmutableSortedMap<K, V> of(Comparator<? super K> comparat
*
* @throws IllegalArgumentException if the two keys are equal according to their natural ordering
*/
@SuppressWarnings("unchecked")
public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
K k1, V v1, K k2, V v2) {
return fromEntries(entryOf(k1, v1), entryOf(k2, v2));
Expand Down Expand Up @@ -238,7 +237,6 @@ public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
* @throws IllegalArgumentException if any two keys are equal according to their natural ordering
* @since 31.0
*/
@SuppressWarnings("unchecked")
public static <K extends Comparable<? super K>, V> ImmutableSortedMap<K, V> of(
K k1,
V v1,
Expand Down Expand Up @@ -740,23 +738,30 @@ public ImmutableSortedMap<K, V> build() {
* @since 31.0
*/
@Override
@SuppressWarnings("unchecked") // see inline comments
public ImmutableSortedMap<K, V> buildOrThrow() {
switch (size) {
case 0:
return emptyMap(comparator);
case 1:
// We're careful to put only K and V instances in.
// requireNonNull is safe because the first `size` elements have been filled in.
return of(comparator, (K) requireNonNull(keys[0]), (V) requireNonNull(values[0]));
ImmutableSortedMap<K, V> result =
of(comparator, (K) requireNonNull(keys[0]), (V) requireNonNull(values[0]));
return result;
default:
Object[] sortedKeys = Arrays.copyOf(keys, size);
Arrays.sort((K[]) sortedKeys, comparator);
// We're careful to put only K instances in.
K[] sortedKs = (K[]) sortedKeys;
Arrays.sort(sortedKs, comparator);
Object[] sortedValues = new Object[size];

// We might, somehow, be able to reorder values in-place. But it doesn't seem like
// there's a way around creating the separate sortedKeys array, and if we're allocating
// one array of size n, we might as well allocate two -- to say nothing of the allocation
// done in Arrays.sort.
for (int i = 0; i < size; i++) {
// We're careful to put only K instances in.
if (i > 0 && comparator.compare((K) sortedKeys[i - 1], (K) sortedKeys[i]) == 0) {
throw new IllegalArgumentException(
"keys required to be distinct but compared as equal: "
Expand All @@ -765,6 +770,7 @@ public ImmutableSortedMap<K, V> buildOrThrow() {
+ sortedKeys[i]);
}
// requireNonNull is safe because the first `size` elements have been filled in.
// We're careful to put only K instances in.
int index =
Arrays.binarySearch((K[]) sortedKeys, (K) requireNonNull(keys[i]), comparator);
sortedValues[index] = requireNonNull(values[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static <E> ImmutableSortedMultiset<E> copyOf(Iterable<? extends E> elemen
// Hack around E not being a subtype of Comparable.
// Unsafe, see ImmutableSortedMultisetFauxverideShim.
@SuppressWarnings("unchecked")
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable<?>>natural();
return copyOf(naturalOrder, elements);
}

Expand All @@ -250,7 +250,7 @@ public static <E> ImmutableSortedMultiset<E> copyOf(Iterator<? extends E> elemen
// Hack around E not being a subtype of Comparable.
// Unsafe, see ImmutableSortedMultisetFauxverideShim.
@SuppressWarnings("unchecked")
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable<?>>natural();
return copyOf(naturalOrder, elements);
}

Expand All @@ -276,7 +276,6 @@ public static <E> ImmutableSortedMultiset<E> copyOf(
*
* @throws NullPointerException if {@code comparator} or any of {@code elements} is null
*/
@SuppressWarnings("unchecked")
public static <E> ImmutableSortedMultiset<E> copyOf(
Comparator<? super E> comparator, Iterable<? extends E> elements) {
if (elements instanceof ImmutableSortedMultiset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public abstract class ImmutableSortedSet<E> extends ImmutableSet<E>

static <E> RegularImmutableSortedSet<E> emptySet(Comparator<? super E> comparator) {
if (Ordering.natural().equals(comparator)) {
return (RegularImmutableSortedSet<E>) RegularImmutableSortedSet.NATURAL_EMPTY_SET;
@SuppressWarnings("unchecked") // The natural-ordered empty set supports all types.
RegularImmutableSortedSet<E> result =
(RegularImmutableSortedSet<E>) RegularImmutableSortedSet.NATURAL_EMPTY_SET;
return result;
} else {
return new RegularImmutableSortedSet<E>(ImmutableList.<E>of(), comparator);
}
Expand All @@ -90,6 +93,7 @@ static <E> RegularImmutableSortedSet<E> emptySet(Comparator<? super E> comparato
*
* <p><b>Performance note:</b> the instance returned is a singleton.
*/
@SuppressWarnings("unchecked") // The natural-ordered empty set supports all types.
public static <E> ImmutableSortedSet<E> of() {
return (ImmutableSortedSet<E>) RegularImmutableSortedSet.NATURAL_EMPTY_SET;
}
Expand Down Expand Up @@ -117,7 +121,6 @@ public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E
*
* @throws NullPointerException if any element is null
*/
@SuppressWarnings("unchecked")
public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E e2, E e3) {
return construct(Ordering.natural(), 3, e1, e2, e3);
}
Expand All @@ -140,7 +143,6 @@ public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(E e1, E
*
* @throws NullPointerException if any element is null
*/
@SuppressWarnings("unchecked")
public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
E e1, E e2, E e3, E e4, E e5) {
return construct(Ordering.natural(), 5, e1, e2, e3, e4, e5);
Expand All @@ -157,7 +159,7 @@ public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
@SuppressWarnings("unchecked")
public static <E extends Comparable<? super E>> ImmutableSortedSet<E> of(
E e1, E e2, E e3, E e4, E e5, E e6, E... remaining) {
Comparable[] contents = new Comparable[6 + remaining.length];
Comparable<?>[] contents = new Comparable<?>[6 + remaining.length];
contents[0] = e1;
contents[1] = e2;
contents[2] = e3;
Expand Down Expand Up @@ -207,7 +209,7 @@ public static <E> ImmutableSortedSet<E> copyOf(Iterable<? extends E> elements) {
// Hack around E not being a subtype of Comparable.
// Unsafe, see ImmutableSortedSetFauxverideShim.
@SuppressWarnings("unchecked")
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable<?>>natural();
return copyOf(naturalOrder, elements);
}

Expand Down Expand Up @@ -239,7 +241,7 @@ public static <E> ImmutableSortedSet<E> copyOf(Collection<? extends E> elements)
// Hack around E not being a subtype of Comparable.
// Unsafe, see ImmutableSortedSetFauxverideShim.
@SuppressWarnings("unchecked")
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable<?>>natural();
return copyOf(naturalOrder, elements);
}

Expand All @@ -258,7 +260,7 @@ public static <E> ImmutableSortedSet<E> copyOf(Iterator<? extends E> elements) {
// Hack around E not being a subtype of Comparable.
// Unsafe, see ImmutableSortedSetFauxverideShim.
@SuppressWarnings("unchecked")
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable>natural();
Ordering<E> naturalOrder = (Ordering<E>) Ordering.<Comparable<?>>natural();
return copyOf(naturalOrder, elements);
}

Expand Down Expand Up @@ -436,6 +438,14 @@ public static final class Builder<E> extends ImmutableSet.Builder<E> {
* Creates a new builder. The returned builder is equivalent to the builder generated by {@link
* ImmutableSortedSet#orderedBy}.
*/
/*
* TODO(cpovirk): use Object[] instead of E[] in the mainline? (The backport is different and
* doesn't need this suppression, but we keep it to minimize diffs.) Generally be more clear
* about when we have an Object[] vs. a Comparable[] or other array type in internalArray? If we
* used Object[], we might be able to optimize toArray() to use clone() sometimes. (See
* cl/592273615 and cl/592273683.)
*/
@SuppressWarnings("unchecked")
public Builder(Comparator<? super E> comparator) {
this.comparator = checkNotNull(comparator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ public boolean add(@ParametricNullness V value) {
private void rehashIfNecessary() {
if (Hashing.needsResizing(size, hashTable.length, VALUE_SET_LOAD_FACTOR)) {
@SuppressWarnings("unchecked")
ValueEntry<K, V>[] hashTable = new ValueEntry[this.hashTable.length * 2];
ValueEntry<K, V>[] hashTable =
(ValueEntry<K, V>[]) new ValueEntry<?, ?>[this.hashTable.length * 2];
this.hashTable = hashTable;
int mask = hashTable.length - 1;
for (ValueSetLink<K, V> entry = firstEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ V getLiveValue(E entry) {

@SuppressWarnings("unchecked")
final Segment<K, V, E, S>[] newSegmentArray(int ssize) {
return new Segment[ssize];
return (Segment<K, V, E, S>[]) new Segment<?, ?, ?, ?>[ssize];
}

// Inner Classes
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/collect/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
*
* @return a new, empty {@code TreeMap}
*/
@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
public static <K extends Comparable, V extends @Nullable Object> TreeMap<K, V> newTreeMap() {
return new TreeMap<>();
}
Expand Down
Loading

0 comments on commit 7e06618

Please sign in to comment.