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/11 06:03:08 UTC
[groovy] 01/02: Rollback changes and just mark them deprecated
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
commit b188863038b181bea2848102c47e574995d225d7
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jul 11 11:27:41 2020 +0800
Rollback changes and just mark them deprecated
---
.../reflection/GroovyClassValuePreJava7.java | 2 +-
.../metaclass/ThreadManagedMetaBeanProperty.java | 2 +-
.../groovy/util/AbstractConcurrentMap.java | 5 +-
.../groovy/util/AbstractConcurrentMapBase.java | 8 +-
.../codehaus/groovy/util/ManagedConcurrentMap.java | 190 +--------------------
.../util/AbstractConcurrentMapSegmentTest.groovy | 1 -
.../groovy/util/ManagedConcurrentMapTest.groovy | 2 +
7 files changed, 14 insertions(+), 196 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java b/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java
index 1e4db24..7b95d64 100644
--- a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java
+++ b/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java
@@ -94,7 +94,7 @@ class GroovyClassValuePreJava7<T> implements GroovyClassValue<T> {
@Override
public T get(Class<?> type) {
// the value isn't use in the getOrPut call - see the EntryWithValue constructor above
- T value = map.getOrPut(type, null).get();
+ T value = ((EntryWithValue)map.getOrPut(type, null)).getValue();
//all entries are guaranteed to be EntryWithValue. Value can only be null if computeValue returns null
return value;
}
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ThreadManagedMetaBeanProperty.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ThreadManagedMetaBeanProperty.java
index b6ac4cf..5074908 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ThreadManagedMetaBeanProperty.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ThreadManagedMetaBeanProperty.java
@@ -176,7 +176,7 @@ public class ThreadManagedMetaBeanProperty extends MetaBeanProperty {
* @see groovy.lang.MetaMethod#invoke(java.lang.Object, java.lang.Object[])
*/
public Object invoke(Object object, Object[] arguments) {
- return instance2Prop.computeIfAbsent(object, k -> getInitialValue());
+ return instance2Prop.getOrPut(object, getInitialValue()).getValue();
}
}
diff --git a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java
index 0c5557f..d67fd88 100644
--- a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java
+++ b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java
@@ -20,7 +20,6 @@ package org.codehaus.groovy.util;
@Deprecated
public abstract class AbstractConcurrentMap<K, V> extends AbstractConcurrentMapBase {
- protected AbstractConcurrentMap() {}
public AbstractConcurrentMap(Object segmentInfo) {
super(segmentInfo);
@@ -35,7 +34,7 @@ public abstract class AbstractConcurrentMap<K, V> extends AbstractConcurrentMapB
return (V) segmentFor(hash).get(key, hash);
}
- public Entry<K,V> getOrPut$$bridge(K key, V value) {
+ public Entry<K,V> getOrPut(K key, V value) {
int hash = hash(key);
return segmentFor(hash).getOrPut(key, hash, value);
}
@@ -45,7 +44,7 @@ public abstract class AbstractConcurrentMap<K, V> extends AbstractConcurrentMapB
segmentFor(hash).put(key, hash, value);
}
- public void remove$$bridge(K key) {
+ public void remove(K key) {
int hash = hash(key);
segmentFor(hash).remove(key, hash);
}
diff --git a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
index e21de26..57ac68e 100644
--- a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
+++ b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java
@@ -26,11 +26,9 @@ public abstract class AbstractConcurrentMapBase {
protected static final int MAXIMUM_CAPACITY = 1 << 30;
static final int MAX_SEGMENTS = 1 << 16;
static final int RETRIES_BEFORE_LOCK = 2;
- int segmentMask;
- int segmentShift;
- protected Segment[] segments;
-
- protected AbstractConcurrentMapBase() {}
+ final int segmentMask;
+ final int segmentShift;
+ protected final Segment[] segments;
public AbstractConcurrentMapBase(Object segmentInfo) {
int sshift = 0;
diff --git a/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java b/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java
index 9d2371a..7329711 100644
--- a/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java
+++ b/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java
@@ -18,199 +18,21 @@
*/
package org.codehaus.groovy.util;
-import java.util.Collection;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-/**
- * Represents a concurrent map based on {@link ConcurrentHashMap}.
- * When the value is collected by JVM, the entry will be removed too.
- * <p />
- * Note: {@code extends AbstractConcurrentMap<K, V>} is just for binary compatibility
- *
- * @param <K> the key type
- * @param <V> the value type
- */
-public class ManagedConcurrentMap<K, V> extends AbstractConcurrentMap<K, V> {
- private final ConcurrentMap<IdentityKey<K>, ManagedValue<V>> internalMap = new ConcurrentHashMap<>();
- private final ReferenceBundle bundle;
-
+@Deprecated
+public class ManagedConcurrentMap<K,V> extends AbstractConcurrentMap<K,V> {
+ protected ReferenceBundle bundle;
public ManagedConcurrentMap(ReferenceBundle bundle) {
- if (bundle == null) throw new IllegalArgumentException("bundle must not be null");
+ super(bundle);
this.bundle = bundle;
+ if (bundle==null) throw new IllegalArgumentException("bundle must not be null");
}
- /**
- * Returns the value stored for the given key at the point of call.
- *
- * @param key a non null key
- * @return the value stored in the map for the given key
- */
- public V get(Object key) {
- return toValue(internalMap.get(new IdentityKey(key)));
- }
-
- /**
- * Sets a new value for a given key. an older value is overwritten.
- *
- * @param key a non null key
- * @param value the new value
- */
- public void put(final K key, V value) {
- final IdentityKey<K> k = new IdentityKey<K>(key);
- internalMap.put(k, toManagedValue(k, value));
- }
-
- /**
- * If the key is absent, put the key and value into the map and return the value.
- * Or return the key related value directly
- *
- * @param key the key to look up
- * @param mappingFunction providing the value if the key is absent
- * @return the key related value
- */
- public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
- return toValue(getOrPut(key, mappingFunction));
- }
-
- /**
- * If the key is absent, put the key and value into the map and return the value.
- * Or return the key related {@link ManagedReference} instance, which wraps the value
- *
- * @param key the key to look up
- * @param value the value to put if the key is absent
- * @return the {@link ManagedReference} instance wrapping the value
- */
- public ManagedValue<V> getOrPut(K key, V value) {
- return getOrPut(key, k -> value);
- }
-
- private ManagedValue<V> getOrPut(K key, Function<? super K, ? extends V> mappingFunction) {
- final IdentityKey<K> key1 = new IdentityKey<K>(key);
- return internalMap.computeIfAbsent(key1, k -> toManagedValue(key1, mappingFunction.apply(k.key)));
- }
-
- /**
- * Remove the key related entry
- *
- * @param key the key to look up
- * @return the removed entry related value
- */
- public V remove(Object key) {
- return toValue(internalMap.remove(new IdentityKey(key)));
- }
-
- /**
- * Returns map values
- */
- public Collection<V> values() {
- return internalMap.values().stream().map(this::toValue).collect(Collectors.toList());
- }
-
- /**
- * Returns map size
- */
- public int size() {
- return internalMap.size();
- }
-
- private V toValue(ManagedValue<V> mv) {
- if (null == mv) return null;
-
- return mv.value;
- }
-
- private ManagedValue<V> toManagedValue(IdentityKey<K> key, V value) {
- final ManagedValue<V> mv = new ManagedValue<V>(value);
- ManagedReference<V> ref = new ManagedReference<V>(bundle, value) {
- @Override
- public void finalizeReference() {
- internalMap.remove(key, mv);
- super.finalizeReference();
- }
- };
- mv.setManagedReference(ref);
-
- return mv;
- }
-
- /**
- * Represents identity key of {@link ManagedConcurrentMap}
- *
- * @param <K> the key type
- * @since 4.0.0
- */
- private static class IdentityKey<K> {
- private final K key;
-
- private IdentityKey(K key) {
- this.key = key;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof IdentityKey)) return false;
- return key == ((IdentityKey<?>) o).key;
- }
-
- @Override
- public int hashCode() {
- int hash = System.identityHashCode(key);
- hash += (hash << 15) ^ 0xffffcd7d;
- hash ^= (hash >>> 10);
- hash += (hash << 3);
- hash ^= (hash >>> 6);
- hash += (hash << 2) + (hash << 14);
- hash ^= (hash >>> 16);
- return hash;
- }
- }
-
- /**
- * Represents the managed value
- * @param <V> the value type
- * @since 4.0.0
- */
- public static class ManagedValue<V> implements Finalizable {
- private ManagedReference<V> mr;
- private V value;
-
- private ManagedValue(V value) {
- this.value = value;
- }
-
- private void setManagedReference(ManagedReference<V> mr) {
- this.mr = mr;
- }
-
- public V get() {
- return value;
- }
-
- public void clear() {
- this.value = null;
- mr.clear();
- }
-
- @Override
- public void finalizeReference() {
- this.value = null;
- mr.finalizeReference();
- }
- }
-
- // ------------------------- the following members are deprecated ---------------------------
- @Deprecated
protected Segment<K,V> createSegment(Object segmentInfo, int cap) {
ReferenceBundle bundle = (ReferenceBundle) segmentInfo;
if (bundle==null) throw new IllegalArgumentException("bundle must not be null");
return new ManagedConcurrentMap.Segment<K,V>(bundle, cap);
}
- @Deprecated
public static class Segment<K,V> extends AbstractConcurrentMap.Segment<K,V>{
private static final long serialVersionUID = 2742952509311037869L;
protected final ReferenceBundle bundle;
@@ -227,7 +49,6 @@ public class ManagedConcurrentMap<K, V> extends AbstractConcurrentMap<K, V> {
}
}
- @Deprecated
public static class Entry<K,V> extends ManagedReference<K> implements AbstractConcurrentMap.Entry<K,V> {
private final Segment segment;
private final int hash;
@@ -272,7 +93,6 @@ public class ManagedConcurrentMap<K, V> extends AbstractConcurrentMap<K, V> {
}
}
- @Deprecated
public static class EntryWithValue<K,V> extends Entry<K,V> {
private V value;
diff --git a/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy b/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy
index 1706715..4f275f5 100644
--- a/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy
+++ b/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy
@@ -21,7 +21,6 @@ package org.codehaus.groovy.util
import org.junit.Before
import org.junit.Test
-@Deprecated
class AbstractConcurrentMapSegmentTest {
private static final Integer INITIAL_SEGMENT_SIZE = 100
private static final Integer SEGMENT_THRESHOLD = 0.75f * INITIAL_SEGMENT_SIZE
diff --git a/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy b/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy
index 357e37d..d87704f 100644
--- a/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy
+++ b/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy
@@ -32,10 +32,12 @@ class ManagedConcurrentMapTest extends GroovyTestCase {
}
assert map.size() == 5
+ assert map.fullSize() == 5
entries*.finalizeReference()
assert map.size() == 0
+ assert map.fullSize() == 0
}
}