KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > util > cache > Cache


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6
7 /** The Cache class is a framework object: it requires an
8  * object to build instances and a policy for replacement.
9  * Currently, caches are fixed size.
10  *
11  * @author Andy Seaborne
12  * @version $Id: Cache.java,v 1.7 2004/04/30 14:13:13 andy_seaborne Exp $
13  */

14  
15 package org.joseki.util.cache;
16
17 import java.util.* ;
18 import org.apache.commons.logging.*;
19
20 public class Cache
21 {
22     static Log log = LogFactory.getLog(Cache.class.getName()) ;
23
24     Map objects = new HashMap() ;
25     CacheItemFactory factory = null ;
26     CachePolicy policy = null ;
27     int size ;
28     
29     public Cache(int _size, CacheItemFactory _factory, CachePolicy _policy)
30     {
31         factory = _factory ;
32         policy = _policy ;
33         size = _size ;
34     }
35     
36     public synchronized void remove(Object JavaDoc key)
37     {
38         objects.remove(key) ;
39     }
40     
41     public synchronized void put(Object JavaDoc key, Object JavaDoc obj)
42     {
43         if ( objects.size() >= size )
44             expel(1) ;
45
46         policy.newItem(key, obj) ;
47         objects.put(key, obj) ;
48     }
49     
50     public synchronized Object JavaDoc get(Object JavaDoc key)
51     {
52         log.trace("get("+key+")") ;
53         Object JavaDoc obj = objects.get(key) ;
54         if ( obj == null )
55         {
56             if ( objects.size() >= size )
57                 expel(1) ;
58             
59             obj = factory.create(key) ;
60             if ( obj == null )
61                 log.warn("factory.create("+key+") returned null") ;
62
63             put(key, obj) ;
64         }
65         policy.update(key) ;
66         return obj ;
67     }
68
69     private void expel(int count)
70     {
71         Object JavaDoc[] keys = policy.expel(count) ;
72         for ( int i = 0 ; i < keys.length ; i++ )
73         {
74             Object JavaDoc obj = objects.remove(keys[i]) ;
75             factory.destroy(keys[i], obj) ;
76         }
77     }
78 }
79
80
81 /*
82  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
83  * All rights reserved.
84  *
85  * Redistribution and use in source and binary forms, with or without
86  * modification, are permitted provided that the following conditions
87  * are met:
88  * 1. Redistributions of source code must retain the above copyright
89  * notice, this list of conditions and the following disclaimer.
90  * 2. Redistributions in binary form must reproduce the above copyright
91  * notice, this list of conditions and the following disclaimer in the
92  * documentation and/or other materials provided with the distribution.
93  * 3. The name of the author may not be used to endorse or promote products
94  * derived from this software without specific prior written permission.
95  *
96  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
97  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
98  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
99  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
100  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
101  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
105  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106  */

107
Popular Tags