You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/12/11 15:35:26 UTC

groovy git commit: Minor refactoring: Make SimpleCache base on CommonCache

Repository: groovy
Updated Branches:
  refs/heads/master d66bbd754 -> 2b6dced3a


Minor refactoring: Make SimpleCache base on CommonCache


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

Branch: refs/heads/master
Commit: 2b6dced3aa40451c00573a27da46041c037a82fd
Parents: d66bbd7
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 23:35:20 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 23:35:20 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/2b6dced3/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/2b6dced3/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/2b6dced3/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();
     }
 }