From f00afe3247128c74c52a4f323d7c7e18c46ceefe Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 23 Mar 2018 17:36:23 +0100 Subject: [PATCH] Use (Concurrent)Map.computeIfAbsent for lazy nested collection creation --- .../util/xml/SimpleNamespaceContext.java | 9 +++------ .../jms/connection/CachingConnectionFactory.java | 13 ++++--------- .../jms/connection/JmsResourceHolder.java | 8 ++------ .../support/NativeMessageHeaderAccessor.java | 11 +++-------- .../test/context/cache/DefaultContextCache.java | 8 ++------ 5 files changed, 14 insertions(+), 35 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/xml/SimpleNamespaceContext.java b/spring-core/src/main/java/org/springframework/util/xml/SimpleNamespaceContext.java index 8d94ed7e9e..d0498abcea 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/SimpleNamespaceContext.java +++ b/spring-core/src/main/java/org/springframework/util/xml/SimpleNamespaceContext.java @@ -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"); * you may not use this file except in compliance with the License. @@ -123,11 +123,8 @@ public class SimpleNamespaceContext implements NamespaceContext { } else { this.prefixToNamespaceUri.put(prefix, namespaceUri); - Set prefixes = this.namespaceUriToPrefixes.get(namespaceUri); - if (prefixes == null) { - prefixes = new LinkedHashSet<>(); - this.namespaceUriToPrefixes.put(namespaceUri, prefixes); - } + Set prefixes = + this.namespaceUriToPrefixes.computeIfAbsent(namespaceUri, k -> new LinkedHashSet<>()); prefixes.add(prefix); } } diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java index 2426c0d0b9..1c3dd8fe6d 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java @@ -26,6 +26,8 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; @@ -86,7 +88,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory { private volatile boolean active = true; - private final Map> cachedSessions = new HashMap<>(); + private final ConcurrentMap> cachedSessions = new ConcurrentHashMap<>(); /** @@ -208,14 +210,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory { return null; } - LinkedList sessionList; - synchronized (this.cachedSessions) { - sessionList = this.cachedSessions.get(mode); - if (sessionList == null) { - sessionList = new LinkedList<>(); - this.cachedSessions.put(mode, sessionList); - } - } + LinkedList sessionList = this.cachedSessions.computeIfAbsent(mode, k -> new LinkedList<>()); Session session = null; synchronized (sessionList) { if (!sessionList.isEmpty()) { diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/JmsResourceHolder.java b/spring-jms/src/main/java/org/springframework/jms/connection/JmsResourceHolder.java index f38945eb82..5a09a0db85 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/JmsResourceHolder.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/JmsResourceHolder.java @@ -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"); * you may not use this file except in compliance with the License. @@ -139,11 +139,7 @@ public class JmsResourceHolder extends ResourceHolderSupport { if (!this.sessions.contains(session)) { this.sessions.add(session); if (connection != null) { - List sessions = this.sessionsPerConnection.get(connection); - if (sessions == null) { - sessions = new LinkedList<>(); - this.sessionsPerConnection.put(connection, sessions); - } + List sessions = this.sessionsPerConnection.computeIfAbsent(connection, k -> new LinkedList<>()); sessions.add(session); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java index 3049f07c15..4b83ee76e2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java @@ -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"); * you may not use this file except in compliance with the License. @@ -184,11 +184,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor { nativeHeaders = new LinkedMultiValueMap<>(4); setHeader(NATIVE_HEADERS, nativeHeaders); } - List values = nativeHeaders.get(name); - if (values == null) { - values = new LinkedList<>(); - nativeHeaders.put(name, values); - } + List values = nativeHeaders.computeIfAbsent(name, k -> new LinkedList<>()); values.add(value); setModified(true); } @@ -197,8 +193,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor { if (headers == null) { return; } - headers.forEach((key, values) -> - values.forEach(value -> addNativeHeader(key, value))); + headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value))); } @Nullable diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultContextCache.java b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultContextCache.java index 61b8a48ce5..03a1d31eee 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultContextCache.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultContextCache.java @@ -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"); * you may not use this file except in compliance with the License. @@ -143,11 +143,7 @@ public class DefaultContextCache implements ContextCache { MergedContextConfiguration child = key; MergedContextConfiguration parent = child.getParent(); while (parent != null) { - Set list = this.hierarchyMap.get(parent); - if (list == null) { - list = new HashSet<>(); - this.hierarchyMap.put(parent, list); - } + Set list = this.hierarchyMap.computeIfAbsent(parent, k -> new HashSet<>()); list.add(child); child = parent; parent = child.getParent();