Skip to content
Permalink
Browse files
Add KeyedObjectPool.getKeys()
  • Loading branch information
Gary Gregory committed Apr 15, 2022
1 parent 834a25d commit ccd36019f3a87b38405a8ba3ddcacfac855bdb7b
Showing 6 changed files with 52 additions and 6 deletions.
@@ -64,6 +64,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Vamsi Pavan Kumar Sanka, Phil Steitz, Gary Gregory">
Add GenericKeyedObjectPool.getKeys().
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add KeyedObjectPool.getKeys().
</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump actions/checkout from 2.3.4 to 3 #109, #112, #134.
@@ -18,6 +18,8 @@

import java.io.Closeable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

/**
@@ -210,6 +212,16 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA
@Override
void close();

/**
* Gets a copy of the pool key list.
*
* @return a copy of the pool key list.
* @since 2.12.0
*/
default List<K> getKeys() {
return Collections.emptyList();
}

/**
* Gets the total number of instances currently borrowed from this pool but
* not yet returned. Returns a negative value if this information is not
@@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Timer;
@@ -223,6 +224,14 @@ protected KeyedObjectPool<K, V> getKeyedPool() {
return keyedPool;
}

/**
* {@inheritDoc}
*/
@Override
public List<K> getKeys() {
return keyedPool.getKeys();
}

/**
* {@inheritDoc}
*/
@@ -681,7 +690,7 @@ public String toString() {
* @param <K> object pool key type
* @param <V> object pool value type
*/
private static final class SynchronizedKeyedObjectPool<K, V> implements
static final class SynchronizedKeyedObjectPool<K, V> implements
KeyedObjectPool<K, V> {

/**
@@ -785,6 +794,20 @@ public void close() {
}
}

/**
* {@inheritDoc}
*/
@Override
public List<K> getKeys() {
final ReadLock readLock = readWriteLock.readLock();
readLock.lock();
try {
return keyedPool.getKeys();
} finally {
readLock.unlock();
}
}

/**
* {@inheritDoc}
*/
@@ -1112,6 +1112,7 @@ public KeyedPooledObjectFactory<K, T> getFactory() {
* @return a copy of the pool key list.
* @since 2.12.0
*/
@Override
@SuppressWarnings("unchecked")
public List<K> getKeys() {
return (List<K>) poolKeyList.clone();
@@ -16,6 +16,7 @@
*/
package org.apache.commons.pool2.proxy;

import java.util.List;
import java.util.NoSuchElementException;

import org.apache.commons.pool2.KeyedObjectPool;
@@ -83,6 +84,11 @@ public void close() {
pool.close();
}

@Override
public List<K> getKeys() {
return pool.getKeys();
}

@Override
public int getNumActive() {
return pool.getNumActive();
@@ -100,12 +100,13 @@ private static <T> T createProxy(final Class<T> clazz, final List<String> logger
return createProxy(clazz, new MethodCallLogger(logger));
}

private static List<String> invokeEveryMethod(final KeyedObjectPool<Object,Object> kop) throws Exception {
private static List<String> invokeEveryMethod(final KeyedObjectPool<Object, Object> kop) throws Exception {
kop.addObject(null);
kop.borrowObject(null);
kop.clear();
kop.clear(null);
kop.close();
kop.getKeys();
kop.getNumActive();
kop.getNumActive(null);
kop.getNumIdle();
@@ -114,8 +115,8 @@ private static List<String> invokeEveryMethod(final KeyedObjectPool<Object,Objec
kop.returnObject(null, new Object());
kop.toString();

return Arrays.asList("addObject", "borrowObject", "clear", "clear", "close", "getNumActive", "getNumActive",
"getNumIdle", "getNumIdle", "invalidateObject", "returnObject", "toString");
return Arrays.asList("addObject", "borrowObject", "clear", "clear", "close", "getKeys", "getNumActive", "getNumActive", "getNumIdle", "getNumIdle",
"invalidateObject", "returnObject", "toString");
}

private static <K, V> List<String> invokeEveryMethod(final KeyedPooledObjectFactory<K, V> kpof) throws Exception {
@@ -748,8 +749,8 @@ public void testSynchronizedPoolKeyedObjectPool() throws Exception {

final List<String> calledMethods = new ArrayList<>();
try (@SuppressWarnings("unchecked")
final KeyedObjectPool<Object, Object> kop = createProxy(KeyedObjectPool.class, calledMethods);
final KeyedObjectPool<Object, Object> skop = PoolUtils.synchronizedPool(kop)) {
final KeyedObjectPool<Object, Object> kop = createProxy(KeyedObjectPool.class, calledMethods);
final KeyedObjectPool<Object, Object> skop = PoolUtils.synchronizedPool(kop)) {
final List<String> expectedMethods = invokeEveryMethod(skop);
assertEquals(expectedMethods, calledMethods);
}

0 comments on commit ccd3601

Please sign in to comment.