This is an automated email from the ASF dual-hosted git repository.
lihan pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 2a89eddb91 Refactor. Simplify 'Map' operations. No functional change.
2a89eddb91 is described below
commit 2a89eddb9101bee602f03851cef8f9314d64fd77
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 c8ea2fadf2..d1ca241ec3 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 0738148d6e..0b4891b6b0 100644
--- a/java/org/apache/catalina/startup/ContextConfig.java
+++ b/java/org/apache/catalina/startup/ContextConfig.java
@@ -1878,13 +1878,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);
}
}
}
@@ -2416,11 +2410,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 12080d5281..275558c34d 100644
--- a/java/org/apache/catalina/startup/Tomcat.java
+++ b/java/org/apache/catalina/startup/Tomcat.java
@@ -526,12 +526,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 68224e5c50..085c06bb0d 100644
--- a/java/org/apache/jasper/compiler/Generator.java
+++ b/java/org/apache/jasper/compiler/Generator.java
@@ -2365,11 +2365,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 eea0ef97e5..67dd47a319 100644
--- a/java/org/apache/jasper/compiler/PageInfo.java
+++ b/java/org/apache/jasper/compiler/PageInfo.java
@@ -332,13 +332,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 764faae160..5b7c0605a5 100644
--- a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
+++ b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
@@ -611,12 +611,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);
}
@@ -897,11 +892,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 492ef6fd50..b7945698ce 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 ccd2be3f6d..5f307ad5df 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 61bf1e2252..8de33c7e9b 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]