Class PoolUtils.CheckedObjectPool<T>

  • All Implemented Interfaces:
    ObjectPool<T>
    Enclosing class:
    PoolUtils

    private static class PoolUtils.CheckedObjectPool<T>
    extends java.lang.Object
    implements ObjectPool<T>
    An object pool that performs type checking on objects passed to pool methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private ObjectPool<T> pool
      Underlying object pool
      private java.lang.Class<T> type
      Type of objects allowed in the pool.
    • Constructor Summary

      Constructors 
      Constructor Description
      CheckedObjectPool​(ObjectPool<T> pool, java.lang.Class<T> type)
      Create a CheckedObjectPool accepting objects of the given type using the given pool.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addObject()
      Create an object using the factory or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.
      T borrowObject()
      Borrow an object from the pool, checking its type.
      void clear()
      Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).
      void close()
      Close this pool, and free any resources associated with it.
      int getNumActive()
      Return the number of instances currently borrowed from this pool (optional operation).
      int getNumIdle()
      Return the number of instances currently idle in this pool (optional operation).
      void invalidateObject​(T obj)
      Invalidates an object from the pool, verifying that it is of the expected type.
      void returnObject​(T obj)
      Return an object to the pool, verifying that it is of the correct type.
      void setFactory​(PoolableObjectFactory<T> factory)
      Deprecated.
      to be removed in version 2.0
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • type

        private final java.lang.Class<T> type
        Type of objects allowed in the pool. This should be a subtype of the return type of the underlying pool's associated object factory.
      • pool

        private final ObjectPool<T> pool
        Underlying object pool
    • Constructor Detail

      • CheckedObjectPool

        CheckedObjectPool​(ObjectPool<T> pool,
                          java.lang.Class<T> type)
        Create a CheckedObjectPool accepting objects of the given type using the given pool.
        Parameters:
        pool - underlying object pool
        type - expected pooled object type
        Throws:
        java.lang.IllegalArgumentException - if either parameter is null
    • Method Detail

      • borrowObject

        public T borrowObject()
                       throws java.lang.Exception,
                              java.util.NoSuchElementException,
                              java.lang.IllegalStateException
        Borrow an object from the pool, checking its type.
        Specified by:
        borrowObject in interface ObjectPool<T>
        Returns:
        a type-checked object from the pool
        Throws:
        java.lang.ClassCastException - if the object returned by the pool is not of the expected type
        java.lang.IllegalStateException - after close has been called on this pool.
        java.lang.Exception - when makeObject throws an exception.
        java.util.NoSuchElementException - when the pool is exhausted and cannot or will not return another instance.
      • returnObject

        public void returnObject​(T obj)
        Return an object to the pool, verifying that it is of the correct type.
        Specified by:
        returnObject in interface ObjectPool<T>
        Parameters:
        obj - object to return
        Throws:
        java.lang.ClassCastException - if obj is not of the expected type
      • invalidateObject

        public void invalidateObject​(T obj)
        Invalidates an object from the pool, verifying that it is of the expected type.
        Specified by:
        invalidateObject in interface ObjectPool<T>
        Parameters:
        obj - object to invalidate
        Throws:
        java.lang.ClassCastException - if obj is not of the expected type
      • addObject

        public void addObject()
                       throws java.lang.Exception,
                              java.lang.IllegalStateException,
                              java.lang.UnsupportedOperationException
        Create an object using the factory or other implementation dependent mechanism, passivate it, and then place it in the idle object pool. addObject is useful for "pre-loading" a pool with idle objects. (Optional operation).
        Specified by:
        addObject in interface ObjectPool<T>
        Throws:
        java.lang.Exception - when PoolableObjectFactory.makeObject() fails.
        java.lang.IllegalStateException - after ObjectPool.close() has been called on this pool.
        java.lang.UnsupportedOperationException - when this pool cannot add new idle objects.
      • getNumIdle

        public int getNumIdle()
                       throws java.lang.UnsupportedOperationException
        Return the number of instances currently idle in this pool (optional operation). This may be considered an approximation of the number of objects that can be borrowed without creating any new instances. Returns a negative value if this information is not available.
        Specified by:
        getNumIdle in interface ObjectPool<T>
        Returns:
        the number of instances currently idle in this pool or a negative value if unsupported
        Throws:
        java.lang.UnsupportedOperationException - deprecated: if this implementation does not support the operation
      • getNumActive

        public int getNumActive()
                         throws java.lang.UnsupportedOperationException
        Return the number of instances currently borrowed from this pool (optional operation). Returns a negative value if this information is not available.
        Specified by:
        getNumActive in interface ObjectPool<T>
        Returns:
        the number of instances currently borrowed from this pool or a negative value if unsupported
        Throws:
        java.lang.UnsupportedOperationException - deprecated: if this implementation does not support the operation
      • clear

        public void clear()
                   throws java.lang.Exception,
                          java.lang.UnsupportedOperationException
        Clears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must be destroyed.
        Specified by:
        clear in interface ObjectPool<T>
        Throws:
        java.lang.UnsupportedOperationException - if this implementation does not support the operation
        java.lang.Exception
      • setFactory

        @Deprecated
        public void setFactory​(PoolableObjectFactory<T> factory)
                        throws java.lang.IllegalStateException,
                               java.lang.UnsupportedOperationException
        Deprecated.
        to be removed in version 2.0
        Sets the object factory associated with the pool
        Specified by:
        setFactory in interface ObjectPool<T>
        Parameters:
        factory - object factory
        Throws:
        java.lang.IllegalStateException - when the factory cannot be set at this time
        java.lang.UnsupportedOperationException - if this implementation does not support the operation
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object