ObjectPool.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 
00030 package org.openmobileis.common.util;
00031 
00032 import org.openmobileis.common.util.collection.Array;
00033 
00054 public abstract class ObjectPool {
00055   protected java.util.Stack pool;
00056   protected Array arrayPool;
00057   protected Class objectClass;
00058   protected int minSize, incSize;
00059 
00072   protected ObjectPool(String objectClass, int minSize, int incSize) throws ClassNotFoundException, IllegalAccessException, InstantiationException  {
00073     this.pool = new java.util.Stack();
00074     this.objectClass = Class.forName(objectClass);
00075     if (minSize == 0) {
00076       this.minSize = 10;
00077     } else  {
00078       this.minSize = minSize;
00079     }
00080     this.incSize = incSize;
00081     this.arrayPool = new Array(minSize);
00082     createInPool(minSize);
00083   }
00084 
00085   private final void createInPool(int size) throws IllegalAccessException, InstantiationException {
00086     for (int i=0; i<size; i++)  {
00087       Object obj = createNewInstance(objectClass);
00088       this.pool.push(obj);
00089       this.arrayPool.add(obj);
00090     }
00091   }
00092 
00097   protected Object createNewInstance(Class objectClass) throws IllegalAccessException, InstantiationException {
00098         return objectClass.newInstance();
00099   }
00100 
00111   protected final synchronized Object getObject() {
00112     try {
00113       return this.pool.pop();
00114     } catch(java.util.EmptyStackException ex) {
00115       try {
00116         createInPool(this.incSize);
00117                     return pool.pop();
00118       } catch (Exception e) {
00119         // pool empty can't grow.
00120       }
00121     }
00122     return null;
00123   }
00124 
00136   public synchronized void disposeObject(Object object) {
00137     if (object != null) {
00138       this.pool.push( object );
00139     }
00140   }
00141 
00142 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1