KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > MemoryAccessManager


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package com.imagero.uio.buffer;
33
34 import java.util.Enumeration JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Random JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 /**
40  * MemoryAccessManager.
41  * Implements 5 possible strategies to free memory:
42  * - DROP_NEVER - Buffer(s) are never dropped
43  * - DROP_IMMEDIATELY - only 1 Buffer is held in memory
44  * - DROP_RANDOM - if buffer count exceeds maxBufferCount then randomly choosed Buffer is dropped
45  * - DROP_LRU - if buffer count exceeds maxBufferCount then Least Recently Used Buffer is dropped
46  * - DROP_FIFO - if buffer count exceeds maxBufferCount then Least Recently Added Buffer is dropped
47  * @author Andrey Kuznetsov
48  */

49 public abstract class MemoryAccessManager {
50
51     public static final int DROP_NEVER = 0;
52     public static final int DROP_IMMEDIATELY = -1;
53     public static final int DROP_RANDOM = 1;
54     public static final int DROP_LRU = 2;
55     public static final int DROP_FIFO = 3;
56
57     private static int defaultStrategy = DROP_FIFO;
58
59     public static int getDefaultStrategy() {
60         return defaultStrategy;
61     }
62
63     public static void setDefaultStrategy(int defaultStrategy) {
64         MemoryAccessManager.defaultStrategy = defaultStrategy;
65     }
66
67     public static MemoryAccessManager createMemoryAccessManager() {
68         return createMemoryAccessManager(getDefaultStrategy());
69     }
70
71     public static MemoryAccessManager createMemoryAccessManager(int strategy) {
72         switch (strategy) {
73             case DROP_NEVER:
74                 return new DNMemoryAccessManager();
75             case DROP_IMMEDIATELY:
76                 return new DIMemoryAccessManager();
77             case DROP_RANDOM:
78                 return new DRMemoryAccessManager();
79             case DROP_LRU:
80                 return new LRUMemoryAccessManager();
81             case DROP_FIFO:
82                 return new FIFOMemoryAccessManager();
83             default:
84                 throw new IllegalArgumentException JavaDoc("unknown strategy:" + strategy);
85         }
86     }
87
88     int maxBufferCount = 10;
89     Hashtable JavaDoc ht = new Hashtable JavaDoc();
90
91     public abstract Buffer get(Object JavaDoc key);
92
93     public abstract void put(Object JavaDoc key, Buffer b);
94
95     public void add(Buffer b) {
96         put(new Integer JavaDoc(ht.size()), b);
97     }
98
99     public Buffer get(int i) {
100         return get(new Integer JavaDoc(i));
101     }
102
103     public int getCount() {
104         return ht.size();
105     }
106
107     public int getBufferLength(int i) {
108         return getBufferLength(new Integer JavaDoc(i));
109     }
110
111     public int getBufferLength(Object JavaDoc key) {
112         Buffer b = get(key);
113         if (b != null) {
114             return b.length();
115         }
116         return 0;
117     }
118
119     public Buffer drop(Object JavaDoc key) {
120         return (Buffer) ht.remove(key);
121     }
122
123     public int getMaxBufferCount() {
124         return maxBufferCount;
125     }
126
127     public void setMaxBufferCount(int maxBufferCount) {
128         this.maxBufferCount = maxBufferCount;
129     }
130
131     public void clear() {
132         ht.clear();
133     }
134
135     public boolean isDirty(int index) {
136         Buffer b = (Buffer) ht.get(new Integer JavaDoc(index));
137         if (b != null) {
138             return b.isDirty();
139         }
140         return false;
141     }
142
143     public boolean isDirty(Object JavaDoc key) {
144         Buffer b = (Buffer) ht.get(key);
145         if (b != null) {
146             return b.isDirty();
147         }
148         return false;
149     }
150
151     public Enumeration JavaDoc keys() {
152         return ht.keys();
153     }
154
155     static class DNMemoryAccessManager extends MemoryAccessManager {
156
157         public Buffer get(Object JavaDoc key) {
158             return (Buffer) ht.get(key);
159         }
160
161         public void put(Object JavaDoc key, Buffer b) {
162             ht.put(key, b);
163         }
164     }
165
166     static class DIMemoryAccessManager extends MemoryAccessManager {
167         public Buffer get(Object JavaDoc key) {
168             return (Buffer) ht.get(key);
169         }
170
171         public void put(Object JavaDoc key, Buffer b) {
172             ht.clear();
173             ht.put(key, b);
174         }
175     }
176
177     static class DRMemoryAccessManager extends MemoryAccessManager {
178         Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
179
180         public Buffer get(Object JavaDoc key) {
181             return (Buffer) ht.get(key);
182         }
183
184         public void put(Object JavaDoc key, Buffer b) {
185             int size = ht.size();
186             if (size >= maxBufferCount) {
187                 int r = Math.abs(random.nextInt()) % size;
188                 Enumeration JavaDoc keys = ht.keys();
189                 for (int i = 0; i < r; i++) {
190                     if (keys.hasMoreElements()) {
191                         keys.nextElement();
192                     }
193                 }
194                 boolean dropped = false;
195                 //drop after random position
196
while (keys.hasMoreElements()) {
197                     Object JavaDoc k = keys.nextElement();
198                     Buffer b0 = get(k);
199                     if (!b0.isDirty()) {
200                         drop(k);
201                         dropped = true;
202                         break;
203                     }
204                 }
205                 if (!dropped) {
206                     keys = ht.keys();
207                     //drop first not dirty
208
while (keys.hasMoreElements()) {
209                         Object JavaDoc k = keys.nextElement();
210                         Buffer b0 = get(k);
211                         if (!b0.isDirty()) {
212                             drop(k);
213                             break;
214                         }
215                     }
216                 }
217             }
218             ht.put(key, b);
219         }
220
221         protected void drop(int index) {
222             ht.remove(new Integer JavaDoc(index));
223         }
224     }
225
226     static class FIFOMemoryAccessManager extends MemoryAccessManager {
227         Vector JavaDoc fifo = new Vector JavaDoc();
228
229         public Buffer get(Object JavaDoc key) {
230             return (Buffer) ht.get(key);
231         }
232
233         public void put(Object JavaDoc key, Buffer b) {
234             int size = ht.size();
235             if (size >= maxBufferCount) {
236                 for (int i = 0; i < fifo.size(); i++) {
237                     Object JavaDoc k = fifo.elementAt(i);
238                     Buffer b0 = get(k);
239                     if (!b0.isDirty()) {
240                         drop(k);
241                         break;
242                     }
243                 }
244             }
245             fifo.addElement(key);
246             ht.put(key, b);
247         }
248
249         protected void drop(int index) {
250             Object JavaDoc k = fifo.elementAt(index);
251             fifo.removeElementAt(index);
252             ht.remove(k);
253         }
254
255         public Buffer drop(Object JavaDoc key) {
256             fifo.removeElement(key);
257             return super.drop(key);
258         }
259
260         public void clear() {
261             ht.clear();
262             fifo.removeAllElements();
263         }
264     }
265
266     static class LRUMemoryAccessManager extends MemoryAccessManager {
267         Vector JavaDoc lru = new Vector JavaDoc();
268
269         public Buffer get(Object JavaDoc key) {
270             lru.removeElement(key);
271             lru.addElement(key);
272             return (Buffer) ht.get(key);
273         }
274
275         public void put(Object JavaDoc key, Buffer b) {
276             lru.removeElement(key);
277             lru.addElement(key);
278             if (lru.size() >= maxBufferCount) {
279                 for (int i = 0; i < lru.size(); i++) {
280                     Object JavaDoc k = lru.elementAt(i);
281                     Buffer b0 = get(k);
282                     if (!b0.isDirty()) {
283                         drop(k);
284                         break;
285                     }
286                 }
287             }
288             ht.put(key, b);
289         }
290
291         protected void drop(int index) {
292             Object JavaDoc k = lru.elementAt(index);
293             lru.removeElementAt(index);
294             ht.remove(k);
295         }
296
297         public Buffer drop(Object JavaDoc key) {
298             lru.removeElement(key);
299             return super.drop(key);
300         }
301
302         public void clear() {
303             ht.clear();
304             lru.removeAllElements();
305         }
306     }
307 }
308
Popular Tags