KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > pool > ArrayListPool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: AbstractPool.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/pool/ArrayListPool.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:26 $
47  */

48  
49
50 package com.sun.enterprise.util.pool;
51
52 import java.util.Collection JavaDoc;
53 import java.util.ArrayList JavaDoc;
54
55 /**
56  * <p>Abstract pool provides the basic implementation of an object pool. The implementation
57  * uses a linked list to maintain a collection of (available) objects. If the pool is
58  * empty it simply creates one using the ObjectFactory instance. Subclasses can change
59  * this behaviour by overriding getObject(...) and returnObject(....) methods. This
60  * class provides basic support for synchronization, event notification, pool shutdown
61  * and pool object recycling. It also does some very basic bookkeeping like the
62  * number of objects created, number of threads waiting for object.
63  * <p> Subclasses can make use of these book-keeping data to provide complex pooling
64  * mechanism like LRU / MRU / Random. Also, note that AbstractPool does not have a
65  * notion of pool limit. It is upto to the derived classes to implement these features.
66  * <p>This class does not define the canCreate() method.
67  */

68 public abstract class ArrayListPool
69     extends AbstractPool
70 {
71     protected ArrayList JavaDoc arrayList;
72     
73     protected ArrayListPool(ObjectFactory factory) {
74         super.factory = factory;
75         super.collection = this.arrayList = new ArrayList JavaDoc(6);
76     }
77     
78     protected ArrayListPool(ObjectFactory factory, int initialCapacity) {
79         super.factory = factory;
80         super.collection = this.arrayList = new ArrayList JavaDoc(initialCapacity);
81     }
82     
83     /**
84      * Notification when an object is put back into the pool (checkin).
85      * @param The object to be returned back to the pool.
86      * @return Any non null value can be returned to signal that the object
87      * was indeed added to the pool. This class always adds the object to the
88      * pool (at the end of the collection), it returns non-null value.
89      * Subclasses can override this behaviour.
90      */

91     protected Object JavaDoc checkin(Object JavaDoc object) {
92         collection.add(object);
93         return this;
94     }
95                 
96     /**
97      * Notification when an object is given out from the pool (checout).
98      * @return The object that has to be returned to the application. A null
99      * value must be returned if no object can be returned to the application. Since this
100      * class always returns the last node from the collection, it returns non-null value.
101      * Subclasses can override this behaviour.
102      */

103     protected Object JavaDoc checkout() {
104         return arrayList.remove(arrayList.size() - 1);
105     }
106                 
107     /**
108      * Notification when an object is given out from the pool (checout).
109      * @return The object that has to be returned to the application. A null
110      * value must be returned if no object can be returned to the application. Since this
111      * class always returns the last node from the collection, it returns non-null value.
112      * Subclasses can override this behaviour.
113      */

114     protected Object JavaDoc checkout(long param) {
115         return arrayList.remove(arrayList.size() - 1);
116     }
117                 
118     /**
119      * Notification when an object is given out from the pool (checout).
120      * @return The object that has to be returned to the application. A null
121      * value must be returned if no object can be returned to the application. Since this
122      * class always returns the last node from the collection, it returns non-null value.
123      * Subclasses can override this behaviour.
124      */

125     protected Object JavaDoc checkout(Object JavaDoc param) {
126         return arrayList.remove(arrayList.size() - 1);
127     }
128 }
Popular Tags