Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X ce617b756 -> c8a077e2e
Fix finalizeReference method override for ManagedReference subclasses (closes #220) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c8a077e2 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c8a077e2 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c8a077e2 Branch: refs/heads/GROOVY_2_4_X Commit: c8a077e2ea0984ebe2ba5fb88c3217ab8ccaef96 Parents: ce617b7 Author: John Wagenleitner <[email protected]> Authored: Sun Jan 24 11:12:38 2016 -0800 Committer: John Wagenleitner <[email protected]> Committed: Wed Jan 27 21:36:36 2016 -0800 ---------------------------------------------------------------------- .../groovy/util/ManagedConcurrentMap.java | 21 +++++++--- .../groovy/util/ManagedConcurrentValueMap.java | 4 +- .../groovy/util/ManagedDoubleKeyMap.java | 11 ++++-- .../codehaus/groovy/util/ManagedLinkedList.java | 1 + .../groovy/util/ManagedConcurrentMapTest.groovy | 41 ++++++++++++++++++++ .../util/ManagedConcurrentValueMapTest.groovy | 38 ++++++++++++++++++ .../groovy/util/ManagedDoubleKeyMapTest.groovy | 41 ++++++++++++++++++++ 7 files changed, 146 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/main/org/codehaus/groovy/util/ManagedConcurrentMap.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/util/ManagedConcurrentMap.java b/src/main/org/codehaus/groovy/util/ManagedConcurrentMap.java index 0c1a7c7..db44a00 100644 --- a/src/main/org/codehaus/groovy/util/ManagedConcurrentMap.java +++ b/src/main/org/codehaus/groovy/util/ManagedConcurrentMap.java @@ -76,9 +76,18 @@ public class ManagedConcurrentMap<K,V> extends AbstractConcurrentMap<K,V> { return hash; } - public void finalizeRef() { - super.finalizeReference(); + @Override + public void finalizeReference() { segment.removeEntry(this); + super.finalizeReference(); + } + + /** + * @deprecated use finalizeReference + */ + @Deprecated + public void finalizeRef() { + finalizeReference(); } } @@ -90,18 +99,20 @@ public class ManagedConcurrentMap<K,V> extends AbstractConcurrentMap<K,V> { setValue(value); } + @Override public V getValue() { return value; } + @Override public void setValue(V value) { this.value = value; } - - public void finalizeRef() { + @Override + public void finalizeReference() { value = null; - super.finalizeRef(); + super.finalizeReference(); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/main/org/codehaus/groovy/util/ManagedConcurrentValueMap.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/util/ManagedConcurrentValueMap.java b/src/main/org/codehaus/groovy/util/ManagedConcurrentValueMap.java index a9d12c6..f28ee68 100644 --- a/src/main/org/codehaus/groovy/util/ManagedConcurrentValueMap.java +++ b/src/main/org/codehaus/groovy/util/ManagedConcurrentValueMap.java @@ -22,7 +22,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * This is a basic implementation of a map able to forget its values. This - * map uses internally a ConcurrentHashMap, thus should be save for concurrency. + * map uses internally a ConcurrentHashMap, thus should be safe for concurrency. * hashcode and equals are used to find the entries and should thus be implemented * properly for the keys. This map does not support null keys. * @author <a href="mailto:[email protected]">Jochen "blackdrag" Theodorou</a> @@ -67,8 +67,8 @@ public class ManagedConcurrentValueMap<K,V> { ManagedReference<V> ref = new ManagedReference<V>(bundle, value) { @Override public void finalizeReference() { + internalMap.remove(key, this); super.finalizeReference(); - internalMap.remove(key, get()); } }; internalMap.put(key, ref); http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java b/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java index 65b2ee6..9dfe170 100644 --- a/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java +++ b/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java @@ -48,8 +48,10 @@ public class ManagedDoubleKeyMap<K1,K2,V> extends AbstractConcurrentDoubleKeyMap this.entry = entry; } - public void finalizeRef() { + @Override + public void finalizeReference() { this.entry.clean(); + super.finalizeReference(); } } @@ -87,8 +89,6 @@ public class ManagedDoubleKeyMap<K1,K2,V> extends AbstractConcurrentDoubleKeyMap public void clean() { segment.removeEntry(this); - ref1.clear(); - ref2.clear(); } } @@ -99,17 +99,20 @@ public class ManagedDoubleKeyMap<K1,K2,V> extends AbstractConcurrentDoubleKeyMap super(bundle, key1, key2, hash, segment); } + @Override public V getValue() { return value; } + @Override public void setValue(V value) { this.value = value; } + @Override public void clean() { - super.clean(); value = null; + super.clean(); } } } http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/main/org/codehaus/groovy/util/ManagedLinkedList.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/util/ManagedLinkedList.java b/src/main/org/codehaus/groovy/util/ManagedLinkedList.java index cc06c2a..5f7bab5 100644 --- a/src/main/org/codehaus/groovy/util/ManagedLinkedList.java +++ b/src/main/org/codehaus/groovy/util/ManagedLinkedList.java @@ -40,6 +40,7 @@ public class ManagedLinkedList<T> { super(bundle, value); } + @Override public void finalizeReference() { if (previous != null && previous.next != null) { previous.next = next; http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy b/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy new file mode 100644 index 0000000..7fe81f6 --- /dev/null +++ b/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy @@ -0,0 +1,41 @@ +/* + * 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.util + +class ManagedConcurrentMapTest extends GroovyTestCase { + + ManagedConcurrentMap<Object, String> map = + new ManagedConcurrentMap<Object, String>(ReferenceBundle.getHardBundle()) + + void testEntriesRemoveSelfFromMapWhenFinalized() { + List<ManagedReference<Object>> entries = [] + for (int i = 0; i < 5; i++) { + entries << map.getOrPut(new Object(), "Object ${i}") + } + + assert map.size() == 5 + assert map.fullSize() == 5 + + entries*.finalizeReference() + + assert map.size() == 0 + assert map.fullSize() == 0 + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/test/org/codehaus/groovy/util/ManagedConcurrentValueMapTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/util/ManagedConcurrentValueMapTest.groovy b/src/test/org/codehaus/groovy/util/ManagedConcurrentValueMapTest.groovy new file mode 100644 index 0000000..90e59dd --- /dev/null +++ b/src/test/org/codehaus/groovy/util/ManagedConcurrentValueMapTest.groovy @@ -0,0 +1,38 @@ +/* + * 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.util + +class ManagedConcurrentValueMapTest extends GroovyTestCase { + + ManagedConcurrentValueMap<String, Object> map = + new ManagedConcurrentValueMap<String, Object>(ReferenceBundle.getHardBundle()) + + void testEntriesRemoveSelfFromMapWhenFinalized() { + for (int i = 0; i < 5; i++) { + map.put("Key${i}", new Object()) + } + + assert [email protected]() == 5 + + Collection<ManagedReference<Object>> values = [email protected]() + values*.finalizeReference() + + assert [email protected]() == 0 + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/c8a077e2/src/test/org/codehaus/groovy/util/ManagedDoubleKeyMapTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/util/ManagedDoubleKeyMapTest.groovy b/src/test/org/codehaus/groovy/util/ManagedDoubleKeyMapTest.groovy new file mode 100644 index 0000000..82a90d9 --- /dev/null +++ b/src/test/org/codehaus/groovy/util/ManagedDoubleKeyMapTest.groovy @@ -0,0 +1,41 @@ +/* + * 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.util + +class ManagedDoubleKeyMapTest extends GroovyTestCase { + + ManagedDoubleKeyMap<Object, Object, String> map = + new ManagedDoubleKeyMap<Object, Object, String>(ReferenceBundle.getHardBundle()) + + void testEntriesRemoveSelfFromMapWhenFinalized() { + def entries = [] + for (int i = 0; i < 5; i++) { + entries << map.getOrPut(new Object(), new Object(), "Value${i}") + } + + assert map.size() == 5 + assert map.fullSize() == 5 + + entries*.clean() + + assert map.size() == 0 + assert map.fullSize() == 0 + } + +}
