Skip to content
Permalink
Browse files
Pool 269 generic exceptions (#143)
* [POOL-269] Use a generic exception instead of Exception.

Generic Exception for ObjectPool.

* [POOL-269] Use a generic exception instead of Exception.

Generic Exception for KeyedPooledObjectFactory.

* [POOL-269] Use a generic exception instead of Exception.

Generic Exception for KeyedObjectPool.

* [POOL-269] Use a generic exception instead of Exception.

Generic Exception for KeyedObjectPool.

* [POOL-269] Use a generic exception instead of Exception.

Wait a little more.

Co-authored-by: Gary Gregory <ggregory@rocketsoftware.com>
  • Loading branch information
garydgregory and Gary Gregory committed Apr 21, 2022
1 parent 727d91d commit a6148a72197f10c43170847d9e7193b2872df8f2
Showing 44 changed files with 718 additions and 701 deletions.
@@ -29,11 +29,11 @@
*
* @param <K> The type of keys managed by this factory.
* @param <V> Type of element managed by this factory.
* @param <E> Type of exception thrown by this factory.
*
* @since 2.0
*/
public abstract class BaseKeyedPooledObjectFactory<K, V> extends BaseObject
implements KeyedPooledObjectFactory<K, V> {
public abstract class BaseKeyedPooledObjectFactory<K, V, E extends Exception> extends BaseObject implements KeyedPooledObjectFactory<K, V, E> {

/**
* Reinitialize an instance to be returned by the pool.
@@ -45,8 +45,7 @@
* @param p a {@code PooledObject} wrapping the instance to be activated
*/
@Override
public void activateObject(final K key, final PooledObject<V> p)
throws Exception {
public void activateObject(final K key, final PooledObject<V> p) throws E {
// The default implementation is a no-op.
}

@@ -56,11 +55,10 @@ public void activateObject(final K key, final PooledObject<V> p)
* @param key the key used when constructing the object
* @return an instance that can be served by the pool
*
* @throws Exception if there is a problem creating a new instance,
* @throws E if there is a problem creating a new instance,
* this will be propagated to the code requesting an object.
*/
public abstract V create(K key)
throws Exception;
public abstract V create(K key) throws E;

/**
* Destroy an instance no longer needed by the pool.
@@ -72,13 +70,12 @@ public abstract V create(K key)
* @param p a {@code PooledObject} wrapping the instance to be destroyed
*/
@Override
public void destroyObject(final K key, final PooledObject<V> p)
throws Exception {
public void destroyObject(final K key, final PooledObject<V> p) throws E {
// The default implementation is a no-op.
}

@Override
public PooledObject<V> makeObject(final K key) throws Exception {
public PooledObject<V> makeObject(final K key) throws E {
return wrap(create(key));
}

@@ -92,8 +89,7 @@ public PooledObject<V> makeObject(final K key) throws Exception {
* @param p a {@code PooledObject} wrapping the instance to be passivated
*/
@Override
public void passivateObject(final K key, final PooledObject<V> p)
throws Exception {
public void passivateObject(final K key, final PooledObject<V> p) throws E {
// The default implementation is a no-op.
}

@@ -25,10 +25,11 @@
* </p>
*
* @param <T> Type of element pooled in this pool.
* @param <E> Type of exception thrown by this pool.
*
* @since 2.0
*/
public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> {
public abstract class BaseObjectPool<T, E extends Exception> extends BaseObject implements ObjectPool<T, E> {

private volatile boolean closed;

@@ -40,7 +41,7 @@
* method
*/
@Override
public void addObject() throws Exception, UnsupportedOperationException {
public void addObject() throws E, UnsupportedOperationException {
throw new UnsupportedOperationException();
}

@@ -59,7 +60,7 @@ protected final void assertOpen() throws IllegalStateException {
}

@Override
public abstract T borrowObject() throws Exception;
public abstract T borrowObject() throws E;

/**
* Not supported in this base implementation.
@@ -68,7 +69,7 @@ protected final void assertOpen() throws IllegalStateException {
* method
*/
@Override
public void clear() throws Exception, UnsupportedOperationException {
public void clear() throws E, UnsupportedOperationException {
throw new UnsupportedOperationException();
}

@@ -105,7 +106,7 @@ public int getNumIdle() {
}

@Override
public abstract void invalidateObject(T obj) throws Exception;
public abstract void invalidateObject(T obj) throws E;

/**
* Has this pool instance been closed.
@@ -117,7 +118,7 @@ public final boolean isClosed() {
}

@Override
public abstract void returnObject(T obj) throws Exception;
public abstract void returnObject(T obj) throws E;

@Override
protected void toStringAppendFields(final StringBuilder builder) {
@@ -24,21 +24,22 @@
* This class is immutable, and therefore thread-safe
*
* @param <T> Type of element managed in this factory.
* @param <E> Type of exception thrown in this factory.
*
* @see PooledObjectFactory
* @see BaseKeyedPooledObjectFactory
*
* @since 2.0
*/
public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {
public abstract class BasePooledObjectFactory<T, E extends Exception> extends BaseObject implements PooledObjectFactory<T, E> {

/**
* No-op.
*
* @param p ignored
*/
@Override
public void activateObject(final PooledObject<T> p) throws Exception {
public void activateObject(final PooledObject<T> p) throws E {
// The default implementation is a no-op.
}

@@ -49,10 +50,10 @@ public void activateObject(final PooledObject<T> p) throws Exception {
*
* @return an instance to be served by the pool
*
* @throws Exception if there is a problem creating a new instance,
* @throws E if there is a problem creating a new instance,
* this will be propagated to the code requesting an object.
*/
public abstract T create() throws Exception;
public abstract T create() throws E;

/**
* No-op.
@@ -61,12 +62,12 @@ public void activateObject(final PooledObject<T> p) throws Exception {
*/
@Override
public void destroyObject(final PooledObject<T> p)
throws Exception {
throws E {
// The default implementation is a no-op.
}

@Override
public PooledObject<T> makeObject() throws Exception {
public PooledObject<T> makeObject() throws E {
return wrap(create());
}

@@ -77,7 +78,7 @@ public PooledObject<T> makeObject() throws Exception {
*/
@Override
public void passivateObject(final PooledObject<T> p)
throws Exception {
throws E {
// The default implementation is a no-op.
}

@@ -61,14 +61,15 @@
*
* @param <K> The type of keys maintained by this pool.
* @param <V> Type of element pooled in this pool.
* @param <E> Type of exception thrown by this pool.
*
* @see KeyedPooledObjectFactory
* @see ObjectPool
* @see org.apache.commons.pool2.impl.GenericKeyedObjectPool GenericKeyedObjectPool
*
* @since 2.0
*/
public interface KeyedObjectPool<K, V> extends Closeable {
public interface KeyedObjectPool<K, V, E extends Exception> extends Closeable {

/**
* Creates an object using the {@link KeyedPooledObjectFactory factory} or
@@ -78,15 +79,14 @@
*
* @param key the key a new instance should be added to
*
* @throws Exception
* @throws E
* when {@link KeyedPooledObjectFactory#makeObject} fails.
* @throws IllegalStateException
* after {@link #close} has been called on this pool.
* @throws UnsupportedOperationException
* when this pool cannot add new idle objects.
*/
void addObject(K key) throws Exception, IllegalStateException,
UnsupportedOperationException;
void addObject(K key) throws E, IllegalStateException, UnsupportedOperationException;

/**
* Calls {@link KeyedObjectPool#addObject(Object)} with each
@@ -98,14 +98,14 @@ void addObject(K key) throws Exception, IllegalStateException,
* {@link Collection} of keys to add objects for.
* @param count
* the number of idle objects to add for each {@code key}.
* @throws Exception
* @throws E
* when {@link KeyedObjectPool#addObject(Object)} fails.
* @throws IllegalArgumentException
* when {@code keyedPool}, {@code keys}, or any value
* in {@code keys} is {@code null}.
* @see #addObjects(Object, int)
*/
default void addObjects(final Collection<K> keys, final int count) throws Exception, IllegalArgumentException {
default void addObjects(final Collection<K> keys, final int count) throws E, IllegalArgumentException {
if (keys == null) {
throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEYS);
}
@@ -122,13 +122,13 @@ default void addObjects(final Collection<K> keys, final int count) throws Except
* the key to add objects for.
* @param count
* the number of idle objects to add for {@code key}.
* @throws Exception
* @throws E
* when {@link KeyedObjectPool#addObject(Object)} fails.
* @throws IllegalArgumentException
* when {@code key} is {@code null}.
* @since 2.8.0
*/
default void addObjects(final K key, final int count) throws Exception, IllegalArgumentException {
default void addObjects(final K key, final int count) throws E, IllegalArgumentException {
if (key == null) {
throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEY);
}
@@ -166,24 +166,24 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA
*
* @throws IllegalStateException
* after {@link #close close} has been called on this pool
* @throws Exception
* @throws E
* when {@link KeyedPooledObjectFactory#makeObject
* makeObject} throws an exception
* @throws NoSuchElementException
* when the pool is exhausted and cannot or will not return
* another instance
*/
V borrowObject(K key) throws Exception, NoSuchElementException, IllegalStateException;
V borrowObject(K key) throws E, NoSuchElementException, IllegalStateException;

/**
* Clears the pool, removing all pooled instances (optional operation).
*
* @throws UnsupportedOperationException when this implementation doesn't
* support the operation
*
* @throws Exception if the pool cannot be cleared
* @throws E if the pool cannot be cleared
*/
void clear() throws Exception, UnsupportedOperationException;
void clear() throws E, UnsupportedOperationException;

/**
* Clears the specified pool, removing all pooled instances corresponding to
@@ -194,9 +194,9 @@ default void addObjects(final K key, final int count) throws Exception, IllegalA
* @throws UnsupportedOperationException when this implementation doesn't
* support the operation
*
* @throws Exception if the key cannot be cleared
* @throws E if the key cannot be cleared
*/
void clear(K key) throws Exception, UnsupportedOperationException;
void clear(K key) throws E, UnsupportedOperationException;

/**
* Closes this pool, and free any resources associated with it.
@@ -277,9 +277,9 @@ default List<K> getKeys() {
* @param key the key used to obtain the object
* @param obj a {@link #borrowObject borrowed} instance to be returned.
*
* @throws Exception if the instance cannot be invalidated
* @throws E if the instance cannot be invalidated
*/
void invalidateObject(K key, V obj) throws Exception;
void invalidateObject(K key, V obj) throws E;

/**
* Invalidates an object from the pool, using the provided
@@ -300,10 +300,10 @@ default List<K> getKeys() {
* @param obj a {@link #borrowObject borrowed} instance to be returned.
* @param destroyMode destroy activation context provided to the factory
*
* @throws Exception if the instance cannot be invalidated
* @throws E if the instance cannot be invalidated
* @since 2.9.0
*/
default void invalidateObject(final K key, final V obj, final DestroyMode destroyMode) throws Exception {
default void invalidateObject(final K key, final V obj, final DestroyMode destroyMode) throws E {
invalidateObject(key, obj);
}

@@ -324,7 +324,7 @@ default void invalidateObject(final K key, final V obj, final DestroyMode destro
* to return an object that was never borrowed from the pool
* will trigger this exception.
*
* @throws Exception if an instance cannot be returned to the pool
* @throws E if an instance cannot be returned to the pool
*/
void returnObject(K key, V obj) throws Exception;
void returnObject(K key, V obj) throws E;
}

0 comments on commit a6148a7

Please sign in to comment.