1
package org.postgresql.core;
4
* A simple and fast object pool implementation that can pool objects
5
* of any type. This implementation is not thread safe, it is up to the users
6
* of this class to assure thread safety.
9
public class SimpleObjectPool implements ObjectPool
11
// This was originally in PG_Stream but moved out to fix the major problem
12
// where more than one query (usually all the time) overwrote the results
16
Object arr[] = new Object[maxsize];
19
* Adds an object to the pool
20
* @param o Object to add
22
public void add(Object o)
24
if (cursize >= maxsize)
26
Object newarr[] = new Object[maxsize * 2];
27
System.arraycopy(arr, 0, newarr, 0, maxsize);
28
maxsize = maxsize * 2;
35
* Removes the top object from the pool
36
* @return Object from the top.
38
public Object remove()
40
return arr[--cursize];
44
* Removes the given object from the pool
45
* @param o Object to remove
47
public void remove(Object o)
50
while (p < cursize && !arr[p].equals(o))
54
// This should be ok as there should be no overlap conflict
55
System.arraycopy(arr, p + 1, arr, p, cursize - p);
61
* @return true if the pool is empty
63
public boolean isEmpty()
69
* @return the number of objects in the pool
77
* Adds all objects in one pool to this one
78
* @param pool The pool to take the objects from
80
public void addAll(ObjectPool p)
82
SimpleObjectPool pool = (SimpleObjectPool)p;
84
int srcsize = pool.size();
87
int totalsize = srcsize + cursize;
88
if (totalsize > maxsize)
90
Object newarr[] = new Object[totalsize * 2];
91
System.arraycopy(arr, 0, newarr, 0, cursize);
92
maxsize = maxsize = totalsize * 2;
95
System.arraycopy(pool.arr, 0, arr, cursize, srcsize);
100
* Clears the pool of all objects