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 02:06:39 UTC

groovy git commit: Refine the API of EvictableCache and javadoc

Repository: groovy
Updated Branches:
  refs/heads/master 0d8da15cc -> 06b295681


Refine the API of EvictableCache and javadoc


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

Branch: refs/heads/master
Commit: 06b295681eb125c45463b6943a7f7721a6a02a44
Parents: 0d8da15
Author: sunlan <su...@apache.org>
Authored: Mon Dec 11 10:06:00 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Dec 11 10:06:00 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java     | 55 ++++++++++++++++++--
 .../groovy/runtime/memoize/EvictableCache.java  |  7 +++
 .../groovy/runtime/memoize/MemoizeCache.java    | 11 ++++
 3 files changed, 70 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/06b29568/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 13e668f..f24706c 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/CommonCache.java
@@ -31,7 +31,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 /**
  *
- * Represents a simple cache, which is thread safe and backed by a {@link java.util.Map} instance
+ * Represents a simple key-value cache, which is thread safe and backed by a {@link java.util.Map} instance
  *
  * @param <K> type of the keys
  * @param <V> type of the values
@@ -53,8 +53,8 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
 
     /**
      * Another LRU cache, which is slower than {@link LRUCache} but will not put same value multi-times concurrently
-     * @param initialCapacity
-     * @param maxSize
+     * @param initialCapacity initial capacity of the LRU cache
+     * @param maxSize max size of the LRU cache
      */
     public CommonCache(final int initialCapacity, final int maxSize) {
         this(new LinkedHashMap<K, V>(initialCapacity, 0.75f, true) {
@@ -65,14 +65,26 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         });
     }
 
+    /**
+     * Another LRU cache with the default initial capacity(16)
+     * @param maxSize max size of the LRU cache
+     * @see #CommonCache(int, int)
+     */
     public CommonCache(final int maxSize) {
         this(16, maxSize);
     }
 
+    /**
+     * Constructs a cache backed by the specified {@link java.util.Map} instance
+     * @param map the {@link java.util.Map} instance
+     */
     public CommonCache(Map<K, V> map) {
         this.map = map;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V get(K key) {
         if (null == key) {
@@ -87,6 +99,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V put(K key, V value) {
         writeLock.lock();
@@ -98,6 +113,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
 
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V getAndPut(K key, ValueProvider<K, V> valueProvider) {
         return getAndPut(key, valueProvider, true);
@@ -139,6 +157,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         return value;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Collection<V> values() {
         readLock.lock();
@@ -149,6 +170,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Set<K> keys() {
         readLock.lock();
@@ -159,6 +183,22 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean containsKey(K key) {
+        readLock.lock();
+        try {
+            return map.containsKey(key);
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public int size() {
         readLock.lock();
@@ -169,6 +209,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public V remove(K key) {
         writeLock.lock();
@@ -179,6 +222,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public Collection<V> clear() {
         Collection<V> values;
@@ -194,6 +240,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
         return values;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void cleanUpNullReferences() {
         writeLock.lock();

http://git-wip-us.apache.org/repos/asf/groovy/blob/06b29568/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 acce66d..43bc8bd 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/EvictableCache.java
@@ -63,6 +63,13 @@ public interface EvictableCache<K, V> extends MemoizeCache<K, V> {
     Set<K> keys();
 
     /**
+     * Determines if the cache contains an entry for the specified key.
+     * @param key key whose presence in this cache is to be tested.
+     * @return true if the cache contains a mapping for the specified key
+     */
+    boolean containsKey(K key);
+
+    /**
      * Get the size of the cache
      * @return the size of the cache
      */

http://git-wip-us.apache.org/repos/asf/groovy/blob/06b29568/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java b/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
index e7f5584..aa0eb8e 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/MemoizeCache.java
@@ -27,8 +27,19 @@ package org.codehaus.groovy.runtime.memoize;
  */
 public interface MemoizeCache<K, V> {
 
+    /**
+     * Associates the specified value with the specified key in the cache.
+     * @param key key with which the specified value is to be associated
+     * @param value value to be associated with the specified key
+     * @return null, or the old value if the key associated with the specified key.
+     */
     V put(K key, V value);
 
+    /**
+     * Gets a value from the cache
+     * @param key the key whose associated value is to be returned
+     * @return the value, or null, if it does not exist.
+     */
     V get(K key);
 
     /**