| package org.eclipse.stem.core; |
| |
| import java.util.ArrayList; |
| |
| |
| /******************************************************************************* |
| * Copyright (c) 2012 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| |
| |
| /** |
| * A simple object pool used to avoid excessive object creation. The pool allows clients to retrieve pre-initialized objects of a given |
| * kind from a pool of available objects. When retrieved, the object becomes unavailable and other clients cannot use it concurrently. |
| * The client must make sure to return the object to the pool when done with it. |
| * |
| * You specify the type of the object by passing a sample and the clone() method is used to create new instances. |
| * |
| * Clients must implement the createNewObject() method |
| * |
| * TODO: Periodically maintain the size of the pool and shrink when not used |
| * @author edlund |
| * |
| */ |
| public abstract class STEMObjectPool { |
| |
| ArrayList<Object> freeObjects = new ArrayList<Object>(); |
| int growthInc; |
| |
| @SuppressWarnings("rawtypes") |
| public STEMObjectPool(int initialSize, int growthIncrement) { |
| this.growthInc = growthIncrement <=0? 1:growthIncrement; // safe |
| |
| try { |
| for(int i=0;i<initialSize;++i) |
| freeObjects.add(createNewObject()); |
| } catch(Exception e) { |
| CorePlugin.logError("Error growing STEM object pool", e); |
| } |
| } |
| |
| public synchronized Object get() { |
| if(freeObjects.size() > 0) |
| return freeObjects.remove(freeObjects.size()-1); |
| // Grow pool |
| try { |
| for(int i=0;i<growthInc;++i) { |
| freeObjects.add(createNewObject()); |
| } |
| } catch(Exception e) { |
| CorePlugin.logError("Error growing STEM object pool", e); |
| } |
| return freeObjects.remove(freeObjects.size()-1); |
| } |
| |
| public synchronized void release(Object o) { |
| freeObjects.add(o); |
| } |
| |
| protected abstract Object createNewObject(); // Must be implemented by subclass |
| |
| } |