Skip to content

Commit 3da34f9

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Add some nullness annotations to MapMakerInternalMap.
These were mostly return-type annotations identified by tools, but I made a few small additions myself. RELNOTES=n/a PiperOrigin-RevId: 531569431
1 parent bbaf76a commit 3da34f9

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

android/guava/src/com/google/common/collect/MapMakerInternalMap.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ private StrongKeyWeakValueEntry(K key, int hash) {
488488
}
489489

490490
@Override
491+
@CheckForNull
491492
public final V getValue() {
492493
return valueReference.get();
493494
}
@@ -1157,6 +1158,7 @@ Segment<K, V, E, S> createSegment(int initialCapacity) {
11571158
* Gets the value from an entry. Returns {@code null} if the entry is invalid, partially-collected
11581159
* or computing.
11591160
*/
1161+
@CheckForNull
11601162
V getLiveValue(E entry) {
11611163
if (entry.getKey() == null) {
11621164
return null;
@@ -1265,6 +1267,7 @@ void setValue(E entry, V value) {
12651267
}
12661268

12671269
/** Returns a copy of the given {@code entry}. */
1270+
@CheckForNull
12681271
E copyEntry(E original, E newNext) {
12691272
return this.map.entryHelper.copy(self(), original, newNext);
12701273
}
@@ -1352,6 +1355,7 @@ boolean removeTableEntryForTesting(InternalEntry<K, V, ?> entry) {
13521355
}
13531356

13541357
/** Unsafely removes the given entry from the given chain in this segment's hash table. */
1358+
@CheckForNull
13551359
E removeFromChainForTesting(InternalEntry<K, V, ?> first, InternalEntry<K, V, ?> entry) {
13561360
return removeFromChain(castForTesting(first), castForTesting(entry));
13571361
}
@@ -1410,6 +1414,7 @@ <T> void clearReferenceQueue(ReferenceQueue<T> referenceQueue) {
14101414
}
14111415

14121416
/** Returns first entry of bin for given hash. */
1417+
@CheckForNull
14131418
E getFirst(int hash) {
14141419
// read this volatile field only once
14151420
AtomicReferenceArray<E> table = this.table;
@@ -1418,6 +1423,7 @@ E getFirst(int hash) {
14181423

14191424
// Specialized implementations of map methods
14201425

1426+
@CheckForNull
14211427
E getEntry(Object key, int hash) {
14221428
if (count != 0) { // read-volatile
14231429
for (E e = getFirst(hash); e != null; e = e.getNext()) {
@@ -1440,10 +1446,12 @@ E getEntry(Object key, int hash) {
14401446
return null;
14411447
}
14421448

1449+
@CheckForNull
14431450
E getLiveEntry(Object key, int hash) {
14441451
return getEntry(key, hash);
14451452
}
14461453

1454+
@CheckForNull
14471455
V get(Object key, int hash) {
14481456
try {
14491457
E e = getLiveEntry(key, hash);
@@ -1503,6 +1511,7 @@ boolean containsValue(Object value) {
15031511
}
15041512
}
15051513

1514+
@CheckForNull
15061515
V put(K key, int hash, V value, boolean onlyIfAbsent) {
15071516
lock();
15081517
try {
@@ -1676,6 +1685,7 @@ boolean replace(K key, int hash, V oldValue, V newValue) {
16761685
}
16771686
}
16781687

1688+
@CheckForNull
16791689
V replace(K key, int hash, V newValue) {
16801690
lock();
16811691
try {
@@ -1717,6 +1727,7 @@ V replace(K key, int hash, V newValue) {
17171727
}
17181728
}
17191729

1730+
@CheckForNull
17201731
@CanIgnoreReturnValue
17211732
V remove(Object key, int hash) {
17221733
lock();
@@ -1831,6 +1842,7 @@ void clear() {
18311842
* @return the new first entry for the table
18321843
*/
18331844
@GuardedBy("this")
1845+
@CheckForNull
18341846
E removeFromChain(E first, E entry) {
18351847
int newCount = count;
18361848
E newFirst = entry.getNext();
@@ -2043,7 +2055,9 @@ StrongKeyStrongValueSegment<K, V> self() {
20432055

20442056
@SuppressWarnings("unchecked")
20452057
@Override
2046-
public StrongKeyStrongValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2058+
@CheckForNull
2059+
public StrongKeyStrongValueEntry<K, V> castForTesting(
2060+
@CheckForNull InternalEntry<K, V, ?> entry) {
20472061
return (StrongKeyStrongValueEntry<K, V>) entry;
20482062
}
20492063
}
@@ -2072,7 +2086,9 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {
20722086

20732087
@SuppressWarnings("unchecked")
20742088
@Override
2075-
public StrongKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2089+
@CheckForNull
2090+
public StrongKeyWeakValueEntry<K, V> castForTesting(
2091+
@CheckForNull InternalEntry<K, V, ?> entry) {
20762092
return (StrongKeyWeakValueEntry<K, V>) entry;
20772093
}
20782094

@@ -2202,7 +2218,8 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {
22022218

22032219
@SuppressWarnings("unchecked")
22042220
@Override
2205-
public WeakKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2221+
@CheckForNull
2222+
public WeakKeyWeakValueEntry<K, V> castForTesting(@CheckForNull InternalEntry<K, V, ?> entry) {
22062223
return (WeakKeyWeakValueEntry<K, V>) entry;
22072224
}
22082225

@@ -2372,6 +2389,7 @@ public V get(@CheckForNull Object key) {
23722389
* Returns the internal entry for the specified key. The entry may be computing or partially
23732390
* collected. Does not impact recency ordering.
23742391
*/
2392+
@CheckForNull
23752393
E getEntry(@CheckForNull Object key) {
23762394
if (key == null) {
23772395
return null;

guava/src/com/google/common/collect/MapMakerInternalMap.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ private StrongKeyWeakValueEntry(K key, int hash) {
488488
}
489489

490490
@Override
491+
@CheckForNull
491492
public final V getValue() {
492493
return valueReference.get();
493494
}
@@ -1157,6 +1158,7 @@ Segment<K, V, E, S> createSegment(int initialCapacity) {
11571158
* Gets the value from an entry. Returns {@code null} if the entry is invalid, partially-collected
11581159
* or computing.
11591160
*/
1161+
@CheckForNull
11601162
V getLiveValue(E entry) {
11611163
if (entry.getKey() == null) {
11621164
return null;
@@ -1265,6 +1267,7 @@ void setValue(E entry, V value) {
12651267
}
12661268

12671269
/** Returns a copy of the given {@code entry}. */
1270+
@CheckForNull
12681271
E copyEntry(E original, E newNext) {
12691272
return this.map.entryHelper.copy(self(), original, newNext);
12701273
}
@@ -1352,6 +1355,7 @@ boolean removeTableEntryForTesting(InternalEntry<K, V, ?> entry) {
13521355
}
13531356

13541357
/** Unsafely removes the given entry from the given chain in this segment's hash table. */
1358+
@CheckForNull
13551359
E removeFromChainForTesting(InternalEntry<K, V, ?> first, InternalEntry<K, V, ?> entry) {
13561360
return removeFromChain(castForTesting(first), castForTesting(entry));
13571361
}
@@ -1410,6 +1414,7 @@ <T> void clearReferenceQueue(ReferenceQueue<T> referenceQueue) {
14101414
}
14111415

14121416
/** Returns first entry of bin for given hash. */
1417+
@CheckForNull
14131418
E getFirst(int hash) {
14141419
// read this volatile field only once
14151420
AtomicReferenceArray<E> table = this.table;
@@ -1418,6 +1423,7 @@ E getFirst(int hash) {
14181423

14191424
// Specialized implementations of map methods
14201425

1426+
@CheckForNull
14211427
E getEntry(Object key, int hash) {
14221428
if (count != 0) { // read-volatile
14231429
for (E e = getFirst(hash); e != null; e = e.getNext()) {
@@ -1440,10 +1446,12 @@ E getEntry(Object key, int hash) {
14401446
return null;
14411447
}
14421448

1449+
@CheckForNull
14431450
E getLiveEntry(Object key, int hash) {
14441451
return getEntry(key, hash);
14451452
}
14461453

1454+
@CheckForNull
14471455
V get(Object key, int hash) {
14481456
try {
14491457
E e = getLiveEntry(key, hash);
@@ -1503,6 +1511,7 @@ boolean containsValue(Object value) {
15031511
}
15041512
}
15051513

1514+
@CheckForNull
15061515
V put(K key, int hash, V value, boolean onlyIfAbsent) {
15071516
lock();
15081517
try {
@@ -1676,6 +1685,7 @@ boolean replace(K key, int hash, V oldValue, V newValue) {
16761685
}
16771686
}
16781687

1688+
@CheckForNull
16791689
V replace(K key, int hash, V newValue) {
16801690
lock();
16811691
try {
@@ -1717,6 +1727,7 @@ V replace(K key, int hash, V newValue) {
17171727
}
17181728
}
17191729

1730+
@CheckForNull
17201731
@CanIgnoreReturnValue
17211732
V remove(Object key, int hash) {
17221733
lock();
@@ -1831,6 +1842,7 @@ void clear() {
18311842
* @return the new first entry for the table
18321843
*/
18331844
@GuardedBy("this")
1845+
@CheckForNull
18341846
E removeFromChain(E first, E entry) {
18351847
int newCount = count;
18361848
E newFirst = entry.getNext();
@@ -2043,7 +2055,9 @@ StrongKeyStrongValueSegment<K, V> self() {
20432055

20442056
@SuppressWarnings("unchecked")
20452057
@Override
2046-
public StrongKeyStrongValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2058+
@CheckForNull
2059+
public StrongKeyStrongValueEntry<K, V> castForTesting(
2060+
@CheckForNull InternalEntry<K, V, ?> entry) {
20472061
return (StrongKeyStrongValueEntry<K, V>) entry;
20482062
}
20492063
}
@@ -2072,7 +2086,9 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {
20722086

20732087
@SuppressWarnings("unchecked")
20742088
@Override
2075-
public StrongKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2089+
@CheckForNull
2090+
public StrongKeyWeakValueEntry<K, V> castForTesting(
2091+
@CheckForNull InternalEntry<K, V, ?> entry) {
20762092
return (StrongKeyWeakValueEntry<K, V>) entry;
20772093
}
20782094

@@ -2202,7 +2218,8 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {
22022218

22032219
@SuppressWarnings("unchecked")
22042220
@Override
2205-
public WeakKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
2221+
@CheckForNull
2222+
public WeakKeyWeakValueEntry<K, V> castForTesting(@CheckForNull InternalEntry<K, V, ?> entry) {
22062223
return (WeakKeyWeakValueEntry<K, V>) entry;
22072224
}
22082225

@@ -2372,6 +2389,7 @@ public V get(@CheckForNull Object key) {
23722389
* Returns the internal entry for the specified key. The entry may be computing or partially
23732390
* collected. Does not impact recency ordering.
23742391
*/
2392+
@CheckForNull
23752393
E getEntry(@CheckForNull Object key) {
23762394
if (key == null) {
23772395
return null;

0 commit comments

Comments
 (0)