gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] libvob/org/nongnu/libvob/util CachingMap.java


From: Tuomas J. Lukka
Subject: [Gzz-commits] libvob/org/nongnu/libvob/util CachingMap.java
Date: Thu, 24 Apr 2003 08:04:47 -0400

CVSROOT:        /cvsroot/libvob
Module name:    libvob
Changes by:     Tuomas J. Lukka <address@hidden>        03/04/24 08:04:47

Modified files:
        org/nongnu/libvob/util: CachingMap.java 

Log message:
        Allow detection of removal

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/libvob/libvob/org/nongnu/libvob/util/CachingMap.java.diff?tr1=1.1&tr2=1.2&r1=text&r2=text

Patches:
Index: libvob/org/nongnu/libvob/util/CachingMap.java
diff -u libvob/org/nongnu/libvob/util/CachingMap.java:1.1 
libvob/org/nongnu/libvob/util/CachingMap.java:1.2
--- libvob/org/nongnu/libvob/util/CachingMap.java:1.1   Thu Mar 27 03:24:10 2003
+++ libvob/org/nongnu/libvob/util/CachingMap.java       Thu Apr 24 08:04:47 2003
@@ -39,6 +39,18 @@
     private HashMap map = new HashMap();
     private int maxEntries;
 
+    /** An interface for values that need to know when they
+     * are removed from the map.
+     */
+    public interface Removable {
+       /** Called when this value was removed (from the given key.
+        */
+       void wasRemoved(Object key);
+    }
+
+    /** Create a new cachingmap with the given maximum number of entries.
+     * @param maxEntries Number of entries to keep. 0 = don't cache anything.
+     */
     public CachingMap(int maxEntries) {
        this.maxEntries = maxEntries;
        if(maxEntries < 0) throw new Error("Can't keep so few.");
@@ -51,13 +63,16 @@
        int toRemove = map.size() - maxEntries;
        while(toRemove-- > 0) {
            Iterator i = map.keySet().iterator();
-           for(int nth = (int)(Math.random() * 22); nth > 0; nth--) {
+           for(int nth = (int)(Math.random() * map.size()); nth > 0; nth--) {
                i.next();
                if(!i.hasNext()) 
                    i = map.keySet().iterator();
            }
-           i.next();
-           i.remove();
+           Object rkey = i.next();
+           Object rval = map.get(key);
+           if(rval instanceof Removable) 
+               ((Removable)rval).wasRemoved(rkey);
+           map.remove(rkey);
        }
        return ret;
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]