This is an automated email from the ASF dual-hosted git repository.
lihan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 3e62a4f13b Refactor. Simplify 'Map' operations. No functional change.
3e62a4f13b is described below
commit 3e62a4f13bd1fc04b3e6d89988135c5c90fe2c1c
Author: lihan <[email protected]>
AuthorDate: Tue Nov 1 11:42:47 2022 +0800
Refactor. Simplify 'Map' operations. No functional change.
---
java/org/apache/catalina/authenticator/SavedRequest.java | 7 +------
.../apache/catalina/session/PersistentManagerBase.java | 6 +-----
java/org/apache/catalina/startup/ContextConfig.java | 14 ++------------
java/org/apache/catalina/startup/Tomcat.java | 7 +------
java/org/apache/coyote/http2/HpackEncoder.java | 6 +-----
java/org/apache/jasper/compiler/Generator.java | 7 ++-----
java/org/apache/jasper/compiler/PageInfo.java | 9 ++-------
java/org/apache/naming/factory/ResourceLinkFactory.java | 11 +++--------
java/org/apache/tomcat/util/buf/StringCache.java | 15 ++-------------
java/org/apache/tomcat/util/digester/RulesBase.java | 7 +------
java/org/apache/tomcat/util/modeler/Registry.java | 12 ++----------
.../org/apache/tomcat/websocket/WsWebSocketContainer.java | 13 ++-----------
java/org/apache/tomcat/websocket/server/UpgradeUtil.java | 11 ++---------
test/org/apache/catalina/startup/TesterMapRealm.java | 7 +------
test/org/apache/tomcat/unittest/TesterRequest.java | 7 +------
15 files changed, 24 insertions(+), 115 deletions(-)
diff --git a/java/org/apache/catalina/authenticator/SavedRequest.java
b/java/org/apache/catalina/authenticator/SavedRequest.java
index faaf8ec9a6..38d1db2716 100644
--- a/java/org/apache/catalina/authenticator/SavedRequest.java
+++ b/java/org/apache/catalina/authenticator/SavedRequest.java
@@ -67,12 +67,7 @@ public final class SavedRequest implements Serializable {
private final Map<String, List<String>> headers = new HashMap<>();
public void addHeader(String name, String value) {
- List<String> values = headers.get(name);
- if (values == null) {
- values = new ArrayList<>();
- headers.put(name, values);
- }
- values.add(value);
+ headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
}
public Iterator<String> getHeaderNames() {
diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java
b/java/org/apache/catalina/session/PersistentManagerBase.java
index a7698ee2fa..2c5b01deb9 100644
--- a/java/org/apache/catalina/session/PersistentManagerBase.java
+++ b/java/org/apache/catalina/session/PersistentManagerBase.java
@@ -706,11 +706,7 @@ public abstract class PersistentManagerBase extends
ManagerBase
* carry on.
*/
synchronized (this) {
- swapInLock = sessionSwapInLocks.get(id);
- if (swapInLock == null) {
- swapInLock = new Object();
- sessionSwapInLocks.put(id, swapInLock);
- }
+ swapInLock = sessionSwapInLocks.computeIfAbsent(id, k -> new
Object());
}
Session session = null;
diff --git a/java/org/apache/catalina/startup/ContextConfig.java
b/java/org/apache/catalina/startup/ContextConfig.java
index dc3bf7822b..b06c7e4294 100644
--- a/java/org/apache/catalina/startup/ContextConfig.java
+++ b/java/org/apache/catalina/startup/ContextConfig.java
@@ -1869,13 +1869,7 @@ public class ContextConfig implements LifecycleListener {
} else {
handlesTypesNonAnnotations = true;
}
- Set<ServletContainerInitializer> scis =
- typeInitializerMap.get(type);
- if (scis == null) {
- scis = new HashSet<>();
- typeInitializerMap.put(type, scis);
- }
- scis.add(sci);
+ typeInitializerMap.computeIfAbsent(type, k -> new
HashSet<>()).add(sci);
}
}
}
@@ -2407,11 +2401,7 @@ public class ContextConfig implements LifecycleListener {
}
for (ServletContainerInitializer sci : entry.getSciSet()) {
- Set<Class<?>> classes = initializerClassMap.get(sci);
- if (classes == null) {
- classes = new HashSet<>();
- initializerClassMap.put(sci, classes);
- }
+ Set<Class<?>> classes =
initializerClassMap.computeIfAbsent(sci, k -> new HashSet<>());
classes.add(clazz);
}
}
diff --git a/java/org/apache/catalina/startup/Tomcat.java
b/java/org/apache/catalina/startup/Tomcat.java
index ac18e65aa6..39142b6b06 100644
--- a/java/org/apache/catalina/startup/Tomcat.java
+++ b/java/org/apache/catalina/startup/Tomcat.java
@@ -525,12 +525,7 @@ public class Tomcat {
* @param role The role name
*/
public void addRole(String user, String role) {
- List<String> roles = userRoles.get(user);
- if (roles == null) {
- roles = new ArrayList<>();
- userRoles.put(user, roles);
- }
- roles.add(role);
+ userRoles.computeIfAbsent(user, k -> new ArrayList<>()).add(role);
}
// ------- Extra customization -------
diff --git a/java/org/apache/coyote/http2/HpackEncoder.java
b/java/org/apache/coyote/http2/HpackEncoder.java
index ae81033e84..1de32c632d 100644
--- a/java/org/apache/coyote/http2/HpackEncoder.java
+++ b/java/org/apache/coyote/http2/HpackEncoder.java
@@ -245,11 +245,7 @@ class HpackEncoder {
private void addToDynamicTable(String headerName, String val) {
int pos = entryPositionCounter++;
DynamicTableEntry d = new DynamicTableEntry(headerName, val, -pos);
- List<TableEntry> existing = dynamicTable.get(headerName);
- if (existing == null) {
- dynamicTable.put(headerName, existing = new ArrayList<>(1));
- }
- existing.add(d);
+ dynamicTable.computeIfAbsent(headerName, k -> new
ArrayList<>(1)).add(d);
evictionQueue.add(d);
currentTableSize += d.getSize();
runEvictionIfRequired();
diff --git a/java/org/apache/jasper/compiler/Generator.java
b/java/org/apache/jasper/compiler/Generator.java
index d092141d7c..7b25cf31ee 100644
--- a/java/org/apache/jasper/compiler/Generator.java
+++ b/java/org/apache/jasper/compiler/Generator.java
@@ -2114,11 +2114,8 @@ class Generator {
private TagHandlerInfo getTagHandlerInfo(Node.CustomTag n)
throws JasperException {
- Map<String,TagHandlerInfo> handlerInfosByShortName =
handlerInfos.get(n.getPrefix());
- if (handlerInfosByShortName == null) {
- handlerInfosByShortName = new HashMap<>();
- handlerInfos.put(n.getPrefix(), handlerInfosByShortName);
- }
+ Map<String, TagHandlerInfo> handlerInfosByShortName = handlerInfos.
+ computeIfAbsent(n.getPrefix(), k -> new HashMap<>());
TagHandlerInfo handlerInfo =
handlerInfosByShortName.get(n.getLocalName());
if (handlerInfo == null) {
diff --git a/java/org/apache/jasper/compiler/PageInfo.java
b/java/org/apache/jasper/compiler/PageInfo.java
index 2d24d98427..43a02d101e 100644
--- a/java/org/apache/jasper/compiler/PageInfo.java
+++ b/java/org/apache/jasper/compiler/PageInfo.java
@@ -336,13 +336,8 @@ class PageInfo {
* @param uri The URI to be pushed onto the stack
*/
public void pushPrefixMapping(String prefix, String uri) {
- Deque<String> stack = xmlPrefixMapper.get(prefix);
- if (stack == null) {
- // Must be LinkedList as it needs to accept nulls
- stack = new LinkedList<>();
- xmlPrefixMapper.put(prefix, stack);
- }
- stack.addFirst(uri);
+ // Must be LinkedList as it needs to accept nulls
+ xmlPrefixMapper.computeIfAbsent(prefix, k -> new
LinkedList<>()).addFirst(uri);
}
/*
diff --git a/java/org/apache/naming/factory/ResourceLinkFactory.java
b/java/org/apache/naming/factory/ResourceLinkFactory.java
index 765a02c7f1..17ca733b7e 100644
--- a/java/org/apache/naming/factory/ResourceLinkFactory.java
+++ b/java/org/apache/naming/factory/ResourceLinkFactory.java
@@ -71,14 +71,9 @@ public class ResourceLinkFactory implements ObjectFactory {
String globalName) {
validateGlobalContext(globalContext);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
- Map<String,String> registrations = globalResourceRegistrations.get(cl);
- if (registrations == null) {
- // Web application initialization is single threaded so this is
- // safe.
- registrations = new HashMap<>();
- globalResourceRegistrations.put(cl, registrations);
- }
- registrations.put(localName, globalName);
+ // Web application initialization is single threaded so this is
+ // safe.
+ globalResourceRegistrations.computeIfAbsent(cl, k -> new
HashMap<>()).put(localName, globalName);
}
diff --git a/java/org/apache/tomcat/util/buf/StringCache.java
b/java/org/apache/tomcat/util/buf/StringCache.java
index 84acd19005..37a3c28336 100644
--- a/java/org/apache/tomcat/util/buf/StringCache.java
+++ b/java/org/apache/tomcat/util/buf/StringCache.java
@@ -241,13 +241,7 @@ public class StringCache {
int[] countA = item.getValue();
Integer count = Integer.valueOf(countA[0]);
// Add to the list for that count
- ArrayList<ByteEntry> list = tempMap.get(count);
- if (list == null) {
- // Create list
- list = new ArrayList<>();
- tempMap.put(count, list);
- }
- list.add(entry);
+ tempMap.computeIfAbsent(count, k -> new
ArrayList<>()).add(entry);
}
// Allocate array of the right size
int size = bcStats.size();
@@ -358,12 +352,7 @@ public class StringCache {
int[] countA = item.getValue();
Integer count = Integer.valueOf(countA[0]);
// Add to the list for that count
- ArrayList<CharEntry> list = tempMap.get(count);
- if (list == null) {
- // Create list
- list = new ArrayList<>();
- tempMap.put(count, list);
- }
+ ArrayList<CharEntry> list =
tempMap.computeIfAbsent(count, k -> new ArrayList<>());
list.add(entry);
}
// Allocate array of the right size
diff --git a/java/org/apache/tomcat/util/digester/RulesBase.java
b/java/org/apache/tomcat/util/digester/RulesBase.java
index 9a182fd500..2531509953 100644
--- a/java/org/apache/tomcat/util/digester/RulesBase.java
+++ b/java/org/apache/tomcat/util/digester/RulesBase.java
@@ -103,12 +103,7 @@ public class RulesBase implements Rules {
pattern = pattern.substring(0, patternLength-1);
}
- List<Rule> list = cache.get(pattern);
- if (list == null) {
- list = new ArrayList<>();
- cache.put(pattern, list);
- }
- list.add(rule);
+ cache.computeIfAbsent(pattern, k -> new ArrayList<>()).add(rule);
rules.add(rule);
if (this.digester != null) {
rule.setDigester(this.digester);
diff --git a/java/org/apache/tomcat/util/modeler/Registry.java
b/java/org/apache/tomcat/util/modeler/Registry.java
index 84cf831420..130d6f5bdd 100644
--- a/java/org/apache/tomcat/util/modeler/Registry.java
+++ b/java/org/apache/tomcat/util/modeler/Registry.java
@@ -277,11 +277,7 @@ public class Registry implements RegistryMBean,
MBeanRegistration {
if (domain == null) {
domain = "";
}
- Hashtable<String, Integer> domainTable = idDomains.get(domain);
- if (domainTable == null) {
- domainTable = new Hashtable<>();
- idDomains.put(domain, domainTable);
- }
+ Hashtable<String, Integer> domainTable =
idDomains.computeIfAbsent(domain, k -> new Hashtable<>());
if (name == null) {
name = "";
}
@@ -291,11 +287,7 @@ public class Registry implements RegistryMBean,
MBeanRegistration {
return i.intValue();
}
- int id[] = ids.get(domain);
- if (id == null) {
- id = new int[1];
- ids.put(domain, id);
- }
+ int[] id = ids.computeIfAbsent(domain, k -> new int[1]);
int code = id[0]++;
domainTable.put(name, Integer.valueOf(code));
return code;
diff --git a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
index 2db1226084..273b9945cc 100644
--- a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
+++ b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
@@ -606,12 +606,7 @@ public class WsWebSocketContainer implements
WebSocketContainer, BackgroundProce
if (endpointSessionMap.size() == 0) {
BackgroundProcessManager.getInstance().register(this);
}
- Set<WsSession> wsSessions = endpointSessionMap.get(key);
- if (wsSessions == null) {
- wsSessions = new HashSet<>();
- endpointSessionMap.put(key, wsSessions);
- }
- wsSessions.add(wsSession);
+ endpointSessionMap.computeIfAbsent(key, k -> new
HashSet<>()).add(wsSession);
}
sessions.put(wsSession, wsSession);
}
@@ -892,11 +887,7 @@ public class WsWebSocketContainer implements
WebSocketContainer, BackgroundProce
// expected to handle splitting into individual values
String headerValue = line.substring(index + 1).trim();
- List<String> values = headers.get(headerName);
- if (values == null) {
- values = new ArrayList<>(1);
- headers.put(headerName, values);
- }
+ List<String> values = headers.computeIfAbsent(headerName, k -> new
ArrayList<>(1));
values.add(headerValue);
}
diff --git a/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
b/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
index de6c3527d6..b0e6b49328 100644
--- a/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
+++ b/java/org/apache/tomcat/websocket/server/UpgradeUtil.java
@@ -256,15 +256,8 @@ public class UpgradeUtil {
List<Transformation> result = new
ArrayList<>(negotiatedExtensions.size());
for (Extension extension : negotiatedExtensions) {
- List<List<Extension.Parameter>> preferences =
- extensionPreferences.get(extension.getName());
-
- if (preferences == null) {
- preferences = new ArrayList<>();
- extensionPreferences.put(extension.getName(), preferences);
- }
-
- preferences.add(extension.getParameters());
+ extensionPreferences.computeIfAbsent(extension.getName(), k -> new
ArrayList<>())
+ .add(extension.getParameters());
}
for (Map.Entry<String,List<List<Extension.Parameter>>> entry :
diff --git a/test/org/apache/catalina/startup/TesterMapRealm.java
b/test/org/apache/catalina/startup/TesterMapRealm.java
index 3f5abc957b..c1ee7f3f5b 100644
--- a/test/org/apache/catalina/startup/TesterMapRealm.java
+++ b/test/org/apache/catalina/startup/TesterMapRealm.java
@@ -38,12 +38,7 @@ public final class TesterMapRealm extends RealmBase {
}
public void addUserRole(String username, String role) {
- List<String> userRoles = roles.get(username);
- if (userRoles == null) {
- userRoles = new ArrayList<>();
- roles.put(username, userRoles);
- }
- userRoles.add(role);
+ roles.computeIfAbsent(username, k -> new ArrayList<>()).add(role);
}
@Override
diff --git a/test/org/apache/tomcat/unittest/TesterRequest.java
b/test/org/apache/tomcat/unittest/TesterRequest.java
index f73c68d0af..cdecdffe20 100644
--- a/test/org/apache/tomcat/unittest/TesterRequest.java
+++ b/test/org/apache/tomcat/unittest/TesterRequest.java
@@ -111,12 +111,7 @@ public class TesterRequest extends Request {
private final Map<String,List<String>> headers = new HashMap<>();
public void addHeader(String name, String value) {
- List<String> values = headers.get(name);
- if (values == null) {
- values = new ArrayList<>();
- headers.put(name, values);
- }
- values.add(value);
+ headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
}
@Override
public String getHeader(String name) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]