diff --git a/src/main/java/org/springframework/data/gemfire/support/GemfireCache.java b/src/main/java/org/springframework/data/gemfire/support/GemfireCache.java index 0ed9b5d9..64782a69 100644 --- a/src/main/java/org/springframework/data/gemfire/support/GemfireCache.java +++ b/src/main/java/org/springframework/data/gemfire/support/GemfireCache.java @@ -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,7 +29,7 @@ import com.gemstone.gemfire.cache.Region; * * @author Costin Leau * @author John Blum - * + * @author Oliver Gierke */ public class GemfireCache implements Cache { @@ -67,9 +67,33 @@ public class GemfireCache implements Cache { return (value == null ? null : new SimpleValueWrapper(value)); } + @SuppressWarnings("unchecked") + public T get(final Object key, final Class type) { + + 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") public void put(Object key, Object value) { 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); + } } diff --git a/src/test/java/org/springframework/data/gemfire/support/GemfireCacheTest.java b/src/test/java/org/springframework/data/gemfire/support/GemfireCacheTest.java index 5c16999d..b20679a3 100644 --- a/src/test/java/org/springframework/data/gemfire/support/GemfireCacheTest.java +++ b/src/test/java/org/springframework/data/gemfire/support/GemfireCacheTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010 the original author or authors. + * Copyright 2010-104 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. @@ -16,8 +16,14 @@ package org.springframework.data.gemfire.support; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + import java.util.Properties; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.cache.Cache; import com.gemstone.gemfire.cache.CacheFactory; @@ -26,22 +32,24 @@ import com.gemstone.gemfire.distributed.DistributedSystem; /** * @author Costin Leau + * @author Oliver Gierke */ public class GemfireCacheTest extends AbstractNativeCacheTest> { + @Rule public ExpectedException exception = ExpectedException.none(); + @Override protected Cache createCache(Region nativeCache) { return new GemfireCache(nativeCache); } @Override - @SuppressWarnings({"deprecation", "unchecked" }) + @SuppressWarnings({ "deprecation", "unchecked" }) protected Region createNativeCache() throws Exception { com.gemstone.gemfire.cache.Cache instance = null; try { instance = CacheFactory.getAnyInstance(); - } catch (Exception ex) { - } + } catch (Exception ex) {} if (instance == null) { DistributedSystem ds = DistributedSystem.connect(new Properties()); @@ -55,4 +63,43 @@ public class GemfireCacheTest extends AbstractNativeCacheTest