KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > collections > SimplePool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.collections;
19
20 /**
21  * Simple object pool. Based on ThreadPool and few other classes
22  *
23  * The pool will ignore overflow and return null if empty.
24  *
25  * @author Gal Shachor
26  * @author Costin Manolache
27  */

28 public final class SimplePool {
29     
30     
31     private static org.apache.commons.logging.Log log=
32         org.apache.commons.logging.LogFactory.getLog(SimplePool.class );
33     
34     /*
35      * Where the threads are held.
36      */

37     private Object JavaDoc pool[];
38
39     private int max;
40     private int last;
41     private int current=-1;
42     
43     private Object JavaDoc lock;
44     public static final int DEFAULT_SIZE=32;
45     static final int debug=0;
46     
47     public SimplePool() {
48     this(DEFAULT_SIZE,DEFAULT_SIZE);
49     }
50
51     public SimplePool(int size) {
52     this(size, size);
53     }
54
55     public SimplePool(int size, int max) {
56     this.max=max;
57     pool=new Object JavaDoc[size];
58     this.last=size-1;
59     lock=new Object JavaDoc();
60     }
61
62     public void set(Object JavaDoc o) {
63     put(o);
64     }
65
66     /**
67      * Add the object to the pool, silent nothing if the pool is full
68      */

69     public void put(Object JavaDoc o) {
70     synchronized( lock ) {
71         if( current < last ) {
72         current++;
73         pool[current] = o;
74             } else if( current < max ) {
75         // realocate
76
int newSize=pool.length*2;
77         if( newSize > max ) newSize=max+1;
78         Object JavaDoc tmp[]=new Object JavaDoc[newSize];
79         last=newSize-1;
80         System.arraycopy( pool, 0, tmp, 0, pool.length);
81         pool=tmp;
82         current++;
83         pool[current] = o;
84         }
85         if( debug > 0 ) log("put " + o + " " + current + " " + max );
86     }
87     }
88
89     /**
90      * Get an object from the pool, null if the pool is empty.
91      */

92     public Object JavaDoc get() {
93     Object JavaDoc item = null;
94     synchronized( lock ) {
95         if( current >= 0 ) {
96         item = pool[current];
97         pool[current] = null;
98         current -= 1;
99         }
100         if( debug > 0 )
101         log("get " + item + " " + current + " " + max);
102     }
103     return item;
104     }
105
106     /**
107      * Return the size of the pool
108      */

109     public int getMax() {
110     return max;
111     }
112
113     /**
114      * Number of object in the pool
115      */

116     public int getCount() {
117     return current+1;
118     }
119
120
121     public void shutdown() {
122     }
123     
124     private void log( String JavaDoc s ) {
125         if (log.isDebugEnabled())
126             log.debug("SimplePool: " + s );
127     }
128 }
129
Popular Tags