KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > ObjectPoolImpl


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

15 package org.apache.tapestry.services.impl;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.tapestry.event.ResetEventListener;
23 import org.apache.tapestry.services.ObjectPool;
24
25 /**
26  * Implementation of the {@link org.apache.tapestry.services.ObjectPool} interface.
27  *
28  * <p>
29  * This ia a minimal implementation, one that has no concept of automatically removing
30  * unused pooled objects. Eventually, it will also register for notifications about
31  * general cache cleaning.
32  *
33  * @author Howard Lewis Ship
34  * @since 4.0
35  */

36 public class ObjectPoolImpl implements ObjectPool, ResetEventListener
37 {
38     /**
39      * Pool of Lists (of pooled objects), keyed on arbitrary key.
40      */

41     private Map JavaDoc _pool = new HashMap JavaDoc();
42
43     public synchronized Object JavaDoc get(Object JavaDoc key)
44     {
45         List JavaDoc pooled = (List JavaDoc) _pool.get(key);
46
47         if (pooled == null || pooled.isEmpty())
48             return null;
49
50         return pooled.remove(0);
51     }
52
53     public synchronized void store(Object JavaDoc key, Object JavaDoc value)
54     {
55         List JavaDoc pooled = (List JavaDoc) _pool.get(key);
56
57         if (pooled == null)
58         {
59             pooled = new LinkedList JavaDoc();
60             _pool.put(key, pooled);
61         }
62
63         pooled.add(value);
64     }
65
66     public synchronized void resetEventDidOccur()
67     {
68         _pool.clear();
69     }
70
71 }
72
Popular Tags