KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > FlatCache


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * Fixed length cache with a LRU replacement policy. If cache items
33  * implement CacheListener, they will be informed when they're removed
34  * from the cache.
35  *
36  * <p>Null keys are not allowed. LruCache is synchronized.
37  */

38 public class FlatCache<K,V> {
39   private Object JavaDoc []_keys;
40   private V []_values;
41
42   // maximum allowed entries
43
private int _capacity;
44   private int _mask;
45
46   private static Object JavaDoc NULL = new Object JavaDoc();
47
48   public FlatCache(int initialCapacity)
49   {
50     for (_capacity = 32; _capacity < 2 * initialCapacity; _capacity *= 2) {
51     }
52
53     _keys = new Object JavaDoc[_capacity];
54     _values = (V []) new Object JavaDoc[_capacity];
55
56     _mask = _capacity - 1;
57   }
58
59   /**
60    * Clears the cache
61    */

62   public synchronized void clear()
63   {
64     for (int i = 0; i < _capacity; i++) {
65       if (_values[i] instanceof CacheListener)
66         ((CacheListener) _values[i]).removeEvent();
67
68       _keys[i] = null;
69       _values[i] = null;
70     }
71   }
72
73   /**
74    * Get an item from the cache and make it most recently used.
75    *
76    * @param key key to lookup the item
77    * @return the matching object in the cache
78    */

79   public Object JavaDoc get(K key)
80   {
81     Object JavaDoc okey = key;
82     if (okey == null)
83       okey = NULL;
84
85     int hash = okey.hashCode() & _mask;
86
87     Object JavaDoc testKey = _keys[hash];
88
89     if (testKey != null && testKey.equals(okey) && testKey == _keys[hash])
90       return _values[hash];
91     else
92       return null;
93   }
94
95   /**
96    * Puts a new item in the cache. If the cache is full, remove the
97    * LRU item.
98    *
99    * @param key key to store data
100    * @param value value to be stored
101    *
102    * @return old value stored under the key
103    */

104   public synchronized V put(K key, V value)
105   {
106     Object JavaDoc okey = key;
107     if (okey == null)
108       okey = NULL;
109
110     int hash = okey.hashCode() & _mask;
111
112     V old = _values[hash];
113
114     _keys[hash] = null;
115     _values[hash] = value;
116     _keys[hash] = okey;
117
118     if (old instanceof CacheListener)
119       ((CacheListener) old).removeEvent();
120
121     return old;
122   }
123
124   /**
125    * Removes an item from the cache
126    *
127    * @param key the key to remove
128    *
129    * @return the value removed
130    */

131   public V remove(K key)
132   {
133     return put(key, null);
134   }
135 }
136
Popular Tags