FileDocCategorySizeDatePackage
SimpleObjectPool.javaAPI DocApache Lucene 2.1.05504Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.utils

SimpleObjectPool

public class SimpleObjectPool extends Object implements Pool
A Simple implementation of the {@link org.apache.lucene.gdata.utils.Pool} interface using a {@link java.util.Stack} as a buffer for the pooled objects. This implementation does not provide any timeout mechanismn. Objects will stay inside the pool until the pool is destroyed.

If any object will be released e.g. handover to the pool and the pool has already enought objects in the pool the released object will be destroyed. If the pool is empty a new Object will be created.

This implementation does not track any references to the objects aquired by any other resource. The objects must be destroyed manually if not released to the pool after aquired.

author
Simon Willnauer
param

Fields Summary
private static final Log
LOG
private volatile boolean
isDestroyed
private final PoolObjectFactory
factory
static final int
DEFAULTSIZE
static final int
MINIMALSIZE
private final int
size
private final Stack
pool
private final ReentrantReadWriteLock
masterLock
private final Lock
readLock
private final Lock
writeLock
Constructors Summary
public SimpleObjectPool(int size, PoolObjectFactory factory)
Constructs a new {@link SimpleObjectPool} and sets the ObjectFactory and the pool size

param
size - the maximum size of the pool
param
factory - factory to create and destroy pooled objects


                                           
         
        if (factory == null)
            throw new IllegalArgumentException("Factory must not be null");
        this.factory = factory;
        this.size = size < MINIMALSIZE ? MINIMALSIZE : size;
        this.pool = new Stack<Type>();
        for (int i = 0; i < this.size; i++) {
            this.pool.push(this.factory.getInstance());
        }
    
public SimpleObjectPool(PoolObjectFactory factory)

param
factory

        this(DEFAULTSIZE,factory);
        
    
Methods Summary
public Typeaquire()

see
org.apache.lucene.gdata.utils.Pool#aquire()

        // fail if writelock is aquired
        if (this.readLock.tryLock()) {
            try {
                if (this.isDestroyed)
                    throw new IllegalStateException(
                            "The pool has already been closed");
                if (this.pool.isEmpty())
                    return this.factory.getInstance();
                return this.pool.pop();
            } finally {
                this.readLock.unlock();
            }
        }
        throw new IllegalStateException("The pool has already been closed");
    
public voiddestroy()

see
org.apache.lucene.gdata.utils.Pool#destroy()

        this.writeLock.lock();
        try {
            if (this.isDestroyed)
                return;
            this.isDestroyed = true;
            LOG.info("Destroy all elements in the pool -- poolsize: "+this.pool.size());
            for (Type type : this.pool) {
                this.factory.destroyInstance(type);
            }
            this.pool.clear();
        } finally {
            this.writeLock.unlock();
        }

    
public booleanexpires()

see
org.apache.lucene.gdata.utils.Pool#expires()


        return false;
    
public longgetExpireTime()

see
org.apache.lucene.gdata.utils.Pool#getExpireTime()


        return 0;
    
public intgetSize()

see
org.apache.lucene.gdata.utils.Pool#getSize()


        return this.size;
    
public voidrelease(Type type)

param
type - generic type
see
org.apache.lucene.gdata.utils.Pool#release(Object)

        // fail if writelock is aquired
        if (this.readLock.tryLock()) {
            try {
                if (this.pool.size() < this.size && !this.isDestroyed)
                    this.pool.push(type);
                else
                    this.factory.destroyInstance(type);
            } finally {
                this.readLock.unlock();
            }
            return;
        }
        // enable object need to be destoryed
        this.factory.destroyInstance(type);