KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > LRU


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * CacheManager.java
25  *
26  * Created on 17 septembre 2002, 16:15
27  */

28
29 package org.xquark.mapper.util;
30
31 import java.util.*;
32
33 /**
34  * Implements a LRU collection manager
35  *
36  */

37 public class LRU extends LinkedList
38 {
39     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
40     private static final String JavaDoc RCSName = "$Name: $";
41
42     private int maxSize;
43     private HashMap index;
44     
45     /** Creates a new instance of CacheManager */
46     public LRU(int maxSize)
47     {
48         setMaxSize(maxSize);
49         index = new HashMap();
50     }
51     
52     public void setMaxSize(int maxSize)
53     {
54         while (size() > maxSize) // max size reached: discard oldest
55
{
56             Node node = (Node)remove(0);
57             index.remove(node.key);
58         }
59         this.maxSize = maxSize;
60     }
61     
62     public int getMaxSize()
63     {
64         return maxSize;
65     }
66     
67     /**
68      * @return the potential removed object
69      */

70     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc o)
71     {
72         Object JavaDoc discarded = null;
73         if (maxSize > 0)
74         {
75             Node node = new Node(key, o);
76             super.add(node);
77             index.put(key, node);
78             if (size() > maxSize) // max size reached: discard oldest
79
{
80                 node = (Node)remove(0);
81                 index.remove(node.key);
82                 discarded = node.o;
83             }
84         }
85         else
86             discarded = o;
87         
88         return discarded;
89     }
90     
91     public Object JavaDoc get(Object JavaDoc key)
92     {
93         Node node = (Node)index.get(key);
94         // move object to the end of the list (LRU)
95
if (node != null)
96         {
97             add(remove(indexOf(node)));
98             return node.o;
99         }
100         else
101             return null;
102     }
103     
104     public void clear()
105     {
106         super.clear();
107         index.clear();
108     }
109
110     public synchronized void setKey(Object JavaDoc oldKey, Object JavaDoc newKey)
111     {
112         Node node = (Node)index.remove(oldKey);
113         if (node != null)
114         {
115             node.key = newKey;
116             index.put(newKey, node);
117         }
118     }
119     
120     public Object JavaDoc removeObject(Object JavaDoc key)
121     {
122         Node removed = (Node)index.remove(key);
123         if (removed != null)
124             super.remove(removed);
125         return removed;
126     }
127     
128     public Collection values()
129     {
130         ArrayList list = new ArrayList();
131         Iterator it = super.iterator();
132         while (it.hasNext())
133         {
134             list.add(((Node)it.next()).o);
135         }
136         return list;
137     }
138     
139     private class Node
140     {
141         Object JavaDoc key;
142         Object JavaDoc o;
143         
144         Node(Object JavaDoc key, Object JavaDoc o)
145         {
146             this.key = key;
147             this.o = o;
148         }
149     }
150 }
151
Popular Tags