You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/07/07 13:26:55 UTC

[commons-pool] branch master updated: Use modern Map APIs instead of manually operating on the Map

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git


The following commit(s) were added to refs/heads/master by this push:
     new c228925c Use modern Map APIs instead of manually operating on the Map
     new aa42480e Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-pool.git
c228925c is described below

commit c228925c6d1066232bb80ef19025fbb555c6d16f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jul 7 09:26:37 2023 -0400

    Use modern Map APIs instead of manually operating on the Map
    
    Refactor to use internal constants
---
 .../org/apache/commons/pool2/WaiterFactory.java    | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/src/test/java/org/apache/commons/pool2/WaiterFactory.java b/src/test/java/org/apache/commons/pool2/WaiterFactory.java
index 661c929e..b3c6a8d3 100644
--- a/src/test/java/org/apache/commons/pool2/WaiterFactory.java
+++ b/src/test/java/org/apache/commons/pool2/WaiterFactory.java
@@ -33,6 +33,12 @@ import org.apache.commons.pool2.impl.DefaultPooledObject;
  */
 public class WaiterFactory<K> implements PooledObjectFactory<Waiter, IllegalStateException>, KeyedPooledObjectFactory<K, Waiter, RuntimeException> {
 
+    /** Integer value 0. */
+    private static final Integer ZERO = Integer.valueOf(0);
+
+    /** Integer value 1. */
+    private static final Integer ONE = Integer.valueOf(1);
+
     /** Latency of activateObject */
     private final long activateLatency;
 
@@ -157,19 +163,15 @@ public class WaiterFactory<K> implements PooledObjectFactory<Waiter, IllegalStat
     @Override
     public PooledObject<Waiter> makeObject(final K key) {
         synchronized (this) {
-            Integer count = activeCounts.get(key);
-            if (count == null) {
-                count = Integer.valueOf(1);
-                activeCounts.put(key, count);
-            } else {
-                if (count.intValue() >= maxActivePerKey) {
+            activeCounts.merge(key, ONE, (v1, v2) -> {
+                if (v1.intValue() >= maxActivePerKey) {
                     throw new IllegalStateException("Too many active " +
-                    "instances for key = " + key + ": " + count.intValue() +
+                    "instances for key = " + key + ": " + v1.intValue() +
                     " in circulation " + "with maxActivePerKey = " +
                     maxActivePerKey);
                 }
-                activeCounts.put(key, Integer.valueOf(count.intValue() + 1));
-            }
+                return Integer.valueOf(v1.intValue() + 1);
+            });
         }
         return makeObject();
     }
@@ -194,7 +196,7 @@ public class WaiterFactory<K> implements PooledObjectFactory<Waiter, IllegalStat
             return;
         }
         for (K key : activeCounts.keySet()) {
-            activeCounts.put(key, Integer.valueOf(0));
+            activeCounts.put(key, ZERO);
         }
     }