KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > lock > ObjectPool


1 package com.quadcap.sql.lock;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Iterator JavaDoc;
42
43 import com.quadcap.util.Debug;
44
45 //#set defs(NOPOOLxx) 1
46
//#autogen begin
47
//#autogen end
48

49 /**
50  *
51  *
52  * @author Stan Bailes
53  */

54 public class ObjectPool {
55     PooledObject proto;
56
57     public ObjectPool(PooledObject proto) {
58         this.proto = proto;
59     }
60
61     //#ifdef NOPOOL
62
//- public PooledObject get() {
63
//- PooledObject p = proto.create();
64
//- p.live = true;
65
//- return p;
66
//- }
67
//- public void release(PooledObject p) {
68
//- p.live = false;
69
//- }
70
//- public Iterator iterator() { return null; }
71
//#else
72
PooledObject[] pool;
73     int next = 0;
74     
75     Object JavaDoc lock = new Object JavaDoc();
76     
77     final void resizePool(int lim) {
78         PooledObject[] old = pool;
79         if (old == null || old.length != lim) {
80             pool = new PooledObject[lim];
81             int f = 0;
82             if (old != null) {
83                 System.arraycopy(old, 0, pool, 0,
84                                  Math.min(old.length, lim));
85                 f = old.length;
86             }
87             for (int i = f; i < lim; i++) {
88                 pool[i] = proto.create();
89                 pool[i].poolIndex = i;
90             }
91         }
92     }
93     
94     public PooledObject get() {
95         synchronized (lock) {
96             if (pool == null || next >= pool.length) {
97                 resizePool(pool == null ?
98                            8 : (pool.length + (pool.length >> 2)));
99             }
100             PooledObject obj = pool[next++];
101             if (obj.live) throw new RuntimeException JavaDoc("Already live: " + obj);
102             obj.live = true;
103             return obj;
104         }
105     }
106
107     public void release(PooledObject obj) {
108         synchronized (lock) {
109             if (!obj.live) throw new RuntimeException JavaDoc("Not live: " + obj);
110             obj.live = false;
111             final int idx = obj.poolIndex;
112             if (pool[idx] != obj)
113                 throw new RuntimeException JavaDoc("Pool check: " + obj);
114             final int last = --next;
115             if (idx != last) {
116                 pool[idx] = pool[last];
117                 pool[idx].poolIndex = idx;
118                 pool[last] = obj;
119                 obj.poolIndex = last;
120             }
121         }
122     }
123
124     /**
125      * Return an Iterator that can be used to access all live objects
126      * in the pool.
127      */

128     public Iterator JavaDoc iterator() {
129         return new Iterator JavaDoc() {
130                 int idx = 0;
131                 
132                 public boolean hasNext() {
133                     return idx < next;
134                 }
135
136                 public Object JavaDoc next() {
137                     return idx < next ? pool[idx++] : null;
138                 }
139                 public void remove() {}
140             };
141     }
142             
143     //#ifdef DEBUG
144
public String JavaDoc toString() {
145         String JavaDoc s = getClass().getName();
146         int idx = s.lastIndexOf('.');
147         if (idx >= 0) s = s.substring(idx+1);
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
149         sb.append("(");
150         int len = s.length() + 1; // s + '['
151
for (int i = 0; i < next; i++) {
152             if (i > 0) sb.append(',');
153             String JavaDoc p = pool[i].toString();
154             final int plen = p.length();
155             len += plen + 1;
156             if (len > 80) {
157                 sb.append('\n');
158                 len = plen;
159             }
160             sb.append(p);
161         }
162         sb.append(')');
163         return sb.toString();
164     }
165     //#endif
166
//#endif
167
}
168
Popular Tags