Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.replica.replicaisland;
     17 
     18 /**
     19  * A general-purpose pool of objects.  Objects in the pool are allocated up front and then
     20  * passed out to requesting objects until the pool is exhausted (at which point an error is thrown).
     21  * Code that requests objects from the pool should return them to the pool when they are finished.
     22  * This class is abstract; derivations need to implement the fill() function to fill the pool, and
     23  * may wish to override release() to clear state on objects as they are returned to the pool.
     24  */
     25 public abstract class ObjectPool extends BaseObject {
     26     private FixedSizeArray<Object> mAvailable;
     27     private int mSize;
     28 
     29     private static final int DEFAULT_SIZE = 32;
     30 
     31     public ObjectPool() {
     32         super();
     33         setSize(DEFAULT_SIZE);
     34     }
     35 
     36     public ObjectPool(int size) {
     37         super();
     38         setSize(size);
     39     }
     40 
     41     @Override
     42     public void reset() {
     43     }
     44 
     45     /** Allocates an object from the pool */
     46     protected Object allocate() {
     47         Object result = mAvailable.removeLast();
     48         assert result != null : "Object pool of type " + this.getClass().getSimpleName()
     49                                 + " exhausted!!";
     50         return result;
     51     }
     52 
     53     /** Returns an object to the pool. */
     54     public void release(Object entry) {
     55         mAvailable.add(entry);
     56     }
     57 
     58     /** Returns the number of pooled elements that have been allocated but not released. */
     59     public int getAllocatedCount() {
     60         return mAvailable.getCapacity() - mAvailable.getCount();
     61     }
     62 
     63     private void setSize(int size) {
     64         mSize = size;
     65         mAvailable = new FixedSizeArray<Object>(mSize);
     66 
     67         fill();
     68     }
     69 
     70     protected abstract void fill();
     71 
     72     protected FixedSizeArray<Object> getAvailable() {
     73         return mAvailable;
     74     }
     75 
     76     protected int getSize() {
     77         return mSize;
     78     }
     79 
     80 
     81 }
     82