// SimplePool.java

package org.bson.util;

import java.util.*;

public abstract class SimplePool<T> {

    public SimplePool( int max ){
        _max = max;
    }

    public SimplePool(){
        _max = 1000;
    }
    
    protected abstract T createNew();

    protected boolean ok( T t ){
        return true;
    }
    
    public T get(){
        synchronized ( _stored ){
            if ( _stored.size() > 0 ) 
                return _stored.removeFirst();
        }
        return createNew();
    }

    public void done( T t ){
        if ( ! ok( t ) )
            return;
        synchronized ( _stored ){
            if ( _stored.size() > _max )
                return;
            _stored.addFirst( t );
        }
    }
    
    final int _max;
    private LinkedList<T> _stored = new LinkedList<T>();
}

@@ -56,16 +56,47 @@
 .util.*;
+%0Aimport java.util.concurrent.*;
 %0A%0Apublic
@@ -382,75 +382,55 @@
     
-synchronized ( _stored )%7B%0A            if ( _stored.size() %3E 0 ) %0A  
+T t = _stored.poll();%0A        if ( t != null )%0A
     
@@ -441,49 +441,17 @@
     
-  
 return 
-_stored.removeFirst();%0A        %7D
+t;
 %0A   
@@ -568,38 +568,9 @@
     
-synchronized ( _stored )%7B%0A    
+%0A
     
@@ -606,36 +606,32 @@
 x )%0A            
-    
 return;%0A        
@@ -630,20 +630,16 @@
         
-   
- 
 _stored.
@@ -645,30 +645,15 @@
 .add
-First
 ( t );%0A
-        %7D%0A
     
@@ -691,26 +691,21 @@
 private 
-LinkedList
+Queue
 %3CT%3E _sto
@@ -718,18 +718,29 @@
 new 
+Concurrent
 Linked
-List
+Queue
 %3CT%3E(

use java.util.concurrent