SGF-317 - Improved GemfireCache for Spring 4.1 compatibility.
Added putIfAbsent(…) to GemfireCache to make sure we can compile against Spring 4.1. Improved the implementation of get(Object, Class<T>) to adhere to the contract defined by the interface.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2013 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,7 @@ import com.gemstone.gemfire.cache.Region;
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author John Blum
|
||||
* @author Oliver Gierke
|
||||
* @see org.springframework.cache.Cache
|
||||
* @see com.gemstone.gemfire.cache.Region
|
||||
*/
|
||||
@@ -68,8 +69,16 @@ public class GemfireCache implements Cache {
|
||||
return (value == null ? null : new SimpleValueWrapper(value));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(final Object key, final Class<T> type) {
|
||||
return type.cast(region.get(key));
|
||||
|
||||
Object value = region.get(key);
|
||||
|
||||
if (value != null && type != null && !type.isInstance(value)) {
|
||||
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
|
||||
}
|
||||
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -77,4 +86,16 @@ public class GemfireCache implements Cache {
|
||||
region.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation to satisfy extension of the {@link Cache} interface in Spring 4.1. Don't add the {@link Override}
|
||||
* annotation as this will break the compilation on 4.0.
|
||||
*
|
||||
* @see org.springframework.cache.Cache#putIfAbsent(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public ValueWrapper putIfAbsent(Object key, Object value) {
|
||||
|
||||
Object existingValue = region.putIfAbsent(key, value);
|
||||
return existingValue == null ? null : new SimpleValueWrapper(existingValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user