Proper nullable return declaration for AbstractValueAdaptingCache.lookup

Issue: SPR-15540
This commit is contained in:
Juergen Hoeller
2018-02-26 13:05:12 +01:00
parent 7d89de06e3
commit c2d5ca9811
5 changed files with 24 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -100,6 +100,7 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
} }
@Override @Override
@Nullable
protected Object lookup(Object key) { protected Object lookup(Object key) {
return this.cache.getIfPresent(key); return this.cache.getIfPresent(key);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -42,13 +42,15 @@ public class EhCacheCache implements Cache {
/** /**
* Create an {@link EhCacheCache} instance. * Create an {@link EhCacheCache} instance.
* @param ehcache backing Ehcache instance * @param ehcache the backing Ehcache instance
*/ */
public EhCacheCache(Ehcache ehcache) { public EhCacheCache(Ehcache ehcache) {
Assert.notNull(ehcache, "Ehcache must not be null"); Assert.notNull(ehcache, "Ehcache must not be null");
Status status = ehcache.getStatus(); Status status = ehcache.getStatus();
Assert.isTrue(Status.STATUS_ALIVE.equals(status), if (!Status.STATUS_ALIVE.equals(status)) {
"An 'alive' Ehcache is required - current cache is " + status.toString()); throw new IllegalArgumentException(
"An 'alive' Ehcache is required - current cache is " + status.toString());
}
this.cache = ehcache; this.cache = ehcache;
} }
@@ -81,7 +83,7 @@ public class EhCacheCache implements Cache {
else { else {
this.cache.acquireWriteLockOnKey(key); this.cache.acquireWriteLockOnKey(key);
try { try {
element = lookup(key); // One more attempt with the write lock element = lookup(key); // one more attempt with the write lock
if (element != null) { if (element != null) {
return (T) element.getObjectValue(); return (T) element.getObjectValue();
} }
@@ -115,7 +117,8 @@ public class EhCacheCache implements Cache {
Element element = this.cache.get(key); Element element = this.cache.get(key);
Object value = (element != null ? element.getObjectValue() : null); Object value = (element != null ? element.getObjectValue() : null);
if (value != null && type != null && !type.isInstance(value)) { if (value != null && type != null && !type.isInstance(value)) {
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value); throw new IllegalStateException(
"Cached value is not of required type [" + type.getName() + "]: " + value);
} }
return (T) value; return (T) value;
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -71,6 +71,7 @@ public class JCacheCache extends AbstractValueAdaptingCache {
} }
@Override @Override
@Nullable
protected Object lookup(Object key) { protected Object lookup(Object key) {
return this.cache.get(key); return this.cache.get(key);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -132,6 +132,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
} }
@Override @Override
@Nullable
protected Object lookup(Object key) { protected Object lookup(Object key) {
return this.store.get(key); return this.store.get(key);
} }
@@ -201,8 +202,8 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
} }
@Override @Override
protected Object fromStoreValue(Object storeValue) { protected Object fromStoreValue(@Nullable Object storeValue) {
if (this.serialization != null) { if (storeValue != null && this.serialization != null) {
try { try {
return super.fromStoreValue(deserializeValue(this.serialization, storeValue)); return super.fromStoreValue(deserializeValue(this.serialization, storeValue));
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -65,7 +65,8 @@ public abstract class AbstractValueAdaptingCache implements Cache {
public <T> T get(Object key, @Nullable Class<T> type) { public <T> T get(Object key, @Nullable Class<T> type) {
Object value = fromStoreValue(lookup(key)); Object value = fromStoreValue(lookup(key));
if (value != null && type != null && !type.isInstance(value)) { if (value != null && type != null && !type.isInstance(value)) {
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value); throw new IllegalStateException(
"Cached value is not of required type [" + type.getName() + "]: " + value);
} }
return (T) value; return (T) value;
} }
@@ -73,8 +74,9 @@ public abstract class AbstractValueAdaptingCache implements Cache {
/** /**
* Perform an actual lookup in the underlying store. * Perform an actual lookup in the underlying store.
* @param key the key whose associated value is to be returned * @param key the key whose associated value is to be returned
* @return the raw store value for the key * @return the raw store value for the key, or {@code null} if none
*/ */
@Nullable
protected abstract Object lookup(Object key); protected abstract Object lookup(Object key);
@@ -85,7 +87,7 @@ public abstract class AbstractValueAdaptingCache implements Cache {
* @return the value to return to the user * @return the value to return to the user
*/ */
@Nullable @Nullable
protected Object fromStoreValue(Object storeValue) { protected Object fromStoreValue(@Nullable Object storeValue) {
if (this.allowNullValues && storeValue == NullValue.INSTANCE) { if (this.allowNullValues && storeValue == NullValue.INSTANCE) {
return null; return null;
} }
@@ -104,8 +106,7 @@ public abstract class AbstractValueAdaptingCache implements Cache {
return NullValue.INSTANCE; return NullValue.INSTANCE;
} }
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Cache '%s' is configured to not allow null " + "Cache '" + getName() + "' is configured to not allow null values but null was provided");
"values but null was provided", getName()));
} }
return userValue; return userValue;
} }