Standard use of resolvedDestinationCache Map; fixed formatting

Issue: SPR-11939
This commit is contained in:
Juergen Hoeller
2014-07-02 20:53:52 +02:00
parent 6d15fcc4a6
commit 526b463474
2 changed files with 40 additions and 39 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.messaging.core;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.InitializingBean;
@@ -34,9 +35,9 @@ import org.springframework.util.Assert;
*/
public class CachingDestinationResolverProxy<D> implements DestinationResolver<D>, InitializingBean {
private final ConcurrentHashMap<String, D> resolvedDestinationCache = new ConcurrentHashMap<String, D>();
private final Map<String, D> resolvedDestinationCache = new ConcurrentHashMap<String, D>();
private DestinationResolver<D> targetDestinationResolver;
private DestinationResolver<D> targetDestinationResolver;
/**
@@ -47,14 +48,14 @@ public class CachingDestinationResolverProxy<D> implements DestinationResolver<D
}
/**
* Create a new CachingDestinationResolverProxy using the given target
* Create a new CachingDestinationResolverProxy using the given target
* DestinationResolver to actually resolve destinations.
* @param targetDestinationResolver the target DestinationResolver to delegate to
*/
public CachingDestinationResolverProxy(DestinationResolver<D> targetDestinationResolver) {
Assert.notNull(targetDestinationResolver, "Target DestinationResolver must not be null");
this.targetDestinationResolver = targetDestinationResolver;
}
* @param targetDestinationResolver the target DestinationResolver to delegate to
*/
public CachingDestinationResolverProxy(DestinationResolver<D> targetDestinationResolver) {
Assert.notNull(targetDestinationResolver, "Target DestinationResolver must not be null");
this.targetDestinationResolver = targetDestinationResolver;
}
/**
@@ -73,22 +74,21 @@ public class CachingDestinationResolverProxy<D> implements DestinationResolver<D
/**
*
* Resolves and caches destinations if successfully resolved by the target
* DestinationResolver implementation.
* @param name the destination name to be resolved
* @return the currently resolved destination or an already cached destination
* @throws DestinationResolutionException if the target DestinationResolver
* reports an error during destination resolution
*/
@Override
public D resolveDestination(String name) throws DestinationResolutionException {
D destination = this.resolvedDestinationCache.get(name);
* Resolves and caches destinations if successfully resolved by the target
* DestinationResolver implementation.
* @param name the destination name to be resolved
* @return the currently resolved destination or an already cached destination
* @throws DestinationResolutionException if the target DestinationResolver
* reports an error during destination resolution
*/
@Override
public D resolveDestination(String name) throws DestinationResolutionException {
D destination = this.resolvedDestinationCache.get(name);
if (destination == null) {
destination = this.targetDestinationResolver.resolveDestination(name);
this.resolvedDestinationCache.putIfAbsent(name, destination);
}
return destination;
}
destination = this.targetDestinationResolver.resolveDestination(name);
this.resolvedDestinationCache.put(name, destination);
}
return destination;
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.core;
import org.junit.Test;
@@ -28,23 +29,23 @@ import static org.mockito.Mockito.*;
*/
public class CachingDestinationResolverTests {
@Test
public void cachedDestination() {
@SuppressWarnings("unchecked")
DestinationResolver<String> destinationResolver = (DestinationResolver<String>) mock(DestinationResolver.class);
CachingDestinationResolverProxy<String> cachingDestinationResolver = new CachingDestinationResolverProxy<String>(destinationResolver);
@Test
public void cachedDestination() {
@SuppressWarnings("unchecked")
DestinationResolver<String> destinationResolver = (DestinationResolver<String>) mock(DestinationResolver.class);
CachingDestinationResolverProxy<String> cachingDestinationResolver = new CachingDestinationResolverProxy<String>(destinationResolver);
when(destinationResolver.resolveDestination("abcd")).thenReturn("dcba");
when(destinationResolver.resolveDestination("abcd")).thenReturn("dcba");
when(destinationResolver.resolveDestination("1234")).thenReturn("4321");
assertEquals("dcba", cachingDestinationResolver.resolveDestination("abcd"));
assertEquals("4321", cachingDestinationResolver.resolveDestination("1234"));
assertEquals("dcba", cachingDestinationResolver.resolveDestination("abcd"));
assertEquals("4321", cachingDestinationResolver.resolveDestination("1234"));
assertEquals("4321", cachingDestinationResolver.resolveDestination("1234"));
assertEquals("dcba", cachingDestinationResolver.resolveDestination("abcd"));
verify(destinationResolver, times(1)).resolveDestination("abcd");
verify(destinationResolver, times(1)).resolveDestination("abcd");
verify(destinationResolver, times(1)).resolveDestination("1234");
}
}
@Test(expected = IllegalArgumentException.class)
public void noTargetSet() {
@@ -52,9 +53,9 @@ public class CachingDestinationResolverTests {
cachingDestinationResolver.afterPropertiesSet();
}
@Test(expected = IllegalArgumentException.class)
public void nullTargetThroughConstructor() {
new CachingDestinationResolverProxy<String>(null);
}
@Test(expected = IllegalArgumentException.class)
public void nullTargetThroughConstructor() {
new CachingDestinationResolverProxy<String>(null);
}
}