package com.dbxml.util;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: ObjectPool.java,v 1.2 2004/02/12 00:17:58 bradford Exp $
 */

/**
 * ObjectPool is an abstract Object Pool implementation.  In order to
 * create a working ObjectPool, one must extend ObjectPool and implement
 * the createObject method.
 */

public abstract class ObjectPool {
   private ObjectQueue pool = new ObjectQueue();

   /**
    * createObject needs to be implemented in order to allow the ObjectPool
    * to produce Pooled objects.
    *
    * @return Poolable a new Poolable object instance
    */
   protected abstract Poolable createObject();

   /**
    * getObject retrieves a Poolable object from the Object pool, creating
    * a new instance if necessary.
    *
    * @return Poolable a Poolable object instance
    */
   public Poolable getObject() {
      Poolable result = (Poolable)pool.remove();
      if ( result == null ) {
         result = createObject();
         result.setPool(this);
      }
      return result;
   }

   /**
    * putObject should be called by Poolable objects or consumers of Poolable
    * objects to return a Pooled object to the Object Pool.
    *
    * @param object The Poolable object instance
    */
   public void putObject(Poolable object) {
      pool.add(object);
   }

   /**
    * isEmpty returns whether the ObjectPool is currently empty.
    *
    * @return Whether to ObjectPool is empty
    */
   public boolean isEmpty() {
      return pool.isEmpty();
   }
}

