KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > BufferCache


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

18
19 import org.apache.commons.collections.SequencedHashMap;
20
21 /**
22  * A fixed length object cache implementing the LRU algorithm. Convenient for
23  * buffering recently used objects.
24  *
25  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
26  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
27  * @version $Id: BufferCache.java,v 1.5.2.2 2004/05/20 03:16:38 seade Exp $
28  */

29 public class BufferCache
30     extends SequencedHashMap
31 {
32     /**
33      * The default maximum cache size.
34      */

35     private static final int DEFAULT_MAX_SIZE = 35;
36
37     /**
38      * The size of the cache. The newest elements in the sequence are kept
39      * toward the end.
40      */

41     private int maxSize;
42
43     /**
44      * Creates a new instance with default storage buffer pre-allocated.
45      */

46     public BufferCache()
47     {
48         this(DEFAULT_MAX_SIZE);
49     }
50
51     /**
52      * Creates a new instance with the specified storage buffer pre-allocated.
53      *
54      * @param maxSize The maximum size of the cache.
55      */

56     public BufferCache(int maxSize)
57     {
58         super(maxSize);
59         this.maxSize = maxSize;
60     }
61
62     /**
63      * Stores the provided key/value pair, freshening its list index if the
64      * specified key already exists.
65      *
66      * @param key The key to the provided value.
67      * @param value The value to store.
68      * @return The previous value for the specified key, or
69      * <code>null</code> if none.
70      */

71     public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
72     {
73         int size = size();
74         if (size > 0 && size + 1 >= maxSize)
75         {
76             // Stay within constraints of allocated buffer by releasing the
77
// eldest buffered object.
78
remove(0);
79         }
80         return super.put(key, value);
81     }
82
83     /**
84      * Retrieves the value associated with the provided key, freshening the
85      * sequence of the key as well.
86      *
87      * @param key The key whose value to retrieve.
88      * @return The keyed value.
89      */

90     public synchronized Object JavaDoc get(Object JavaDoc key)
91     {
92         return super.get(key);
93     }
94 }
95
Popular Tags