in src/main/java/org/apache/commons/pool3/impl/GenericKeyedObjectPool.java [647:682]
public void clearOldest() {
// build sorted map of idle objects
final TreeMap<PooledObject<T>, K> map = new TreeMap<>();
poolMap.forEach((key, value) -> {
// Each item into the map using the PooledObject object as the
// key. It then gets sorted based on the idle time
value.getIdleObjects().forEach(p -> map.put(p, key));
});
// Now iterate created map and kill the first 15% plus one to account
// for zero
int itemsToRemove = (int) (map.size() * 0.15) + 1;
final Iterator<Entry<PooledObject<T>, K>> iter = map.entrySet().iterator();
while (iter.hasNext() && itemsToRemove > 0) {
final Entry<PooledObject<T>, K> entry = iter.next();
// kind of backwards on naming. In the map, each key is the
// PooledObject because it has the ordering with the timestamp
// value. Each value that the key references is the key of the
// list it belongs to.
final K key = entry.getValue();
final PooledObject<T> p = entry.getKey();
// Assume the destruction succeeds
boolean destroyed = true;
try {
destroyed = destroy(key, p, false, DestroyMode.NORMAL);
} catch (final Exception e) {
swallowException(e);
}
if (destroyed) {
itemsToRemove--;
}
}
}