KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > Pool


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13
14 /**
15  * A simple, generic pool class that can be used to pool any kind of object.
16  * Objects can be released to this pool, either individually or as a
17  * Collection, and then later acquired again. It is not necessary for an
18  * object to have been originally acquired from the pool in order for it to
19  * be released to the pool. If there are no objects present in the pool,
20  * an attempt to acquire one will return null. The number of objects
21  * available in the pool can be determined with the size() method. Finally,
22  * it should be noted that the pool does not attempt to perform any kind
23  * of cleanup or re-initialization on the objects to restore them to some
24  * clean state when they are released to the pool; it's up to the user to
25  * reset any necessary state in the object prior to the release call (or
26  * just after the acquire call).
27  *
28  * @author Neil Rotstan
29  * @author Klaus Meffert
30  * @since 1.0
31  */

32 public class Pool {
33
34   /** String containing the CVS revision. Read out via reflection!*/
35   private final static String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
36
37   /**
38    * The Objects currently in the pool.
39    */

40   private List m_pooledObjects;
41
42   /**
43    * Constructor.
44    *
45    * @author Neil Rotstan
46    * @since 1.0
47    */

48   public Pool() {
49     m_pooledObjects = Collections.synchronizedList(new ArrayList());
50   }
51
52   /**
53    * Attempts to acquire an Object instance from the pool. It should
54    * be noted that no cleanup or re-initialization occurs on these
55    * objects, so it's up to the caller to reset the state of the
56    * returned Object if that's desirable.
57    *
58    * @return an Object instance from the pool or null if no Object instances
59    * are available in the pool
60    *
61    * @author Neil Rotstan
62    * @since 1.0
63    */

64   public synchronized Object JavaDoc acquirePooledObject() {
65     if (m_pooledObjects.isEmpty()) {
66       return null;
67     }
68     else {
69       // Remove the last Object in the pool and return it.
70
// Note that removing the last Object (as opposed to the first
71
// one) is an optimization because it prevents the ArrayList
72
// from resizing itself.
73
// -----------------------------------------------------------
74
return m_pooledObjects.remove(m_pooledObjects.size() - 1);
75     }
76   }
77
78   /**
79    * Releases an Object to the pool. It's not required that the Object
80    * originated from the pool--any Object can be released to it.
81    *
82    * @param a_objectToPool the Object instance to be released into the pool
83    *
84    * @author Neil Rotstan
85    * @since 1.0
86    */

87   public synchronized void releaseObject(final Object JavaDoc a_objectToPool) {
88     m_pooledObjects.add(a_objectToPool);
89   }
90
91   /**
92    * Releases a Collection of objects to the pool. It's not required that
93    * the objects in the Collection originated from the pool - -any objects
94    * can be released to it.
95    *
96    * @param a_objectsToPool the Collection of objects to release into the pool
97    *
98    * @author Neil Rotstan
99    * @since 1.0
100    */

101   public synchronized void releaseAllObjects(final Collection a_objectsToPool) {
102     if (a_objectsToPool != null) {
103       m_pooledObjects.addAll(a_objectsToPool);
104     }
105   }
106
107   /**
108    * Retrieves the number of objects currently available in this pool.
109    *
110    * @return the number of objects in this pool
111    *
112    * @author Neil Rotstan
113    * @since 1.0
114    */

115   public synchronized int size() {
116     return m_pooledObjects.size();
117   }
118
119   /**
120    * Empties out this pool of all objects.
121    *
122    * @author Neil Rotstan
123    * @since 1.0
124    */

125   public synchronized void clear() {
126     m_pooledObjects.clear();
127   }
128 }
129
Popular Tags