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 2020/07/09 16:37:17 UTC

[groovy] branch GROOVY-9631 updated: Fix failing build

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-9631
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-9631 by this push:
     new 6e5ecd7  Fix failing build
6e5ecd7 is described below

commit 6e5ecd74fd4893f65eccd7dad1a09b9b829457b1
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Jul 10 00:35:53 2020 +0800

    Fix failing build
---
 .../groovy/util/AbstractConcurrentMapBase.java     | 43 +++++++++++++++++-----
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
index 8735e61..0ffe096 100644
--- a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
+++ b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
@@ -21,9 +21,10 @@ package org.codehaus.groovy.util;
 import java.util.Collection;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Function;
+import java.util.stream.Collectors;
 
 public abstract class AbstractConcurrentMapBase<K, V> {
-    private final ConcurrentHashMap<K, ManagedReference<V>> internalMap = new ConcurrentHashMap<>();
+    private final ConcurrentHashMap<Key<K>, ManagedReference<V>> internalMap = new ConcurrentHashMap<>();
     private ReferenceBundle bundle;
 
     public AbstractConcurrentMapBase(ReferenceBundle bundle) {
@@ -37,9 +38,7 @@ public abstract class AbstractConcurrentMapBase<K, V> {
      * @return the value stored in the map for the given key
      */
     public V get(Object key) {
-        ManagedReference<V> ref = internalMap.get(key);
-        if (ref != null) return ref.get();
-        return null;
+        return toValue(internalMap.get(new Key(key)));
     }
 
     /**
@@ -49,7 +48,7 @@ public abstract class AbstractConcurrentMapBase<K, V> {
      * @param value the new value
      */
     public void put(final K key, V value) {
-        internalMap.put(key, toManagedReference(key, value));
+        internalMap.put(new Key(key), toManagedReference(key, value));
     }
 
     public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
@@ -57,15 +56,15 @@ public abstract class AbstractConcurrentMapBase<K, V> {
     }
 
     public ManagedReference<V> getOrPut(K key, Function<? super K, ? extends V> mappingFunction) {
-        return internalMap.computeIfAbsent(key, k -> toManagedReference(k, mappingFunction.apply(k)));
+        return internalMap.computeIfAbsent(new Key(key), k -> toManagedReference(k.key, mappingFunction.apply(k.key)));
     }
 
     public V remove(Object key) {
-        return toValue(internalMap.remove(key));
+        return toValue(internalMap.remove(new Key(key)));
     }
 
-    public Collection<ManagedReference<V>> values() {
-        return internalMap.values();
+    public Collection<V> values() {
+        return internalMap.values().stream().map(this::toValue).collect(Collectors.toList());
     }
 
     public int size() {
@@ -99,4 +98,30 @@ public abstract class AbstractConcurrentMapBase<K, V> {
 
         boolean isValid();
     }
+
+    private static class Key<K> {
+        K key;
+
+        Key(K key) {
+            this.key = key;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (!(o instanceof Key)) return false;
+            Key<?> other = (Key<?>) o;
+            return key == other.key;
+        }
+
+        @Override
+        public int hashCode() {
+            int h = System.identityHashCode(key);
+            h += ~(h << 9);
+            h ^=  (h >>> 14);
+            h +=  (h << 4);
+            h ^=  (h >>> 10);
+            return h;
+        }
+    }
 }