Skip to content

Commit

Permalink
OF-1156: Be consistent with the contents of DefaultCache with the
Browse files Browse the repository at this point in the history
Hazelcast clustered cache implemented by MapProxyImpl by preventing null
keys and values being added. Also document this behaviour in the Cache
interface.
  • Loading branch information
GregDThomas committed Jul 4, 2016
1 parent 18e4ac5 commit d93e8d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/java/org/jivesoftware/util/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* If the cache does grow too large, objects will be removed such that those
* that are accessed least frequently are removed first. Because expiration
* happens automatically, the cache makes <b>no</b> gaurantee as to how long
* happens automatically, the cache makes <b>no</b> guarantee as to how long
* an object will remain in cache after it is put in.<p>
*
* Optionally, a maximum lifetime for all objects can be specified. In that
Expand All @@ -41,6 +41,9 @@
*
* All cache operations are thread safe.<p>
*
* Note that neither keys or values can be null; A {@link NullPointerException}
* will be thrown attempting to place or retrieve null values in to the cache.
*
* @see Cacheable
*/
public interface Cache<K,V> extends java.util.Map<K,V> {
Expand Down
32 changes: 17 additions & 15 deletions src/java/org/jivesoftware/util/cache/DefaultCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
*/
public class DefaultCache<K, V> implements Cache<K, V> {

private static final Logger Log = LoggerFactory.getLogger(DefaultCache.class);
private static final String NULL_KEY_IS_NOT_ALLOWED = "Null key is not allowed!";
private static final String NULL_VALUE_IS_NOT_ALLOWED = "Null value is not allowed!";

private static final Logger Log = LoggerFactory.getLogger(DefaultCache.class);

/**
* The map the keys and values are stored in.
Expand Down Expand Up @@ -133,6 +136,8 @@ public DefaultCache(String name, long maxSize, long maxLifetime) {

@Override
public synchronized V put(K key, V value) {
checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);
// Delete an old entry if it exists.
V answer = remove(key);

Expand Down Expand Up @@ -174,6 +179,7 @@ public synchronized V put(K key, V value) {

@Override
public synchronized V get(Object key) {
checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
// First, clear all entries that have been in cache longer than the
// maximum defined age.
deleteExpiredEntries();
Expand All @@ -200,6 +206,7 @@ public synchronized V get(Object key) {

@Override
public synchronized V remove(Object key) {
checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
DefaultCache.CacheObject<V> cacheObject = map.get(key);
// If the object is not in cache, stop trying to remove it.
if (cacheObject == null) {
Expand Down Expand Up @@ -285,6 +292,7 @@ public boolean isEmpty() {

@Override
public boolean contains(Object o) {
checkNotNull(o, NULL_KEY_IS_NOT_ALLOWED);
Iterator<V> it = iterator();
while (it.hasNext()) {
if (it.next().equals(o)) {
Expand Down Expand Up @@ -391,6 +399,7 @@ public void clear() {

@Override
public boolean containsKey(Object key) {
checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED);
// First, clear all entries that have been in cache longer than the
// maximum defined age.
deleteExpiredEntries();
Expand All @@ -409,14 +418,11 @@ public void putAll(Map<? extends K, ? extends V> map) {

@Override
public boolean containsValue(Object value) {
checkNotNull(value, NULL_VALUE_IS_NOT_ALLOWED);
// First, clear all entries that have been in cache longer than the
// maximum defined age.
deleteExpiredEntries();

if(value == null) {
return containsNullValue();
}

Iterator it = values().iterator();
while(it.hasNext()) {
if(value.equals(it.next())) {
Expand All @@ -426,16 +432,6 @@ public boolean containsValue(Object value) {
return false;
}

private boolean containsNullValue() {
Iterator it = values().iterator();
while(it.hasNext()) {
if(it.next() == null) {
return true;
}
}
return false;
}

@Override
public Set<Entry<K, V>> entrySet() {
// First, clear all entries that have been in cache longer than the
Expand Down Expand Up @@ -703,4 +699,10 @@ public CacheObject(V object, int size) {
this.size = size;
}
}

private void checkNotNull(final Object argument, final String message) {
if (argument == null) {
throw new NullPointerException(message);
}
}
}

0 comments on commit d93e8d8

Please sign in to comment.