Skip to content

Commit

Permalink
Prefer tighter generic bounds on invalidate methods
Browse files Browse the repository at this point in the history
This mirrors the change for query methods in that recent commit.
  • Loading branch information
ben-manes committed Jan 16, 2021
1 parent ab8394c commit 72d8ac9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.checkerframework.checker.nullness.qual.Nullable;

import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.google.errorprone.annotations.CompatibleWith;

/**
* A semi-persistent mapping from keys to values. Cache entries are manually added using
Expand Down Expand Up @@ -156,7 +155,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 @CompatibleWith("K") Object key);
void invalidate(@NonNull K key);

/**
* Discards any cached values for the {@code keys}. The behavior of this operation is undefined
Expand All @@ -165,7 +164,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<@NonNull ?> keys);
void invalidateAll(@NonNull Iterable<? extends @NonNull K> keys);

/**
* Discards all entries in the cache. The behavior of this operation is undefined for an entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ public void putAll(Map<? extends K, ? extends V> map) {
}

@Override
public void invalidate(Object key) {
public void invalidate(K key) {
asyncCache().cache().remove(key);
}

@Override
public void invalidateAll(Iterable<?> keys) {
public void invalidateAll(Iterable<? extends K> keys) {
asyncCache().cache().invalidateAll(keys);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ default void putAll(Map<? extends K, ? extends V> map) {
}

@Override
default void invalidate(Object key) {
default void invalidate(K key) {
cache().remove(key);
}

@Override
default void invalidateAll(Iterable<?> keys) {
default void invalidateAll(Iterable<? extends K> keys) {
cache().invalidateAll(keys);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@ public void putAll(Map<? extends K, ? extends V> map) {
}

@Override
public void invalidate(Object key) {
public void invalidate(K key) {
cache.invalidate(key);
}

@Override
public void invalidateAll(Iterable<?> keys) {
public void invalidateAll(Iterable<? extends K> keys) {
keys.forEach(this::invalidate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ public void putAll(Map<? extends K,? extends V> m) {

@Override
public void invalidate(Object key) {
cache.invalidate(key);
@SuppressWarnings("unchecked")
K castedKey = (K) key;
cache.invalidate(castedKey);
}

@Override
public void invalidateAll(Iterable<?> keys) {
cache.invalidateAll(keys);
@SuppressWarnings("unchecked")
var castedKeys = (Iterable<? extends K>) keys;
cache.invalidateAll(castedKeys);
}

@Override
Expand Down

0 comments on commit 72d8ac9

Please sign in to comment.