KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > VariableSizePool


1
2 /*
3  * Copyright (C) The Apache Software Foundation. All rights reserved.
4  *
5  * This software is published under the terms of the Apache Software License
6  * version 1.1, a copy of which has been included with this distribution in
7  * the LICENSE.txt file.
8  */

9 package org.apache.avalon.excalibur.pool;
10
11 import org.apache.avalon.framework.activity.Disposable;
12 import org.apache.avalon.excalibur.collections.Buffer;
13 import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
14 import org.apache.avalon.excalibur.concurrent.Mutex;
15
16 /**
17  * This is an <code>Pool</code> that caches Poolable objects for reuse.
18  * Please note that this pool offers no resource limiting whatsoever.
19  *
20  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
21  * @version CVS $Revision: 1.2 $ $Date: 2001/12/26 16:15:23 $
22  * @since 4.1
23  */

24 public final class VariableSizePool
25     implements Pool, Disposable, ManagablePool
26 {
27     private boolean m_disposed = false;
28     private final Buffer m_buffer;
29     private final ObjectFactory m_factory;
30     private final Mutex m_mutex = new Mutex();
31     private final long m_key;
32
33     /**
34      * Constructor for an unmanaged pool
35      */

36     public VariableSizePool( ObjectFactory factory, int size )
37         throws Exception JavaDoc
38     {
39         this( factory, size, -1 );
40     }
41
42     /**
43      * Constructor for a managed pool
44      */

45     public VariableSizePool( ObjectFactory factory, int size, long key )
46         throws Exception JavaDoc
47     {
48         m_buffer = new VariableSizeBuffer( size );
49         m_factory = factory;
50         m_key = key;
51
52         for ( int i = 0; i < size; i++ )
53         {
54             m_buffer.add( m_factory.newInstance() );
55         }
56     }
57
58     public Poolable get()
59     {
60         if ( m_disposed )
61         {
62             throw new IllegalStateException JavaDoc( "Cannot get an object from a disposed pool" );
63         }
64
65         return (Poolable) m_buffer.remove();
66     }
67
68     public void put( Poolable object )
69     {
70         if ( m_disposed )
71         {
72             try
73             {
74                 m_factory.decommission( object );
75             }
76             catch ( Exception JavaDoc e )
77             {
78                 // We should never get here, but ignore the exception if it happens
79
}
80         }
81         else
82         {
83             m_buffer.add( object );
84         }
85     }
86
87     public void dispose()
88     {
89         m_disposed = true;
90
91         while ( ! m_buffer.isEmpty() )
92         {
93             try
94             {
95                 m_factory.decommission( m_buffer.remove() );
96             }
97             catch ( Exception JavaDoc e )
98             {
99                 // We should never get here, but ignore the exception if it happens
100
}
101         }
102     }
103
104     public void shrink( final int byNum, final long key )
105         throws IllegalAccessException JavaDoc
106     {
107         if ( m_key < 0 || m_key != key )
108         {
109             throw new IllegalAccessException JavaDoc();
110         }
111
112         try
113         {
114             m_mutex.acquire();
115
116             final int num = Math.min( byNum, m_buffer.size() );
117
118             for ( int i = 0; i < num; i++ )
119             {
120                 m_factory.decommission( m_buffer.remove() );
121             }
122         }
123         catch ( Exception JavaDoc e )
124         {
125             // We should never get here, but ignore the exception if it happens
126
}
127         finally
128         {
129             m_mutex.release();
130         }
131     }
132
133     public void grow( final int byNum, final long key )
134         throws IllegalAccessException JavaDoc
135     {
136         if ( m_key < 0 || m_key != key )
137         {
138             throw new IllegalAccessException JavaDoc();
139         }
140
141         try
142         {
143             m_mutex.acquire();
144
145             for ( int i = 0; i < byNum; i++ )
146             {
147                 m_buffer.add( m_factory.newInstance() );
148             }
149         }
150         catch ( Exception JavaDoc ie )
151         {
152             // We should never get here, but ignore the exception if it happens
153
}
154         finally
155         {
156             m_mutex.release();
157         }
158     }
159
160     public int size( final long key )
161         throws IllegalAccessException JavaDoc
162     {
163         if ( m_key < 0 || m_key != key )
164         {
165             throw new IllegalAccessException JavaDoc();
166         }
167
168         return m_buffer.size();
169     }
170 }
171
Popular Tags