From 8575437626bb777a999d718fa18d76b18f73d7e3 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 3 Sep 2012 15:33:12 +0000
Subject: [PATCH]   * LHMCache: New util, replacing several private versions

---
 .../java/src/net/i2p/kademlia/KBucketSet.java | 18 ++------------
 .../src/net/i2p/client/I2PSessionImpl.java    | 21 ++--------------
 .../client/naming/BlockfileNamingService.java |  3 ++-
 .../i2p/client/naming/DummyNamingService.java | 18 ++------------
 core/java/src/net/i2p/data/SDSCache.java      | 18 ++------------
 core/java/src/net/i2p/util/LHMCache.java      | 24 +++++++++++++++++++
 6 files changed, 34 insertions(+), 68 deletions(-)
 create mode 100644 core/java/src/net/i2p/util/LHMCache.java

diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java
index 3b8b09db6c..72ca6e574c 100644
--- a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java
+++ b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java
@@ -14,7 +14,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -24,6 +23,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 import net.i2p.I2PAppContext;
 import net.i2p.data.DataHelper;
 import net.i2p.data.SimpleDataStructure;
+import net.i2p.util.LHMCache;
 import net.i2p.util.Log;
 
 /**
@@ -658,7 +658,7 @@ public class KBucketSet<T extends SimpleDataStructure> {
         public Range(T us, int bValue) {
             _bValue = bValue;
             _bigUs = new BigInteger(1, us.getData());
-            _distanceCache = new LHM(256);
+            _distanceCache = new LHMCache(256);
         }
 
         /** @return 0 to max-1 or -1 for us */
@@ -697,20 +697,6 @@ public class KBucketSet<T extends SimpleDataStructure> {
         }
     }
 
-    private static class LHM<K, V> extends LinkedHashMap<K, V> {
-        private final int _max;
-
-        public LHM(int max) {
-            super(max, 0.75f, true);
-            _max = max;
-        }
-
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
-            return size() > _max;
-        }
-    }
-
     /**
      *  For Collections.binarySearch.
      *  getRangeBegin == getRangeEnd.
diff --git a/core/java/src/net/i2p/client/I2PSessionImpl.java b/core/java/src/net/i2p/client/I2PSessionImpl.java
index 349f3d6782..eb1c6845c2 100644
--- a/core/java/src/net/i2p/client/I2PSessionImpl.java
+++ b/core/java/src/net/i2p/client/I2PSessionImpl.java
@@ -18,7 +18,6 @@ import java.net.Socket;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -44,6 +43,7 @@ import net.i2p.internal.I2CPMessageQueue;
 import net.i2p.internal.InternalClientManager;
 import net.i2p.internal.QueuedI2CPMessageReader;
 import net.i2p.util.I2PAppThread;
+import net.i2p.util.LHMCache;
 import net.i2p.util.Log;
 import net.i2p.util.SimpleScheduler;
 import net.i2p.util.SimpleTimer;
@@ -140,7 +140,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
     /**
      *  @since 0.8.9
      */
-    private static final LookupCache _lookupCache = new LookupCache(16);
+    private static final Map<Hash, Destination> _lookupCache = new LHMCache(16);
 
     /** SSL interface (only) @since 0.8.3 */
     protected static final String PROP_ENABLE_SSL = "i2cp.SSL";
@@ -985,21 +985,4 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
         buf.append(getPrefix());
         return buf.toString();
     }
-
-    /**
-     *  @since 0.8.9
-     */
-    private static class LookupCache extends LinkedHashMap<Hash, Destination> {
-        private final int _max;
-
-        public LookupCache(int max) {
-            super(max, 0.75f, true);
-            _max = max;
-        }
-
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<Hash, Destination> eldest) {
-            return size() > _max;
-        }
-    }
 }
diff --git a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
index 04af6d6460..db7f9eebc0 100644
--- a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
+++ b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
@@ -29,6 +29,7 @@ import net.i2p.data.DataFormatException;
 import net.i2p.data.DataHelper;
 import net.i2p.data.Destination;
 import net.i2p.data.Hash;
+import net.i2p.util.LHMCache;
 import net.i2p.util.Log;
 import net.i2p.util.SecureFileOutputStream;
 
@@ -134,7 +135,7 @@ public class BlockfileNamingService extends DummyNamingService {
         super(context);
         _lists = new ArrayList();
         _invalid = new ArrayList();
-        _negativeCache = new LHM(NEGATIVE_CACHE_SIZE);
+        _negativeCache = new LHMCache(NEGATIVE_CACHE_SIZE);
         BlockFile bf = null;
         RAIFile raf = null;
         boolean readOnly = false;
diff --git a/core/java/src/net/i2p/client/naming/DummyNamingService.java b/core/java/src/net/i2p/client/naming/DummyNamingService.java
index 63c6cf2648..3ce7a83984 100644
--- a/core/java/src/net/i2p/client/naming/DummyNamingService.java
+++ b/core/java/src/net/i2p/client/naming/DummyNamingService.java
@@ -7,13 +7,13 @@
  */
 package net.i2p.client.naming;
 
-import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 
 import net.i2p.I2PAppContext;
 import net.i2p.data.Destination;
+import net.i2p.util.LHMCache;
 
 /**
  * A Dummy naming service that can only handle base64 and b32 destinations.
@@ -30,7 +30,7 @@ class DummyNamingService extends NamingService {
      *  Classes should take care to call removeCache() for any entries that
      *  are invalidated.
      */
-    private static final Map<String, Destination> _cache = new LHM(CACHE_MAX_SIZE);
+    private static final Map<String, Destination> _cache = new LHMCache(CACHE_MAX_SIZE);
 
     /** 
      * The naming service should only be constructed and accessed through the 
@@ -115,18 +115,4 @@ class DummyNamingService extends NamingService {
             _cache.clear();
         }
     }
-
-    protected static class LHM<K, V> extends LinkedHashMap<K, V> {
-        private final int _max;
-
-        public LHM(int max) {
-            super(max, 0.75f, true);
-            _max = max;
-        }
-
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
-            return size() > _max;
-        }
-    }
 }
diff --git a/core/java/src/net/i2p/data/SDSCache.java b/core/java/src/net/i2p/data/SDSCache.java
index e8f7d302d6..0c635084db 100644
--- a/core/java/src/net/i2p/data/SDSCache.java
+++ b/core/java/src/net/i2p/data/SDSCache.java
@@ -6,10 +6,10 @@ import java.io.InputStream;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
-import java.util.LinkedHashMap;
 import java.util.Map;
 
 import net.i2p.I2PAppContext;
+import net.i2p.util.LHMCache;
 import net.i2p.util.SimpleByteCache;
 
 /**
@@ -71,7 +71,7 @@ public class SDSCache<V extends SimpleDataStructure> {
      */
     public SDSCache(Class<V> rvClass, int len, int max) {
         int size = (int) (max * FACTOR);
-        _cache = new LHM(size);
+        _cache = new LHMCache(size);
         _datalen = len;
         try {
             _rvCon = rvClass.getConstructor(conArg);
@@ -181,18 +181,4 @@ public class SDSCache<V extends SimpleDataStructure> {
             rv ^= (data[i] << (i*8));
         return Integer.valueOf(rv);
     }
-
-    private static class LHM<K, V> extends LinkedHashMap<K, V> {
-        private final int _max;
-
-        public LHM(int max) {
-            super(max, 0.75f, true);
-            _max = max;
-        }
-
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
-            return size() > _max;
-        }
-    }
 }
diff --git a/core/java/src/net/i2p/util/LHMCache.java b/core/java/src/net/i2p/util/LHMCache.java
new file mode 100644
index 0000000000..b0818f9fba
--- /dev/null
+++ b/core/java/src/net/i2p/util/LHMCache.java
@@ -0,0 +1,24 @@
+package net.i2p.util;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ *  A LinkedHashMap with a maximum size, for use as
+ *  an LRU cache. Unsynchronized.
+ *
+ *  @since 0.9.3
+ */
+public class LHMCache<K, V> extends LinkedHashMap<K, V> {
+    private final int _max;
+
+    public LHMCache(int max) {
+        super(max, 0.75f, true);
+        _max = max;
+    }
+
+    @Override
+    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+        return size() > _max;
+    }
+}
-- 
GitLab