~ubuntu-branches/ubuntu/oneiric/libpgjava/oneiric

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/core/SimpleObjectPool.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.postgresql.core;
2
 
 
3
 
/*
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.
7
 
 */
8
 
 
9
 
public class SimpleObjectPool implements ObjectPool
10
 
{
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
13
 
        // of another query.
14
 
        int cursize = 0;
15
 
        int maxsize = 16;
16
 
        Object arr[] = new Object[maxsize];
17
 
 
18
 
        /*
19
 
         * Adds an object to the pool
20
 
         * @param o Object to add
21
 
         */
22
 
        public void add(Object o)
23
 
        {
24
 
                if (cursize >= maxsize)
25
 
                {
26
 
                        Object newarr[] = new Object[maxsize * 2];
27
 
                        System.arraycopy(arr, 0, newarr, 0, maxsize);
28
 
                        maxsize = maxsize * 2;
29
 
                        arr = newarr;
30
 
                }
31
 
                arr[cursize++] = o;
32
 
        }
33
 
 
34
 
        /*
35
 
         * Removes the top object from the pool
36
 
         * @return Object from the top.
37
 
         */
38
 
        public Object remove()
39
 
        {
40
 
                return arr[--cursize];
41
 
        }
42
 
 
43
 
        /*
44
 
         * Removes the given object from the pool
45
 
         * @param o Object to remove
46
 
         */
47
 
        public void remove(Object o)
48
 
        {
49
 
                int p = 0;
50
 
                while (p < cursize && !arr[p].equals(o))
51
 
                        p++;
52
 
                if (arr[p].equals(o))
53
 
                {
54
 
                        // This should be ok as there should be no overlap conflict
55
 
                        System.arraycopy(arr, p + 1, arr, p, cursize - p);
56
 
                        cursize--;
57
 
                }
58
 
        }
59
 
 
60
 
        /*
61
 
         * @return true if the pool is empty
62
 
         */
63
 
        public boolean isEmpty()
64
 
        {
65
 
                return cursize == 0;
66
 
        }
67
 
 
68
 
        /*
69
 
         * @return the number of objects in the pool
70
 
         */
71
 
        public int size()
72
 
        {
73
 
                return cursize;
74
 
        }
75
 
 
76
 
        /*
77
 
         * Adds all objects in one pool to this one
78
 
         * @param pool The pool to take the objects from
79
 
         */
80
 
        public void addAll(ObjectPool p)
81
 
        {
82
 
                SimpleObjectPool pool = (SimpleObjectPool)p;
83
 
 
84
 
                int srcsize = pool.size();
85
 
                if (srcsize == 0)
86
 
                        return;
87
 
                int totalsize = srcsize + cursize;
88
 
                if (totalsize > maxsize)
89
 
                {
90
 
                        Object newarr[] = new Object[totalsize * 2];
91
 
                        System.arraycopy(arr, 0, newarr, 0, cursize);
92
 
                        maxsize = maxsize = totalsize * 2;
93
 
                        arr = newarr;
94
 
                }
95
 
                System.arraycopy(pool.arr, 0, arr, cursize, srcsize);
96
 
                cursize = totalsize;
97
 
        }
98
 
 
99
 
        /*
100
 
         * Clears the pool of all objects
101
 
         */
102
 
        public void clear()
103
 
        {
104
 
                cursize = 0;
105
 
        }
106
 
}