You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2020/11/08 16:41:19 UTC

[jmeter] branch master updated: Simplify logic by using computeIfAbsent instead of putIfAbsent

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 99341ca  Simplify logic by using computeIfAbsent instead of putIfAbsent
99341ca is described below

commit 99341ca8bc81f3f76427628731ba4407234d27b5
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Sun Nov 8 17:38:45 2020 +0100

    Simplify logic by using computeIfAbsent instead of putIfAbsent
    
    Spotbugs can't determine correctly, that the lock will be released.
    We help it, by allowing reducing the allowed variables for locks to
    one.
---
 .../java/org/apache/jmeter/functions/StringToFile.java    | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/src/functions/src/main/java/org/apache/jmeter/functions/StringToFile.java b/src/functions/src/main/java/org/apache/jmeter/functions/StringToFile.java
index 9116a3c..3cb0879 100644
--- a/src/functions/src/main/java/org/apache/jmeter/functions/StringToFile.java
+++ b/src/functions/src/main/java/org/apache/jmeter/functions/StringToFile.java
@@ -101,14 +101,9 @@ public class StringToFile extends AbstractFunction {
             return false;
         }
         log.debug("Writing {} to file {} with charset {} and append {}", content, fileName, charset, append);
-        Lock localLock = new ReentrantLock();
-        Lock lock = lockMap.putIfAbsent(fileName, localLock);
+        Lock lock = lockMap.computeIfAbsent(fileName, key -> new ReentrantLock());
         try {
-            if (lock == null) {
-                localLock.lock();
-            } else {
-                lock.lock();
-            }
+            lock.lock();
             File file = new File(fileName);
             File fileParent = file.getParentFile();
             if (fileParent == null || (fileParent.exists() && fileParent.isDirectory() && fileParent.canWrite())) {
@@ -118,11 +113,7 @@ public class StringToFile extends AbstractFunction {
                 return false;
             }
         } finally {
-            if (lock == null) {
-                localLock.unlock();
-            } else {
-                lock.unlock();
-            }
+            lock.unlock();
         }
         return true;
     }