groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [3/4] groovy git commit: Minor refactoring: Make SimpleCache base on CommonCache
Date Mon, 11 Dec 2017 15:52:46 GMT
Minor refactoring: Make SimpleCache base on CommonCache

(cherry picked from commit 2b6dced)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/01bc293e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/01bc293e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/01bc293e

Branch: refs/heads/GROOVY_2_5_X
Commit: 01bc293e0d8ddc7ea06b37c2682d48f93334e58d
Parents: 0975cc9
Author: sunlan <sunlan@apache.org>
Authored: Mon Dec 11 23:35:20 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Mon Dec 11 23:52:31 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java     |  4 ++
 .../main/java/groovy/json/internal/CharBuf.java | 12 +++---
 .../java/groovy/json/internal/SimpleCache.java  | 41 +++++++-------------
 3 files changed, 23 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/01bc293e/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
index a98e3d5..8070a50 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -200,4 +200,8 @@ public class CommonCache<K, V> implements EvictableCache<K, V>
{
         }
     }
 
+    @Override
+    public String toString() {
+        return map.toString();
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/01bc293e/subprojects/groovy-json/src/main/java/groovy/json/internal/CharBuf.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/main/java/groovy/json/internal/CharBuf.java b/subprojects/groovy-json/src/main/java/groovy/json/internal/CharBuf.java
index 18f5d6a..f7e1b29 100644
--- a/subprojects/groovy-json/src/main/java/groovy/json/internal/CharBuf.java
+++ b/subprojects/groovy-json/src/main/java/groovy/json/internal/CharBuf.java
@@ -138,7 +138,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public final CharBuf addInt(Integer key) {
         if (icache == null) {
-            icache = new SimpleCache<Integer, char[]>(20, CacheType.LRU);
+            icache = new SimpleCache<Integer, char[]>(20);
         }
         char[] chars = icache.get(key);
 
@@ -204,7 +204,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public final CharBuf addDouble(Double key) {
         if (dcache == null) {
-            dcache = new SimpleCache<Double, char[]>(20, CacheType.LRU);
+            dcache = new SimpleCache<Double, char[]>(20);
         }
         char[] chars = dcache.get(key);
 
@@ -232,7 +232,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public final CharBuf addFloat(Float key) {
         if (fcache == null) {
-            fcache = new SimpleCache<Float, char[]>(20, CacheType.LRU);
+            fcache = new SimpleCache<Float, char[]>(20);
         }
         char[] chars = fcache.get(key);
 
@@ -701,7 +701,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public CharBuf addBigDecimal(BigDecimal key) {
         if (bigDCache == null) {
-            bigDCache = new SimpleCache<BigDecimal, char[]>(20, CacheType.LRU);
+            bigDCache = new SimpleCache<BigDecimal, char[]>(20);
         }
         char[] chars = bigDCache.get(key);
 
@@ -720,7 +720,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public CharBuf addBigInteger(BigInteger key) {
         if (bigICache == null) {
-            bigICache = new SimpleCache<BigInteger, char[]>(20, CacheType.LRU);
+            bigICache = new SimpleCache<BigInteger, char[]>(20);
         }
         char[] chars = bigICache.get(key);
 
@@ -744,7 +744,7 @@ public class CharBuf extends Writer implements CharSequence {
 
     public final CharBuf addLong(Long key) {
         if (lcache == null) {
-            lcache = new SimpleCache<Long, char[]>(20, CacheType.LRU);
+            lcache = new SimpleCache<Long, char[]>(20);
         }
         char[] chars = lcache.get(key);
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/01bc293e/subprojects/groovy-json/src/main/java/groovy/json/internal/SimpleCache.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-json/src/main/java/groovy/json/internal/SimpleCache.java b/subprojects/groovy-json/src/main/java/groovy/json/internal/SimpleCache.java
index 68c9c91..d3d97d2 100644
--- a/subprojects/groovy-json/src/main/java/groovy/json/internal/SimpleCache.java
+++ b/subprojects/groovy-json/src/main/java/groovy/json/internal/SimpleCache.java
@@ -18,69 +18,54 @@
  */
 package groovy.json.internal;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
+import org.codehaus.groovy.runtime.memoize.CommonCache;
 
 /**
  * @author Richard Hightower
  */
 public class SimpleCache<K, V> implements Cache<K, V> {
-
-    Map<K, V> map = new LinkedHashMap();
-
-    private static class InternalCacheLinkedList<K, V> extends LinkedHashMap<K,
V> {
-        final int limit;
-
-        InternalCacheLinkedList(final int limit, final boolean lru) {
-            super(16, 0.75f, lru);
-            this.limit = limit;
-        }
-
-        protected final boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
-            return super.size() > limit;
-        }
-    }
+    private CommonCache<K, V> cache;
 
     public SimpleCache(final int limit, CacheType type) {
         if (type.equals(CacheType.LRU)) {
-            map = new InternalCacheLinkedList<K, V>(limit, true);
+            cache = new CommonCache<K, V>(limit);
         } else {
-            map = new InternalCacheLinkedList<K, V>(limit, false);
+            cache = new CommonCache<K, V>(16, limit, false);
         }
     }
 
     public SimpleCache(final int limit) {
-        map = new InternalCacheLinkedList<K, V>(limit, true);
+        this(limit, CacheType.LRU);
     }
 
     public void put(K key, V value) {
-        map.put(key, value);
+        cache.put(key, value);
     }
 
     public V get(K key) {
-        return map.get(key);
+        return cache.get(key);
     }
 
     //For testing only
 
     public V getSilent(K key) {
-        V value = map.get(key);
+        V value = cache.get(key);
         if (value != null) {
-            map.remove(key);
-            map.put(key, value);
+            cache.remove(key);
+            cache.put(key, value);
         }
         return value;
     }
 
     public void remove(K key) {
-        map.remove(key);
+        cache.remove(key);
     }
 
     public int size() {
-        return map.size();
+        return cache.size();
     }
 
     public String toString() {
-        return map.toString();
+        return cache.toString();
     }
 }


Mime
View raw message