Skip to content

Commit

Permalink
don't unnesessarily constrain type of getIfPresent and getAllPresent …
Browse files Browse the repository at this point in the history
…parameters

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=28374192
  • Loading branch information
kluever committed Mar 14, 2012
1 parent 1678fdb commit 9eed063
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected LocalManualCache(CacheBuilder<? super K, ? super V> builder,

@Override
@Nullable
public V getIfPresent(K key) {
public V getIfPresent(Object key) {
return localCache.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testInvalidateAll() {
final List<Object> invalidated = Lists.newArrayList();
Cache<Integer, Integer> cache = new AbstractCache<Integer, Integer>() {
@Override
public Integer getIfPresent(Integer key) {
public Integer getIfPresent(Object key) {
throw new UnsupportedOperationException();
}

Expand Down
14 changes: 11 additions & 3 deletions guava/src/com/google/common/cache/AbstractCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException
}

/**
* This implementation of {@code getAllPresent} lacks any insight into the internal cache data
* structure, and is thus forced to return the query keys instead of the cached keys. This is only
* possible with an unsafe cast which requires {@code keys} to actually be of type {@code K}.
*
* {@inheritDoc}
*
* @since 11.0
*/
@Override
public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
Map<K, V> result = Maps.newLinkedHashMap();
for (K key : keys) {
for (Object key : keys) {
if (!result.containsKey(key)) {
result.put(key, getIfPresent(key));
@SuppressWarnings("unchecked")
K castKey = (K) key;
result.put(castKey, getIfPresent(key));
}
}
return ImmutableMap.copyOf(result);
Expand Down
4 changes: 2 additions & 2 deletions guava/src/com/google/common/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface Cache<K, V> {
* @since 11.0
*/
@Nullable
V getIfPresent(K key);
V getIfPresent(Object key);

/**
* Returns the value associated with {@code key} in this cache, obtaining that value from
Expand All @@ -82,7 +82,7 @@ public interface Cache<K, V> {
*
* @since 11.0
*/
ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys);
ImmutableMap<K, V> getAllPresent(Iterable<?> keys);

/**
* Associates {@code value} with {@code key} in this cache. If the cache previously contained a
Expand Down
4 changes: 2 additions & 2 deletions guava/src/com/google/common/cache/ForwardingCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected ForwardingCache() {}
*/
@Override
@Nullable
public V getIfPresent(K key) {
public V getIfPresent(Object key) {
return delegate().getIfPresent(key);
}

Expand All @@ -66,7 +66,7 @@ public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException
* @since 11.0
*/
@Override
public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
return delegate().getAllPresent(keys);
}

Expand Down
16 changes: 9 additions & 7 deletions guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3969,17 +3969,20 @@ V getOrLoad(K key) throws ExecutionException {
return get(key, defaultLoader);
}

ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
int hits = 0;
int misses = 0;

Map<K, V> result = Maps.newLinkedHashMap();
for (K key : keys) {
for (Object key : keys) {
V value = get(key);
if (value == null) {
misses++;
} else {
result.put(key, value);
// TODO(fry): store entry key instead of query key
@SuppressWarnings("unchecked")
K castKey = (K) key;
result.put(castKey, value);
hits++;
}
}
Expand All @@ -3988,8 +3991,7 @@ ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
return ImmutableMap.copyOf(result);
}

ImmutableMap<K, V> getAll(Iterable<? extends K> keys)
throws ExecutionException {
ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
int hits = 0;
int misses = 0;

Expand Down Expand Up @@ -4754,7 +4756,7 @@ protected LocalManualCache(CacheBuilder<? super K, ? super V> builder,

@Override
@Nullable
public V getIfPresent(K key) {
public V getIfPresent(Object key) {
return localCache.getIfPresent(key);
}

Expand All @@ -4770,7 +4772,7 @@ public V load(Object key) throws Exception {
}

@Override
public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
return localCache.getAllPresent(keys);
}

Expand Down

0 comments on commit 9eed063

Please sign in to comment.