Skip to content

Commit

Permalink
Add some nullness annotations to MapMakerInternalMap.
Browse files Browse the repository at this point in the history
These were mostly return-type annotations identified by tools, but I made a few small additions myself.

RELNOTES=n/a
PiperOrigin-RevId: 531569431
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 12, 2023
1 parent bbaf76a commit 3da34f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ private StrongKeyWeakValueEntry(K key, int hash) {
}

@Override
@CheckForNull
public final V getValue() {
return valueReference.get();
}
Expand Down Expand Up @@ -1157,6 +1158,7 @@ Segment<K, V, E, S> createSegment(int initialCapacity) {
* Gets the value from an entry. Returns {@code null} if the entry is invalid, partially-collected
* or computing.
*/
@CheckForNull
V getLiveValue(E entry) {
if (entry.getKey() == null) {
return null;
Expand Down Expand Up @@ -1265,6 +1267,7 @@ void setValue(E entry, V value) {
}

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

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

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

// Specialized implementations of map methods

@CheckForNull
E getEntry(Object key, int hash) {
if (count != 0) { // read-volatile
for (E e = getFirst(hash); e != null; e = e.getNext()) {
Expand All @@ -1440,10 +1446,12 @@ E getEntry(Object key, int hash) {
return null;
}

@CheckForNull
E getLiveEntry(Object key, int hash) {
return getEntry(key, hash);
}

@CheckForNull
V get(Object key, int hash) {
try {
E e = getLiveEntry(key, hash);
Expand Down Expand Up @@ -1503,6 +1511,7 @@ boolean containsValue(Object value) {
}
}

@CheckForNull
V put(K key, int hash, V value, boolean onlyIfAbsent) {
lock();
try {
Expand Down Expand Up @@ -1676,6 +1685,7 @@ boolean replace(K key, int hash, V oldValue, V newValue) {
}
}

@CheckForNull
V replace(K key, int hash, V newValue) {
lock();
try {
Expand Down Expand Up @@ -1717,6 +1727,7 @@ V replace(K key, int hash, V newValue) {
}
}

@CheckForNull
@CanIgnoreReturnValue
V remove(Object key, int hash) {
lock();
Expand Down Expand Up @@ -1831,6 +1842,7 @@ void clear() {
* @return the new first entry for the table
*/
@GuardedBy("this")
@CheckForNull
E removeFromChain(E first, E entry) {
int newCount = count;
E newFirst = entry.getNext();
Expand Down Expand Up @@ -2043,7 +2055,9 @@ StrongKeyStrongValueSegment<K, V> self() {

@SuppressWarnings("unchecked")
@Override
public StrongKeyStrongValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public StrongKeyStrongValueEntry<K, V> castForTesting(
@CheckForNull InternalEntry<K, V, ?> entry) {
return (StrongKeyStrongValueEntry<K, V>) entry;
}
}
Expand Down Expand Up @@ -2072,7 +2086,9 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {

@SuppressWarnings("unchecked")
@Override
public StrongKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public StrongKeyWeakValueEntry<K, V> castForTesting(
@CheckForNull InternalEntry<K, V, ?> entry) {
return (StrongKeyWeakValueEntry<K, V>) entry;
}

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

@SuppressWarnings("unchecked")
@Override
public WeakKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public WeakKeyWeakValueEntry<K, V> castForTesting(@CheckForNull InternalEntry<K, V, ?> entry) {
return (WeakKeyWeakValueEntry<K, V>) entry;
}

Expand Down Expand Up @@ -2372,6 +2389,7 @@ public V get(@CheckForNull Object key) {
* Returns the internal entry for the specified key. The entry may be computing or partially
* collected. Does not impact recency ordering.
*/
@CheckForNull
E getEntry(@CheckForNull Object key) {
if (key == null) {
return null;
Expand Down
24 changes: 21 additions & 3 deletions guava/src/com/google/common/collect/MapMakerInternalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ private StrongKeyWeakValueEntry(K key, int hash) {
}

@Override
@CheckForNull
public final V getValue() {
return valueReference.get();
}
Expand Down Expand Up @@ -1157,6 +1158,7 @@ Segment<K, V, E, S> createSegment(int initialCapacity) {
* Gets the value from an entry. Returns {@code null} if the entry is invalid, partially-collected
* or computing.
*/
@CheckForNull
V getLiveValue(E entry) {
if (entry.getKey() == null) {
return null;
Expand Down Expand Up @@ -1265,6 +1267,7 @@ void setValue(E entry, V value) {
}

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

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

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

// Specialized implementations of map methods

@CheckForNull
E getEntry(Object key, int hash) {
if (count != 0) { // read-volatile
for (E e = getFirst(hash); e != null; e = e.getNext()) {
Expand All @@ -1440,10 +1446,12 @@ E getEntry(Object key, int hash) {
return null;
}

@CheckForNull
E getLiveEntry(Object key, int hash) {
return getEntry(key, hash);
}

@CheckForNull
V get(Object key, int hash) {
try {
E e = getLiveEntry(key, hash);
Expand Down Expand Up @@ -1503,6 +1511,7 @@ boolean containsValue(Object value) {
}
}

@CheckForNull
V put(K key, int hash, V value, boolean onlyIfAbsent) {
lock();
try {
Expand Down Expand Up @@ -1676,6 +1685,7 @@ boolean replace(K key, int hash, V oldValue, V newValue) {
}
}

@CheckForNull
V replace(K key, int hash, V newValue) {
lock();
try {
Expand Down Expand Up @@ -1717,6 +1727,7 @@ V replace(K key, int hash, V newValue) {
}
}

@CheckForNull
@CanIgnoreReturnValue
V remove(Object key, int hash) {
lock();
Expand Down Expand Up @@ -1831,6 +1842,7 @@ void clear() {
* @return the new first entry for the table
*/
@GuardedBy("this")
@CheckForNull
E removeFromChain(E first, E entry) {
int newCount = count;
E newFirst = entry.getNext();
Expand Down Expand Up @@ -2043,7 +2055,9 @@ StrongKeyStrongValueSegment<K, V> self() {

@SuppressWarnings("unchecked")
@Override
public StrongKeyStrongValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public StrongKeyStrongValueEntry<K, V> castForTesting(
@CheckForNull InternalEntry<K, V, ?> entry) {
return (StrongKeyStrongValueEntry<K, V>) entry;
}
}
Expand Down Expand Up @@ -2072,7 +2086,9 @@ ReferenceQueue<V> getValueReferenceQueueForTesting() {

@SuppressWarnings("unchecked")
@Override
public StrongKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public StrongKeyWeakValueEntry<K, V> castForTesting(
@CheckForNull InternalEntry<K, V, ?> entry) {
return (StrongKeyWeakValueEntry<K, V>) entry;
}

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

@SuppressWarnings("unchecked")
@Override
public WeakKeyWeakValueEntry<K, V> castForTesting(InternalEntry<K, V, ?> entry) {
@CheckForNull
public WeakKeyWeakValueEntry<K, V> castForTesting(@CheckForNull InternalEntry<K, V, ?> entry) {
return (WeakKeyWeakValueEntry<K, V>) entry;
}

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

0 comments on commit 3da34f9

Please sign in to comment.