/** * Returns a pool that adaptively decreases its size when idle objects are * no longer needed. This is intended as an always thread-safe alternative * to using an idle object evictor provided by many pool implementations. * This is also an effective way to shrink FIFO ordered pools that * experience load spikes. * * @param pool * the ObjectPool to be decorated so it shrinks its idle count * when possible. * @param <T> the type of objects in the pool * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(ObjectPool, float) */ public static <T> ObjectPool<T> erodingPool(final ObjectPool<T> pool) { return erodingPool(pool, 1f); }
/** * Returns a pool that adaptively decreases its size when idle objects are * no longer needed. This is intended as an always thread-safe alternative * to using an idle object evictor provided by many pool implementations. * This is also an effective way to shrink FIFO ordered pools that * experience load spikes. * * @param keyedPool * the KeyedObjectPool to be decorated so it shrinks its idle * count when possible. * @param <K> the type of the pool key * @param <V> the type of pool entries * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(KeyedObjectPool, float) * @see #erodingPool(KeyedObjectPool, float, boolean) */ public static <K, V> KeyedObjectPool<K, V> erodingPool( final KeyedObjectPool<K, V> keyedPool) { return erodingPool(keyedPool, 1f); }
return erodingPool(keyedPool, factor, false);
@Test public void testErodingObjectPoolDefaultFactor() { try (@SuppressWarnings("unchecked") final ObjectPool<Object> internalPool = createProxy(ObjectPool.class, new InvocationHandler() { @Override public Object invoke(final Object arg0, final Method arg1, final Object[] arg2) throws Throwable { return null; } })) { final ObjectPool<Object> pool = PoolUtils.erodingPool(internalPool); final String expectedToString = "ErodingObjectPool{factor=ErodingFactor{factor=1.0, idleHighWaterMark=1}, pool=" + internalPool + "}"; // The factor is not exposed, but will be printed in the toString() method // In this case since we didn't pass one, the default 1.0f will be printed assertEquals(expectedToString, pool.toString()); } }
@Test public void testErodingPoolKeyedObjectPoolDefaultFactor() { try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object, Object> internalPool = createProxy(KeyedObjectPool.class, new InvocationHandler() { @Override public Object invoke(final Object arg0, final Method arg1, final Object[] arg2) throws Throwable { return null; } })) { final KeyedObjectPool<Object, Object> pool = PoolUtils.erodingPool(internalPool); final String expectedToString = "ErodingKeyedObjectPool{factor=ErodingFactor{factor=1.0, idleHighWaterMark=1}, keyedPool=" + internalPool + "}"; // The factor is not exposed, but will be printed in the toString() method // In this case since we didn't pass one, the default 1.0f will be printed assertEquals(expectedToString, pool.toString()); } }
@Test public void testErodingPerKeyKeyedObjectPool() throws Exception { try { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true); fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool."); } catch (final IllegalArgumentException iae) { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 0f, true); fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor."); } catch (final IllegalArgumentException iae) { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true); fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool."); } catch (final IllegalArgumentException iae) { final KeyedObjectPool<Object, Object> pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor, true)) {
@Test public void testErodingPoolObjectPool() throws Exception { try { PoolUtils.erodingPool((ObjectPool<Object>) null); fail("PoolUtils.erodingPool(ObjectPool) must not allow a null pool."); } catch (final IllegalArgumentException iae) { PoolUtils.erodingPool((ObjectPool<Object>) null, 1f); fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a null pool."); } catch (final IllegalArgumentException iae) { final ObjectPool<?> o = PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), -1f)) { fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor."); } catch (final IllegalArgumentException iae) { final List<String> expectedMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final ObjectPool<Object> pool = PoolUtils.erodingPool(createProxy(ObjectPool.class, handler), factor)) {
/** * Returns a pool that adaptively decreases its size when idle objects are * no longer needed. This is intended as an always thread-safe alternative * to using an idle object evictor provided by many pool implementations. * This is also an effective way to shrink FIFO ordered pools that * experience load spikes. * * @param pool * the ObjectPool to be decorated so it shrinks its idle count * when possible. * @param <T> the type of objects in the pool * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(ObjectPool, float) */ public static <T> ObjectPool<T> erodingPool(final ObjectPool<T> pool) { return erodingPool(pool, 1f); }
@Test public void testErodingPoolKeyedObjectPool() throws Exception { try { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null); fail("PoolUtils.erodingPool(KeyedObjectPool) must not allow a null pool."); } catch (final IllegalArgumentException iae) { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f); fail("PoolUtils.erodingPool(KeyedObjectPool, float) must not allow a null pool."); } catch (final IllegalArgumentException iae) { PoolUtils.erodingPool((KeyedObjectPool<Object, Object>) null, 1f, true); fail("PoolUtils.erodingPool(KeyedObjectPool, float, boolean) must not allow a null pool."); } catch (final IllegalArgumentException iae) { final KeyedObjectPool<?, ?> o = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f)) { fail("PoolUtils.erodingPool(ObjectPool, float) must not allow a non-positive factor."); } catch (final IllegalArgumentException iae) { final KeyedObjectPool<?, ?> o = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), 0f, false)) { fail("PoolUtils.erodingPool(ObjectPool, float, boolean) must not allow a non-positive factor."); } catch (final IllegalArgumentException iae) { final List<String> expectedMethods = new ArrayList<>(); try (@SuppressWarnings("unchecked") final KeyedObjectPool<Object, Object> pool = PoolUtils.erodingPool(createProxy(KeyedObjectPool.class, handler), factor)) {
/** * Returns a pool that adaptively decreases its size when idle objects are * no longer needed. This is intended as an always thread-safe alternative * to using an idle object evictor provided by many pool implementations. * This is also an effective way to shrink FIFO ordered pools that * experience load spikes. * * @param keyedPool * the KeyedObjectPool to be decorated so it shrinks its idle * count when possible. * @param <K> the type of the pool key * @param <V> the type of pool entries * @return a pool that adaptively decreases its size when idle objects are * no longer needed. * @see #erodingPool(KeyedObjectPool, float) * @see #erodingPool(KeyedObjectPool, float, boolean) */ public static <K, V> KeyedObjectPool<K, V> erodingPool( final KeyedObjectPool<K, V> keyedPool) { return erodingPool(keyedPool, 1f); }
return erodingPool(keyedPool, factor, false);