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 2019/10/08 15:07:50 UTC

[commons-pool] branch master updated: [POOL-379] Deprecate PoolUtils.prefill(KeyedObjectPool, K, int) in favor of KeyedObjectPool.addObjects(K, int).

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 ca96e5e  [POOL-379] Deprecate PoolUtils.prefill(KeyedObjectPool<K, V>, K, int) in favor of KeyedObjectPool.addObjects(K, int).
ca96e5e is described below

commit ca96e5efff1dfd81eeac82ed330f9762fe2bb76d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Oct 8 11:07:45 2019 -0400

    [POOL-379] Deprecate PoolUtils.prefill(KeyedObjectPool<K, V>, K, int) in
    favor of KeyedObjectPool.addObjects(K, int).
---
 src/changes/changes.xml                            |  3 +++
 .../org/apache/commons/pool2/KeyedObjectPool.java  | 23 +++++++++++++++++
 .../java/org/apache/commons/pool2/ObjectPool.java  |  4 +--
 .../java/org/apache/commons/pool2/PoolUtils.java   | 17 +++++--------
 .../apache/commons/pool2/TestKeyedObjectPool.java  |  8 +++---
 .../org/apache/commons/pool2/TestPoolUtils.java    | 29 +++++++++++-----------
 .../pool2/impl/TestGenericKeyedObjectPool.java     |  2 +-
 7 files changed, 52 insertions(+), 34 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e780ab8..41f66b0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,9 @@ The <action> type attribute can be add,update,fix,remove.
     <action dev="ggregory" issue="POOL-378" type="add" due-to="Gary Gregory">
       Deprecate PoolUtils.prefill(ObjectPool, int) in favor of ObjectPool.addObjects(int).
     </action>
+    <action dev="ggregory" issue="POOL-379" type="add" due-to="Gary Gregory">
+      Deprecate PoolUtils.prefill(KeyedObjectPool, K, int) in favor of KeyedObjectPool.addObjects(K, int).
+    </action>
   </release>
   <release version="2.7.0" date="2019-07-25" description="This is a feature release (Java 8).">
     <action dev="ggregory" issue="POOL-364" type="update" due-to="Gary Gregory">
diff --git a/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java
index d2419eb..b791f0a 100644
--- a/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java
@@ -86,6 +86,29 @@ public interface KeyedObjectPool<K, V> extends Closeable {
             UnsupportedOperationException;
 
     /**
+     * Calls {@link KeyedObjectPool#addObject(Object)} on <code>keyedPool</code> with
+     * <code>key</code> <code>count</code> number of times.
+     *
+     * @param key
+     *            the key to add objects for.
+     * @param count
+     *            the number of idle objects to add for <code>key</code>.
+     * @throws Exception
+     *             when {@link KeyedObjectPool#addObject(Object)} fails.
+     * @throws IllegalArgumentException
+     *             when <code>key</code> is <code>null</code>.
+     * @since 2.8.0
+     */
+    default void addObjects(final K key, final int count) throws Exception, IllegalArgumentException {
+        if (key == null) {
+            throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEY);
+        }
+        for (int i = 0; i < count; i++) {
+            addObject(key);
+        }
+    }
+    
+    /**
      * Obtains an instance from this pool for the specified <code>key</code>.
      * <p>
      * Instances returned from this method will have been either newly created
diff --git a/src/main/java/org/apache/commons/pool2/ObjectPool.java b/src/main/java/org/apache/commons/pool2/ObjectPool.java
index ba34fb4..18aa41e 100644
--- a/src/main/java/org/apache/commons/pool2/ObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/ObjectPool.java
@@ -83,11 +83,9 @@ public interface ObjectPool<T> extends Closeable {
      *            the number of idle objects to add.
      * @throws Exception
      *             when {@link ObjectPool#addObject()} fails.
-     * @throws IllegalArgumentException
-     *             when <code>pool</code> is <code>null</code>.
      * @since 2.8.0
      */
-    default void addObjects(final int count) throws Exception, IllegalArgumentException {
+    default void addObjects(final int count) throws Exception {
         for (int i = 0; i < count; i++) {
             addObject();
         }
diff --git a/src/main/java/org/apache/commons/pool2/PoolUtils.java b/src/main/java/org/apache/commons/pool2/PoolUtils.java
index a7c9bd4..9d3d3c0 100644
--- a/src/main/java/org/apache/commons/pool2/PoolUtils.java
+++ b/src/main/java/org/apache/commons/pool2/PoolUtils.java
@@ -38,7 +38,7 @@ public final class PoolUtils {
 
     private static final String MSG_FACTOR_NEGATIVE = "factor must be positive.";
     private static final String MSG_MIN_IDLE = "minIdle must be non-negative.";
-    private static final String MSG_NULL_KEY = "key must not be null.";
+    static final String MSG_NULL_KEY = "key must not be null.";
     private static final String MSG_NULL_KEYED_POOL = "keyedPool must not be null.";
     private static final String MSG_NULL_KEYS = "keys must not be null.";
     private static final String MSG_NULL_POOL = "pool must not be null.";
@@ -228,9 +228,7 @@ public final class PoolUtils {
         if (pool == null) {
             throw new IllegalArgumentException(MSG_NULL_POOL);
         }
-        for (int i = 0; i < count; i++) {
-            pool.addObject();
-        }
+        pool.addObjects(count);
     }
 
     /**
@@ -250,19 +248,16 @@ public final class PoolUtils {
      * @throws IllegalArgumentException
      *             when <code>keyedPool</code> or <code>key</code> is
      *             <code>null</code>.
+     * @deprecated Use {@link KeyedObjectPool#addObjects(Object, int)}.
      */
+    @Deprecated
     public static <K, V> void prefill(final KeyedObjectPool<K, V> keyedPool,
             final K key, final int count) throws Exception,
             IllegalArgumentException {
         if (keyedPool == null) {
             throw new IllegalArgumentException(MSG_NULL_KEYED_POOL);
         }
-        if (key == null) {
-            throw new IllegalArgumentException(MSG_NULL_KEY);
-        }
-        for (int i = 0; i < count; i++) {
-            keyedPool.addObject(key);
-        }
+        keyedPool.addObjects(key, count);
     }
 
     /**
@@ -294,7 +289,7 @@ public final class PoolUtils {
         }
         final Iterator<K> iter = keys.iterator();
         while (iter.hasNext()) {
-            prefill(keyedPool, iter.next(), count);
+            keyedPool.addObjects(iter.next(), count);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
index 750ae8f..f8329e6 100644
--- a/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
@@ -375,13 +375,13 @@ public abstract class TestKeyedObjectPool {
         final List<MethodCall> expectedMethods = new ArrayList<>();
 
         /// Test correct behavior code paths
-        PoolUtils.prefill(pool, KEY, 5);
+        pool.addObjects(KEY, 5);
         pool.clear();
 
         //// Test exception handling clear should swallow destroy object failures
         reset(pool, factory, expectedMethods);
         factory.setDestroyObjectFail(true);
-        PoolUtils.prefill(pool, KEY, 5);
+        pool.addObjects(KEY, 5);
         pool.clear();
         pool.close();
     }
@@ -398,14 +398,14 @@ public abstract class TestKeyedObjectPool {
         final List<MethodCall> expectedMethods = new ArrayList<>();
 
         /// Test correct behavior code paths
-        PoolUtils.prefill(pool, KEY, 5);
+        pool.addObjects(KEY, 5);
         pool.close();
 
         //// Test exception handling close should swallow failures
         try (final KeyedObjectPool<Object, Object> pool2 = makeEmptyPool(factory)) {
             reset(pool2, factory, expectedMethods);
             factory.setDestroyObjectFail(true);
-            PoolUtils.prefill(pool2, KEY, 5);
+            pool2.addObjects(KEY, 5);
         }
     }
 
diff --git a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
index c06f77d..e5db063 100644
--- a/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
+++ b/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
@@ -17,8 +17,8 @@
 
 package org.apache.commons.pool2;
 
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.InvocationHandler;
@@ -35,17 +35,19 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TimerTask;
 
-import org.junit.Assert;
-import junit.framework.AssertionFailedError;
-
 import org.apache.commons.pool2.impl.DefaultPooledObject;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
 import org.apache.commons.pool2.impl.GenericObjectPool;
-
+import org.apache.commons.pool2.impl.TestGenericKeyedObjectPool;
+import org.junit.Assert;
 import org.junit.Test;
 
+import junit.framework.AssertionFailedError;
+
 /**
  * Unit tests for {@link PoolUtils}.
+ * 
+ * TODO Replace our own mocking with a mocking library like Mockito.
  */
 public class TestPoolUtils {
 
@@ -305,13 +307,11 @@ public class TestPoolUtils {
 
             PoolUtils.prefill(pool, 0);
             final List<String> expectedMethods = new ArrayList<>();
+            expectedMethods.add("addObjects");
             assertEquals(expectedMethods, calledMethods);
 
             calledMethods.clear();
             PoolUtils.prefill(pool, 3);
-            for (int i = 0; i < 3; i++) {
-                expectedMethods.add("addObject");
-            }
             assertEquals(expectedMethods, calledMethods);
         }
     }
@@ -325,7 +325,7 @@ public class TestPoolUtils {
             // expected
         }
         try (@SuppressWarnings("unchecked")
-        final KeyedObjectPool<Object, Object> pool = createProxy(KeyedObjectPool.class, (List<String>) null)) {
+        final KeyedObjectPool<Object, String> pool = new GenericKeyedObjectPool<>(new TestGenericKeyedObjectPool.SimpleFactory<>())) {
             PoolUtils.prefill(pool, (Object) null, 1);
             fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null key.");
         } catch (final IllegalArgumentException iae) {
@@ -338,13 +338,11 @@ public class TestPoolUtils {
 
             PoolUtils.prefill(pool, new Object(), 0);
             final List<String> expectedMethods = new ArrayList<>();
+            expectedMethods.add("addObjects");
             assertEquals(expectedMethods, calledMethods);
 
             calledMethods.clear();
             PoolUtils.prefill(pool, new Object(), 3);
-            for (int i = 0; i < 3; i++) {
-                expectedMethods.add("addObject");
-            }
             assertEquals(expectedMethods, calledMethods);
         }
     }
@@ -372,9 +370,10 @@ public class TestPoolUtils {
             keys.add("one");
             keys.add("two");
             keys.add("three");
-            PoolUtils.prefill(pool, keys, 3);
-            for (int i = 0; i < keys.size() * 3; i++) {
-                expectedMethods.add("addObject");
+            final int count = 3;
+            PoolUtils.prefill(pool, keys, count);
+            for (int i = 0; i < count; i++) {
+                expectedMethods.add("addObjects");
             }
             assertEquals(expectedMethods, calledMethods);
         }
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
index 509fbb6..868b4c5 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
@@ -1809,7 +1809,7 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
         }
     }
 
-    static class SimpleFactory<K> implements KeyedPooledObjectFactory<K,String> {
+    public static class SimpleFactory<K> implements KeyedPooledObjectFactory<K,String> {
         public SimpleFactory() {
             this(true);
         }