groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Refine EvictableCache and SimpleCache
Date Sat, 09 Dec 2017 16:45:14 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X ddb2b9233 -> 79c65a9df


Refine EvictableCache and SimpleCache

(cherry picked from commit b6f39eb)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 79c65a9df44d1dc5f77f33812d1308fa15a1423a
Parents: ddb2b92
Author: sunlan <sunlan@apache.org>
Authored: Sun Dec 10 00:03:00 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sun Dec 10 00:45:02 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/EvictableCache.java  |  13 ++
 .../groovy/runtime/memoize/SimpleCache.java     |  38 ++++-
 .../groovy/runtime/memoize/SimpleCacheTest.java | 161 +++++++++++++++++++
 3 files changed, 210 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/79c65a9d/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
index a882947..acce66d 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
@@ -19,6 +19,7 @@
 package org.codehaus.groovy.runtime.memoize;
 
 import java.util.Collection;
+import java.util.Set;
 
 /**
  * Represents an evictable memoize cache with its essential methods
@@ -56,6 +57,18 @@ public interface EvictableCache<K, V> extends MemoizeCache<K,
V> {
     Collection<V> values();
 
     /**
+     * Get all keys associated to cached values
+     * @return all keys
+     */
+    Set<K> keys();
+
+    /**
+     * Get the size of the cache
+     * @return the size of the cache
+     */
+    int size();
+
+    /**
      * Represents a provider used to create value
      * @param <K> type of the key
      * @param <V> type of the value

http://git-wip-us.apache.org/repos/asf/groovy/blob/79c65a9d/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index f2b3287..3eef52b 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 /**
@@ -35,11 +36,19 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  * @since 2.5.0
  */
 public class SimpleCache<K, V> implements EvictableCache<K, V> {
-    private final Map<K, V> map = new HashMap<>();
+    private final Map<K, V> map;
     private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
     private final ReentrantReadWriteLock.ReadLock readLock = rwl.readLock();
     private final ReentrantReadWriteLock.WriteLock writeLock = rwl.writeLock();
 
+    public SimpleCache() {
+        this(new HashMap<>());
+    }
+
+    public SimpleCache(Map<K, V> map) {
+        this.map = map;
+    }
+
     @Override
     public V get(K key) {
         if (null == key) {
@@ -108,7 +117,32 @@ public class SimpleCache<K, V> implements EvictableCache<K,
V> {
 
     @Override
     public Collection<V> values() {
-        return map.values();
+        readLock.lock();
+        try {
+            return map.values();
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    @Override
+    public Set<K> keys() {
+        readLock.lock();
+        try {
+            return map.keySet();
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    @Override
+    public int size() {
+        readLock.lock();
+        try {
+            return map.size();
+        } finally {
+            readLock.unlock();
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/groovy/blob/79c65a9d/src/test/org/codehaus/groovy/runtime/memoize/SimpleCacheTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/memoize/SimpleCacheTest.java b/src/test/org/codehaus/groovy/runtime/memoize/SimpleCacheTest.java
new file mode 100644
index 0000000..55ce393
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/memoize/SimpleCacheTest.java
@@ -0,0 +1,161 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.runtime.memoize;
+
+import org.apache.groovy.util.Maps;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+
+public class SimpleCacheTest {
+    @Test
+    public void get() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertEquals("Daniel", sc.get("name"));
+        Assert.assertEquals("Male", sc.get("gender"));
+        Assert.assertEquals("Shanghai", sc.get("city"));
+        Assert.assertNull(sc.get("foo"));
+    }
+
+    @Test
+    public void put() {
+        SimpleCache<String, String> sc = new SimpleCache<>();
+
+        Assert.assertNull(sc.put("name", "Daniel"));
+        Assert.assertEquals("Daniel", sc.get("name"));
+
+        Assert.assertEquals("Daniel", sc.put("name", "sunlan"));
+        Assert.assertEquals("sunlan", sc.get("name"));
+    }
+
+    @Test
+    public void getAndPut() {
+        SimpleCache<String, String> sc = new SimpleCache<>();
+
+        EvictableCache.ValueProvider vp =
+                new EvictableCache.ValueProvider<String, String>() {
+            @Override
+            public String provide(String key) {
+                return "Chinese";
+            }
+        };
+
+        Assert.assertEquals("Chinese", sc.getAndPut("language", vp,false));
+        Assert.assertNull(sc.get("language"));
+
+        Assert.assertEquals("Chinese", sc.getAndPut("language", vp));
+        Assert.assertEquals("Chinese", sc.get("language"));
+    }
+
+    @Test
+    public void values() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertArrayEquals(new String[] {"Daniel", "Male", "Shanghai"}, sc.values().toArray(new
String[0]));
+    }
+
+    @Test
+    public void keys() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertArrayEquals(new String[] {"name", "gender", "city"}, sc.keys().toArray(new
String[0]));
+    }
+
+    @Test
+    public void size() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertEquals(3, sc.size());
+    }
+
+    @Test
+    public void remove() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new HashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertEquals("Shanghai", sc.remove("city"));
+        Assert.assertNull(sc.get("city"));
+    }
+
+    @Test
+    public void clear() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", "Shanghai")
+                        )
+                );
+
+        Assert.assertArrayEquals(new String[] {"Daniel", "Male", "Shanghai"}, sc.values().toArray(new
String[0]));
+    }
+
+    @Test
+    public void cleanUpNullReferences() {
+        SimpleCache<String, String> sc =
+                new SimpleCache<>(
+                        new LinkedHashMap<>(
+                                Maps.of("name", "Daniel",
+                                        "gender", "Male",
+                                        "city", null)
+                        )
+                );
+
+        sc.cleanUpNullReferences();
+        Assert.assertArrayEquals(new String[] {"Daniel", "Male"}, sc.values().toArray(new
String[0]));
+    }
+}


Mime
View raw message