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 52774c2467 Refactor. Simplify 'Map' operations. No functional change.
52774c2467 is described below

commit 52774c24678f11ca7aadc02d98dca156e8c36b3d
Author: lihan <li...@apache.org>
AuthorDate: Sat Oct 8 11:10:30 2022 +0800

    Refactor. Simplify 'Map' operations. No functional change.
---
 java/org/apache/catalina/connector/Request.java    |  8 ++------
 .../catalina/core/ApplicationPushBuilder.java      |  8 +-------
 java/org/apache/catalina/core/StandardContext.java | 23 ++++++----------------
 .../apache/catalina/servlets/WebdavServlet.java    |  8 +-------
 .../util/descriptor/web/SecurityConstraint.java    |  8 ++------
 java/org/apache/tomcat/util/http/Parameters.java   |  7 +------
 6 files changed, 13 insertions(+), 49 deletions(-)

diff --git a/java/org/apache/catalina/connector/Request.java 
b/java/org/apache/catalina/connector/Request.java
index 40475e8fc5..b1f59767cc 100644
--- a/java/org/apache/catalina/connector/Request.java
+++ b/java/org/apache/catalina/connector/Request.java
@@ -3401,12 +3401,8 @@ public class Request implements HttpServletRequest {
         for (AcceptLanguage acceptLanguage : acceptLanguages) {
             // Add a new Locale to the list of Locales for this quality level
             Double key = Double.valueOf(-acceptLanguage.getQuality());  // 
Reverse the order
-            ArrayList<Locale> values = locales.get(key);
-            if (values == null) {
-                values = new ArrayList<>();
-                locales.put(key, values);
-            }
-            values.add(acceptLanguage.getLocale());
+            locales.computeIfAbsent(key, k -> new ArrayList<>())
+                .add(acceptLanguage.getLocale());
         }
     }
 
diff --git a/java/org/apache/catalina/core/ApplicationPushBuilder.java 
b/java/org/apache/catalina/core/ApplicationPushBuilder.java
index 240167c529..42842ef869 100644
--- a/java/org/apache/catalina/core/ApplicationPushBuilder.java
+++ b/java/org/apache/catalina/core/ApplicationPushBuilder.java
@@ -243,13 +243,7 @@ public class ApplicationPushBuilder implements PushBuilder 
{
 
     @Override
     public PushBuilder 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);
         return this;
     }
 
diff --git a/java/org/apache/catalina/core/StandardContext.java 
b/java/org/apache/catalina/core/StandardContext.java
index d64f676f9a..5044ed462a 100644
--- a/java/org/apache/catalina/core/StandardContext.java
+++ b/java/org/apache/catalina/core/StandardContext.java
@@ -4891,12 +4891,7 @@ public class StandardContext extends ContainerBase
                 continue;
             }
             Integer key = Integer.valueOf(loadOnStartup);
-            ArrayList<Wrapper> list = map.get(key);
-            if (list == null) {
-                list = new ArrayList<>();
-                map.put(key, list);
-            }
-            list.add(wrapper);
+            map.computeIfAbsent(key, k -> new ArrayList<>()).add(wrapper);
         }
 
         // Load the collected "load on startup" servlets
@@ -5303,12 +5298,8 @@ public class StandardContext extends ContainerBase
             String jndiName = resource.getName();
             for (InjectionTarget injectionTarget: injectionTargets) {
                 String clazz = injectionTarget.getTargetClass();
-                Map<String, String> injections = injectionMap.get(clazz);
-                if (injections == null) {
-                    injections = new HashMap<>();
-                    injectionMap.put(clazz, injections);
-                }
-                injections.put(injectionTarget.getTargetName(), jndiName);
+                injectionMap.computeIfAbsent(clazz, k -> new HashMap<>())
+                    .put(injectionTarget.getTargetName(), jndiName);
             }
         }
     }
@@ -5332,10 +5323,8 @@ public class StandardContext extends ContainerBase
         ApplicationParameter params[] = findApplicationParameters();
         for (ApplicationParameter param : params) {
             if (param.getOverride()) {
-                if (mergedParams.get(param.getName()) == null) {
-                    mergedParams.put(param.getName(),
-                            param.getValue());
-                }
+                mergedParams.computeIfAbsent(param.getName(),
+                    k -> param.getValue());
             } else {
                 mergedParams.put(param.getName(), param.getValue());
             }
@@ -5864,7 +5853,7 @@ public class StandardContext extends ContainerBase
                 parent = parent.getParent();
             }
             while (!stk.isEmpty()) {
-                buff.append("/" + stk.remove());
+                buff.append("/").append(stk.remove());
             }
             buff.append(getName());
             namingContextName = buff.toString();
diff --git a/java/org/apache/catalina/servlets/WebdavServlet.java 
b/java/org/apache/catalina/servlets/WebdavServlet.java
index f3f57c8a66..f0c88f7d8d 100644
--- a/java/org/apache/catalina/servlets/WebdavServlet.java
+++ b/java/org/apache/catalina/servlets/WebdavServlet.java
@@ -1192,13 +1192,7 @@ public class WebdavServlet extends DefaultServlet {
                         int slash = lock.path.lastIndexOf('/');
                         String parentPath = lock.path.substring(0, slash);
 
-                        List<String> lockNulls = 
lockNullResources.get(parentPath);
-                        if (lockNulls == null) {
-                            lockNulls = new ArrayList<>();
-                            lockNullResources.put(parentPath, lockNulls);
-                        }
-
-                        lockNulls.add(lock.path);
+                        lockNullResources.computeIfAbsent(parentPath, k -> new 
ArrayList<>()).add(lock.path);
                     }
 
                     // Add the Lock-Token header as by RFC 2518 8.10.1
diff --git a/java/org/apache/tomcat/util/descriptor/web/SecurityConstraint.java 
b/java/org/apache/tomcat/util/descriptor/web/SecurityConstraint.java
index f7ad943138..6e779a610c 100644
--- a/java/org/apache/tomcat/util/descriptor/web/SecurityConstraint.java
+++ b/java/org/apache/tomcat/util/descriptor/web/SecurityConstraint.java
@@ -682,12 +682,8 @@ public class SecurityConstraint extends XmlEncodingBase 
implements Serializable
                             }
                         } else {
                             // Build the union of methods for this pattern
-                            Set<String> m = urlMethodMap.get(pattern);
-                            if (m == null) {
-                                m = new HashSet<>();
-                                urlMethodMap.put(pattern, m);
-                            }
-                            m.addAll(Arrays.asList(methods));
+                            urlMethodMap.computeIfAbsent(pattern, k -> new 
HashSet<>())
+                                .addAll(Arrays.asList(methods));
                         }
                     }
                 }
diff --git a/java/org/apache/tomcat/util/http/Parameters.java 
b/java/org/apache/tomcat/util/http/Parameters.java
index c26d428072..ce765374e7 100644
--- a/java/org/apache/tomcat/util/http/Parameters.java
+++ b/java/org/apache/tomcat/util/http/Parameters.java
@@ -210,12 +210,7 @@ public final class Parameters {
                     "parameters.maxCountFail", Integer.valueOf(limit)));
         }
 
-        ArrayList<String> values = paramHashValues.get(key);
-        if (values == null) {
-            values = new ArrayList<>(1);
-            paramHashValues.put(key, values);
-        }
-        values.add(value);
+        paramHashValues.computeIfAbsent(key, k -> new 
ArrayList<>(1)).add(value);
     }
 
     public void setURLDecoder( UDecoder u ) {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to