Skip to content

Commit

Permalink
Forward default methods in Guava's asMap() view
Browse files Browse the repository at this point in the history
Guava's ForwardingConcurrentMap, et. al. inherit the default versions
that are thread-safe but not atomic. These should be explicitly
forwarded, though users are encouraged to not rely on these until Guava
is Java 8 based. Doing so would encourage improper use of Guava's cache.
  • Loading branch information
ben-manes committed Mar 16, 2016
1 parent 18fe743 commit 80ea370
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private CaffeinatedGuava() {}
@Nonnull
public static <K1 extends K, K, V1 extends V, V> Cache<K1, V1> build(
@Nonnull Caffeine<K, V> builder) {
return new CaffeinatedGuavaCache<K1, V1>(builder.build());
return new CaffeinatedGuavaCache<>(builder.build());
}

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ public static <K1 extends K, K, V1 extends V, V> LoadingCache<K1, V1> build(
public static <K1 extends K, K, V1 extends V, V> LoadingCache<K1, V1> build(
@Nonnull Caffeine<K, V> builder,
@Nonnull com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1> loader) {
return new CaffeinatedGuavaLoadingCache<K1, V1>(builder.build(loader));
return new CaffeinatedGuavaLoadingCache<>(builder.build(loader));
}

static boolean hasLoadAll(CacheLoader<?, ?> cacheLoader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -136,12 +139,32 @@ public ConcurrentMap<K, V> asMap() {
@Override public boolean containsKey(Object key) {
return (key != null) && delegate().containsKey(key);
}
@Override
public boolean containsValue(Object value) {
@Override public boolean containsValue(Object value) {
return (value != null) && delegate().containsValue(value);
}
@Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
delegate().replaceAll(function);
}
@Override public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
return delegate().computeIfAbsent(key, mappingFunction);
}
@Override public V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return delegate().computeIfPresent(key, remappingFunction);
}
@Override public V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return delegate().compute(key, remappingFunction);
}
@Override public V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
return delegate().merge(key, value, remappingFunction);
}
@Override public Set<K> keySet() {
return new ForwardingSet<K>() {
@Override public boolean removeIf(Predicate<? super K> filter) {
return delegate().removeIf(filter);
}
@Override public boolean remove(Object o) {
return (o != null) && delegate().remove(o);
}
Expand All @@ -152,6 +175,9 @@ public boolean containsValue(Object value) {
}
@Override public Collection<V> values() {
return new ForwardingCollection<V>() {
@Override public boolean removeIf(Predicate<? super V> filter) {
return delegate().removeIf(filter);
}
@Override public boolean remove(Object o) {
return (o != null) && delegate().remove(o);
}
Expand All @@ -168,6 +194,9 @@ public boolean containsValue(Object value) {
@Override public boolean addAll(Collection<? extends Entry<K, V>> entry) {
throw new UnsupportedOperationException();
}
@Override public boolean removeIf(Predicate<? super Entry<K, V>> filter) {
return delegate().removeIf(filter);
}
@Override
public Iterator<Entry<K, V>> iterator() {
return delegate().stream().map(entry -> {
Expand Down

0 comments on commit 80ea370

Please sign in to comment.