Guava: LocalCache Implementation doesn't allow null values

Created on 19 Apr 2016  Â·  1Comment  Â·  Source: google/guava

In LocalCache.java
@Override
public V put(K key, V value) {
checkNotNull(key);
checkNotNull(value);
int hash = hash(key);
return segmentFor(hash).put(key, hash, value, false);
}

is there a specific reason for adding a null check for the value as well. why can't cache have null values.

status=working-as-intended

Most helpful comment

It's not the implementation; the implementation is required by the Cache
interface to do this (although that could be made clearer).

Null values would break at least the getAll, getIfPresent, and
getAllPresent methods. This would also imply that CacheLoader.load() should
be able to return null, but we discovered that many users assume that null
values get cached and many users assume they *don't *get cached, so that
was another minefield.

Use a Cache> and it will make your code
clearer anyway!

On Tue, Apr 19, 2016 at 12:16 AM, Shrikant [email protected] wrote:

In LocalCache.java
@Override https://github.com/Override
public V put(K key, V value) {
checkNotNull(key);
checkNotNull(value);
int hash = hash(key);
return segmentFor(hash).put(key, hash, value, false);
}

is there a specific reason for adding a null check for the value as well.
why can't cache have null values.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/google/guava/issues/2461

Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]

>All comments

It's not the implementation; the implementation is required by the Cache
interface to do this (although that could be made clearer).

Null values would break at least the getAll, getIfPresent, and
getAllPresent methods. This would also imply that CacheLoader.load() should
be able to return null, but we discovered that many users assume that null
values get cached and many users assume they *don't *get cached, so that
was another minefield.

Use a Cache> and it will make your code
clearer anyway!

On Tue, Apr 19, 2016 at 12:16 AM, Shrikant [email protected] wrote:

In LocalCache.java
@Override https://github.com/Override
public V put(K key, V value) {
checkNotNull(key);
checkNotNull(value);
int hash = hash(key);
return segmentFor(hash).put(key, hash, value, false);
}

is there a specific reason for adding a null check for the value as well.
why can't cache have null values.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/google/guava/issues/2461

Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gissuebot picture gissuebot  Â·  5Comments

ernestp picture ernestp  Â·  3Comments

Bocete picture Bocete  Â·  3Comments

cowwoc picture cowwoc  Â·  3Comments

oliviercailloux picture oliviercailloux  Â·  4Comments