KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > LRUCache


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

18 package org.apache.batik.ext.awt.image.rendered;
19
20 import org.apache.batik.util.DoublyLinkedList;
21
22 public class LRUCache {
23
24     /**
25      * Interface for object participating in the LRU Cache. These
26      * inform the object of key events in the status of the object in
27      * the LRU cache.
28      */

29     public interface LRUObj {
30         /**
31          * Called when the object first becomes active in the LRU cache.
32          * @param nde The LRU cache node associated with this object.
33          * should be remembered so it can be returned by
34          * <tt>lruGet</tt>.
35          */

36         public void lruSet(LRUNode nde);
37         /**
38          * Called to get the LRU node for this object. Should return the
39          * node passed in to lruSet.
40          */

41         public LRUNode lruGet();
42         /**
43          * Called to inform the object that it is no longer in the cache.
44          */

45         public void lruRemove();
46     }
47
48     /**
49      * Interface for nodes in the LRU cache, basicly nodes in a doubly
50      * linked list.
51      */

52     public class LRUNode extends DoublyLinkedList.Node {
53         private LRUObj obj = null;
54         public LRUObj getObj () { return obj; }
55         protected void setObj (LRUObj newObj) {
56             if (obj != null) obj.lruRemove();
57
58             obj = newObj;
59             if (obj != null) obj.lruSet(this);
60         }
61     }
62
63     private DoublyLinkedList free = null;
64     private DoublyLinkedList used = null;
65     private int maxSize = 0;
66         
67     public LRUCache(int size) {
68         if (size <= 0) size=1;
69         maxSize = size;
70         
71         free = new DoublyLinkedList();
72         used = new DoublyLinkedList();
73         
74         while (size > 0) {
75             free.add(new LRUNode());
76             size--;
77         }
78     }
79
80     public int getUsed() {
81         return used.getSize();
82     }
83
84     public synchronized void setSize(int newSz) {
85
86         if (maxSize < newSz) { // list grew...
87

88             for (int i=maxSize; i<newSz; i++)
89                 free.add(new LRUNode());
90
91         } else if (maxSize > newSz) {
92
93             for (int i=used.getSize(); i>newSz; i--) {
94                 LRUNode nde = (LRUNode)used.getTail();
95                 used.remove(nde);
96                 nde.setObj(null);
97             }
98         }
99
100         maxSize = newSz;
101     }
102
103     public synchronized void flush() {
104         while (used.getSize() > 0) {
105             LRUNode nde = (LRUNode)used.pop();
106             nde.setObj(null);
107             free.add(nde);
108         }
109     }
110
111     public synchronized void remove(LRUObj obj) {
112         LRUNode nde = obj.lruGet();
113         if (nde == null) return;
114         used.remove(nde);
115         nde.setObj(null);
116         free.add(nde);
117     }
118
119     public synchronized void touch(LRUObj obj) {
120         LRUNode nde = obj.lruGet();
121         if (nde == null) return;
122         used.touch(nde);
123     }
124
125     public synchronized void add(LRUObj obj) {
126         LRUNode nde = obj.lruGet();
127
128         // already linked in...
129
if (nde != null) {
130             used.touch(nde);
131             return;
132         }
133
134         if (free.getSize() > 0) {
135             nde = (LRUNode)free.pop();
136             nde.setObj(obj);
137             used.add(nde);
138         } else {
139             nde = (LRUNode)used.getTail();
140             nde.setObj(obj);
141             used.touch(nde);
142         }
143     }
144
145     protected synchronized void print() {
146         System.out.println("In Use: " + used.getSize() +
147                            " Free: " + free.getSize());
148         LRUNode nde = (LRUNode)used.getHead();
149         if (nde == null) return;
150         do {
151             System.out.println(nde.getObj());
152             nde = (LRUNode)nde.getNext();
153         } while (nde != used.getHead());
154     }
155
156 }
157
Popular Tags