1 6 7 package org.jfox.pool; 8 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 15 16 public class SimpleIndexableObjectPool extends AbstractIndexableObjectPool { 17 18 private Map indexedObjects = null; 19 20 public SimpleIndexableObjectPool(ObjectFactory factory) { 21 super(factory); 22 } 23 24 public SimpleIndexableObjectPool(String objectFactoryClassName, String poolableClassName) { 25 super(objectFactoryClassName, poolableClassName); 26 } 27 28 protected void doInit() throws Exception { 29 super.doInit(); 30 indexedObjects = new HashMap (); 31 } 32 33 protected void doDestroy() throws Exception { 34 super.doDestroy(); 35 indexedObjects = null; 36 } 37 38 public void clear() { 39 super.clear(); 40 indexedObjects.clear(); 41 } 42 43 public PoolableObject retrieveObject(Object key) throws Exception { 44 if(indexedObjects.containsKey(key)) { 45 return (PoolableObject) indexedObjects.remove(key); 46 } 47 else { 48 return pool.retrieveObject(); 49 } 50 51 } 52 53 public boolean restoreObject(Object key, PoolableObject obj) { 54 if(!factory.validateObject(obj)) { 55 logger.warn("PoolableObject " + obj + " is not a valid pool object in this ObjectPool"); 56 return false; 57 } 58 59 if(indexedObjects.containsKey(key)) { 60 logger.warn("Key " + key + " already exists, will overwrite!"); 61 } 62 indexedObjects.put(key, obj); 63 return true; 64 65 } 66 67 public boolean removeObject(Object key) { 68 if(!indexedObjects.containsKey(key)) { 69 logger.warn("Key " + key + " is not exists!"); 70 return false; 71 } 72 else { 73 indexedObjects.remove(key); 74 return true; 75 } 76 } 77 78 public static void main(String [] args) { 79 80 } 81 } 82 | Popular Tags |