Skip to content

Commit

Permalink
Remove unnecessary nullness annotations (fixes #337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jan 18, 2021
1 parent 9937263 commit 65e4e4d
Show file tree
Hide file tree
Showing 42 changed files with 203 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.github.benmanes.caffeine.cache;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -26,13 +25,13 @@
public interface BasicCache<K, V> {

/** Returns the value stored in the cache, or null if not present. */
@Nullable V get(@NonNull K key);
@Nullable V get(K key);

/** Stores the value into the cache, replacing an existing mapping if present. */
void put(@NonNull K key, @NonNull V value);
void put(K key, V value);

/** Removes the entry from the cache, if present. */
void remove(@NonNull K key);
void remove(K key);

/** Invalidates all entries from the cache. */
void clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand All @@ -50,7 +49,7 @@ public interface AsyncCache<K, V> {
* @throws NullPointerException if the specified key is null
*/
@Nullable
CompletableFuture<V> getIfPresent(@NonNull K key);
CompletableFuture<V> getIfPresent(K key);

/**
* Returns the future associated with {@code key} in this cache, obtaining that value from
Expand All @@ -70,9 +69,7 @@ public interface AsyncCache<K, V> {
* @return the current (existing or computed) future value associated with the specified key
* @throws NullPointerException if the specified key or mappingFunction is null
*/
@NonNull
CompletableFuture<V> get(@NonNull K key,
@NonNull Function<? super K, ? extends V> mappingFunction);
CompletableFuture<V> get(K key, Function<? super K, ? extends V> mappingFunction);

/**
* Returns the future associated with {@code key} in this cache, obtaining that value from
Expand All @@ -95,9 +92,8 @@ CompletableFuture<V> get(@NonNull K key,
* @throws RuntimeException or Error if the mappingFunction does when constructing the future,
* in which case the mapping is left unestablished
*/
@NonNull
CompletableFuture<V> get(@NonNull K key,
@NonNull BiFunction<? super K, Executor, CompletableFuture<V>> mappingFunction);
CompletableFuture<V> get(K key,
BiFunction<? super K, Executor, CompletableFuture<V>> mappingFunction);

/**
* Returns the future of a map of the values associated with {@code keys}, creating or retrieving
Expand All @@ -122,9 +118,8 @@ CompletableFuture<V> get(@NonNull K key,
* @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
* left unestablished
*/
@NonNull
CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys,
@NonNull Function<Set<? extends @NonNull K>, @NonNull Map<K, V>> mappingFunction);
CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys,
Function<Set<? extends K>, Map<K, V>> mappingFunction);

/**
* Returns the future of a map of the values associated with {@code keys}, creating or retrieving
Expand All @@ -149,9 +144,8 @@ CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys
* @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
* left unestablished
*/
@NonNull
CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys,
@NonNull BiFunction<Set<? extends @NonNull K>, Executor, CompletableFuture<Map<K, V>>> mappingFunction);
CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys,
BiFunction<Set<? extends K>, Executor, CompletableFuture<Map<K, V>>> mappingFunction);

/**
* Associates {@code value} with {@code key} in this cache. If the cache previously contained a
Expand All @@ -165,7 +159,7 @@ CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys
* @param valueFuture value to be associated with the specified key
* @throws NullPointerException if the specified key or value is null
*/
void put(@NonNull K key, @NonNull CompletableFuture<V> valueFuture);
void put(K key, CompletableFuture<V> valueFuture);

/**
* Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to
Expand All @@ -182,8 +176,7 @@ CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys
*
* @return a thread-safe view of this cache supporting all of the optional {@link Map} operations
*/
@NonNull
ConcurrentMap<@NonNull K, @NonNull CompletableFuture<V>> asMap();
ConcurrentMap<K, CompletableFuture<V>> asMap();

/**
* Returns a view of the entries stored in this cache as a synchronous {@link Cache}. A mapping is
Expand All @@ -193,6 +186,5 @@ CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys
*
* @return a thread-safe synchronous view of this cache
*/
@NonNull
Cache<K, V> synchronous();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Computes or retrieves values asynchronously, based on a key, for use in populating a
* {@link AsyncLoadingCache}.
Expand Down Expand Up @@ -57,8 +55,7 @@ public interface AsyncCacheLoader<K, V> {
* treated like any other {@code Exception} in all respects except that, when it is
* caught, the thread's interrupt status is set
*/
@NonNull
CompletableFuture<V> asyncLoad(@NonNull K key, @NonNull Executor executor) throws Exception;
CompletableFuture<V> asyncLoad(K key, Executor executor) throws Exception;

/**
* Asynchronously computes or retrieves the values corresponding to {@code keys}. This method is
Expand All @@ -82,9 +79,8 @@ public interface AsyncCacheLoader<K, V> {
* treated like any other {@code Exception} in all respects except that, when it is
* caught, the thread's interrupt status is set
*/
@NonNull
default CompletableFuture<Map<@NonNull K, @NonNull V>> asyncLoadAll(
@NonNull Set<? extends @NonNull K> keys, @NonNull Executor executor) throws Exception {
default CompletableFuture<Map<K, V>> asyncLoadAll(
Set<? extends K> keys, Executor executor) throws Exception {
throw new UnsupportedOperationException();
}

Expand All @@ -106,9 +102,7 @@ public interface AsyncCacheLoader<K, V> {
* treated like any other {@code Exception} in all respects except that, when it is
* caught, the thread's interrupt status is set
*/
@NonNull
default CompletableFuture<V> asyncReload(
@NonNull K key, @NonNull V oldValue, @NonNull Executor executor) throws Exception {
default CompletableFuture<V> asyncReload(K key, V oldValue, Executor executor) throws Exception {
return asyncLoad(key, executor);
}

Expand All @@ -128,7 +122,6 @@ default CompletableFuture<V> asyncReload(
* @return an asynchronous cache loader that delegates to the supplied {@code mappingFunction}
* @throws NullPointerException if the mappingFunction is null
*/
@NonNull
static <K, V> AsyncCacheLoader<K, V> bulk(Function<Set<? extends K>, Map<K, V>> mappingFunction) {
return CacheLoader.bulk(mappingFunction);
}
Expand All @@ -149,7 +142,6 @@ static <K, V> AsyncCacheLoader<K, V> bulk(Function<Set<? extends K>, Map<K, V>>
* @return an asynchronous cache loader that delegates to the supplied {@code mappingFunction}
* @throws NullPointerException if the mappingFunction is null
*/
@NonNull
static <K, V> AsyncCacheLoader<K, V> bulk(
BiFunction<Set<? extends K>, Executor, CompletableFuture<Map<K, V>>> mappingFunction) {
requireNonNull(mappingFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* A semi-persistent mapping from keys to values. Values are automatically loaded by the cache
* asynchronously, and are stored in the cache until either evicted or manually invalidated.
Expand Down Expand Up @@ -49,8 +47,7 @@ public interface AsyncLoadingCache<K, V> extends AsyncCache<K, V> {
* @throws RuntimeException or Error if the {@link AsyncCacheLoader} does when constructing the
* future, in which case the mapping is left unestablished
*/
@NonNull
CompletableFuture<V> get(@NonNull K key);
CompletableFuture<V> get(K key);

/**
* Returns the future of a map of the values associated with {@code keys}, creating or retrieving
Expand Down Expand Up @@ -78,8 +75,7 @@ public interface AsyncLoadingCache<K, V> extends AsyncCache<K, V> {
* {@link AsyncCacheLoader#asyncLoadAll} returns {@code null}, or fails when constructing
* the future, in which case the mapping is left unestablished
*/
@NonNull
CompletableFuture<Map<K, V>> getAll(@NonNull Iterable<? extends @NonNull K> keys);
CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys);

/**
* Returns a view of the entries stored in this cache as a synchronous {@link LoadingCache}. A
Expand All @@ -89,7 +85,6 @@ public interface AsyncLoadingCache<K, V> extends AsyncCache<K, V> {
*
* @return a thread-safe synchronous view of this cache
*/
@NonNull
@Override
LoadingCache<K, V> synchronous();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import com.github.benmanes.caffeine.cache.Async.AsyncExpiry;
Expand Down Expand Up @@ -752,13 +751,13 @@ void evictFromMain(int candidates) {
K victimKey = victim.getKey();
K candidateKey = candidate.getKey();
if (victimKey == null) {
@NonNull Node<K, V> evict = victim;
Node<K, V> evict = victim;
victim = victim.getNextInAccessOrder();
evictEntry(evict, RemovalCause.COLLECTED, 0L);
continue;
} else if (candidateKey == null) {
candidates--;
@NonNull Node<K, V> evict = candidate;
Node<K, V> evict = candidate;
candidate = candidate.getPreviousInAccessOrder();
evictEntry(evict, RemovalCause.COLLECTED, 0L);
continue;
Expand Down Expand Up @@ -3529,7 +3528,7 @@ final class BoundedEviction implements Eviction<K, V> {
@Override public boolean isWeighted() {
return isWeighted;
}
@Override public OptionalInt weightOf(@NonNull K key) {
@Override public OptionalInt weightOf(K key) {
requireNonNull(key);
if (!isWeighted) {
return OptionalInt.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import java.util.function.Consumer;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* A multiple-producer / single-consumer buffer that rejects new elements if it is full or
* fails spuriously due to contention. Unlike a queue and stack, a buffer does not guarantee an
Expand Down Expand Up @@ -50,15 +48,15 @@ static <E> Buffer<E> disabled() {
* @param e the element to add
* @return {@code 1} if the buffer is full, {@code -1} if the CAS failed, or {@code 0} if added
*/
int offer(@NonNull E e);
int offer(E e);

/**
* Drains the buffer, sending each element to the consumer for processing. The caller must ensure
* that a consumer has exclusive read access to the buffer.
*
* @param consumer the action to perform on each element
*/
void drainTo(@NonNull Consumer<E> consumer);
void drainTo(Consumer<E> consumer);

/**
* Returns the number of elements residing in the buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.function.Function;

import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.PolyNull;

import com.github.benmanes.caffeine.cache.stats.CacheStats;

Expand Down Expand Up @@ -50,7 +50,7 @@ public interface Cache<K, V> {
* @throws NullPointerException if the specified key is null
*/
@Nullable
V getIfPresent(@NonNull K key);
V getIfPresent(K key);

/**
* Returns the value associated with the {@code key} in this cache, obtaining that value from the
Expand All @@ -77,8 +77,8 @@ public interface Cache<K, V> {
* @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
* left unestablished
*/
@Nullable
V get(@NonNull K key, @NonNull Function<? super K, ? extends V> mappingFunction);
@PolyNull
V get(K key, Function<? super K, ? extends @PolyNull V> mappingFunction);

/**
* Returns a map of the values associated with the {@code keys} in this cache. The returned map
Expand All @@ -91,8 +91,7 @@ public interface Cache<K, V> {
* @return the unmodifiable mapping of keys to values for the specified keys found in this cache
* @throws NullPointerException if the specified collection is null or contains a null element
*/
@NonNull
Map<@NonNull K, @NonNull V> getAllPresent(@NonNull Iterable<? extends K> keys);
Map<K, V> getAllPresent(Iterable<? extends K> keys);

/**
* Returns a map of the values associated with the {@code keys}, creating or retrieving those
Expand All @@ -118,9 +117,8 @@ public interface Cache<K, V> {
* @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is
* left unestablished
*/
@NonNull
Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
@NonNull Function<Set<? extends @NonNull K>, @NonNull Map<K, V>> mappingFunction);
Map<K, V> getAll(Iterable<? extends K> keys,
Function<Set<? extends K>, Map<K, V>> mappingFunction);

/**
* Associates the {@code value} with the {@code key} in this cache. If the cache previously
Expand All @@ -134,7 +132,7 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
* @param value value to be associated with the specified key
* @throws NullPointerException if the specified key or value is null
*/
void put(@NonNull K key, @NonNull V value);
void put(K key, V value);

/**
* Copies all of the mappings from the specified map to the cache. The effect of this call is
Expand All @@ -146,7 +144,7 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
* @throws NullPointerException if the specified map is null or the specified map contains null
* keys or values
*/
void putAll(@NonNull Map<? extends @NonNull K,? extends @NonNull V> map);
void putAll(Map<? extends K,? extends V> map);

/**
* Discards any cached value for the {@code key}. The behavior of this operation is undefined for
Expand All @@ -155,7 +153,7 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
* @param key the key whose mapping is to be removed from the cache
* @throws NullPointerException if the specified key is null
*/
void invalidate(@NonNull K key);
void invalidate(K key);

/**
* Discards any cached values for the {@code keys}. The behavior of this operation is undefined
Expand All @@ -164,7 +162,7 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
* @param keys the keys whose associated values are to be removed
* @throws NullPointerException if the specified collection is null or contains a null element
*/
void invalidateAll(@NonNull Iterable<? extends @NonNull K> keys);
void invalidateAll(Iterable<? extends K> keys);

/**
* Discards all entries in the cache. The behavior of this operation is undefined for an entry
Expand Down Expand Up @@ -192,7 +190,6 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
*
* @return the current snapshot of the statistics of this cache
*/
@NonNull
CacheStats stats();

/**
Expand All @@ -210,8 +207,7 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
*
* @return a thread-safe view of this cache supporting all of the optional {@link Map} operations
*/
@NonNull
ConcurrentMap<@NonNull K, @NonNull V> asMap();
ConcurrentMap<K, V> asMap();

/**
* Performs any pending maintenance operations needed by the cache. Exactly which activities are
Expand All @@ -226,6 +222,5 @@ Map<K, V> getAll(@NonNull Iterable<? extends @NonNull K> keys,
*
* @return access to inspect and perform advanced operations based on the cache's characteristics
*/
@NonNull
Policy<K, V> policy();
}
Loading

0 comments on commit 65e4e4d

Please sign in to comment.