You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2016/05/03 05:08:28 UTC
[3/3] groovy git commit: refactor(core): remove unused utility
classes (closes #324)
refactor(core): remove unused utility classes (closes #324)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/05e9f563
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/05e9f563
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/05e9f563
Branch: refs/heads/master
Commit: 05e9f5639c9fed6fc3406d594b85aca9892faf76
Parents: 3bcadcf
Author: John Wagenleitner <jw...@apache.org>
Authored: Mon May 2 20:00:24 2016 -0700
Committer: John Wagenleitner <jw...@apache.org>
Committed: Mon May 2 20:04:49 2016 -0700
----------------------------------------------------------------------
.../util/AbstractConcurrentDoubleKeyMap.java | 200 -------------------
.../codehaus/groovy/util/DoubleKeyHashMap.java | 93 ---------
.../groovy/util/ManagedDoubleKeyMap.java | 118 -----------
.../groovy/util/ManagedDoubleKeyMapTest.groovy | 58 ------
4 files changed, 469 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/05e9f563/src/main/org/codehaus/groovy/util/AbstractConcurrentDoubleKeyMap.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/util/AbstractConcurrentDoubleKeyMap.java b/src/main/org/codehaus/groovy/util/AbstractConcurrentDoubleKeyMap.java
deleted file mode 100644
index e83b641..0000000
--- a/src/main/org/codehaus/groovy/util/AbstractConcurrentDoubleKeyMap.java
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * 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;
-
-public abstract class AbstractConcurrentDoubleKeyMap<K1,K2,V> extends AbstractConcurrentMapBase {
- public AbstractConcurrentDoubleKeyMap(Object segmentInfo) {
- super(segmentInfo);
- }
-
- static <K1,K2> int hash(K1 key1, K2 key2) {
- int h = 31*key1.hashCode() + key2.hashCode();
- h += ~(h << 9);
- h ^= (h >>> 14);
- h += (h << 4);
- h ^= (h >>> 10);
- return h;
- }
-
- public V get(K1 key1, K2 key2) {
- int hash = hash(key1, key2);
- return segmentFor(hash).get(key1, key2, hash);
- }
-
- public Entry<K1,K2,V> getOrPut(K1 key1, K2 key2, V value) {
- int hash = hash(key1,key2);
- return segmentFor(hash).getOrPut(key1, key2, hash, value);
- }
-
- public void put(K1 key1, K2 key2, V value) {
- int hash = hash(key1, key2);
- segmentFor(hash).put(key1, key2, hash).setValue(value);
- }
-
- public void remove(K1 key1, K2 key2) {
- int hash = hash(key1, key2);
- segmentFor(hash).remove(key1, key2, hash);
- }
-
- public final Segment<K1,K2,V> segmentFor(int hash) {
- return (Segment<K1,K2,V>) segments[(hash >>> segmentShift) & segmentMask];
- }
-
- abstract static class Segment<K1,K2,V> extends AbstractConcurrentMapBase.Segment {
- Segment(int initialCapacity) {
- super(initialCapacity);
- }
-
- V get(K1 key1, K2 key2, int hash) {
- Object[] tab = table;
- Object o = tab[hash & (tab.length - 1)];
- if (o != null) {
- if (o instanceof Entry) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) o;
- if (e.isEqual(key1,key2,hash)) {
- return e.getValue();
- }
- }
- else {
- Object arr [] = (Object[]) o;
- for (int i = 0; i != arr.length; ++i) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) arr [i];
- if (e != null && e.isEqual(key1, key2, hash))
- return e.getValue();
- }
- }
- }
- return null;
- }
-
- Entry<K1,K2,V> getOrPut(K1 key1, K2 key2, int hash, V value) {
- Object[] tab = table;
- Object o = tab[hash & (tab.length - 1)];
- if (o != null) {
- if (o instanceof Entry) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) o;
- if (e.isEqual(key1,key2,hash)) {
- return e;
- }
- }
- else {
- Object arr [] = (Object[]) o;
- for (int i = 0; i != arr.length; ++i) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) arr [i];
- if (e != null && e.isEqual(key1, key2, hash))
- return e;
- }
- }
- }
-
- final Entry<K1,K2,V> kvEntry = put(key1, key2, hash);
- kvEntry.setValue(value);
- return kvEntry;
- }
-
- Entry<K1,K2,V> put(K1 key1, K2 key2, int hash) {
- lock();
- try {
- rehashIfThresholdExceeded();
-
- Object[] tab = table;
- final int index = hash & (tab.length - 1);
- final Object o = tab[index];
- if (o != null) {
- if (o instanceof Entry) {
- final Entry<K1,K2,V> e = (Entry<K1,K2,V>) o;
- if (e.isEqual(key1,key2,hash)) {
- return e;
- }
- final Object[] arr = new Object[2];
- final Entry<K1,K2,V> res = createEntry(key1, key2, hash);
- arr [0] = res;
- arr [1] = e;
- tab[index] = arr;
- count++; // write-volatile
- return res;
- }
- else {
- Object arr [] = (Object[]) o;
- for (int i = 0; i != arr.length; ++i) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) arr [i];
- if (e != null && e.isEqual(key1, key2, hash)) {
- return e;
- }
- }
- final Object[] newArr = new Object[arr.length+1];
- final Entry<K1,K2,V> res = createEntry(key1,key2, hash);
- newArr[0] = res;
- System.arraycopy(arr, 0, newArr, 1, arr.length);
- tab[index] = newArr;
- count++; // write-volatile
- return res;
- }
- }
-
- final Entry<K1,K2,V> res = createEntry(key1, key2, hash);
- tab[index] = res;
- count++; // write-volatile
- return res;
-
- } finally {
- unlock();
- }
- }
-
- public void remove(K1 key1, K2 key2, int hash) {
- lock();
- try {
- int c = count-1;
- final Object[] tab = table;
- final int index = hash & (tab.length - 1);
- Object o = tab[index];
-
- if (o != null) {
- if (o instanceof Entry) {
- if (((Entry<K1,K2,V>)o).isEqual(key1, key2, hash)) {
- tab[index] = null;
- count = c;
- }
- }
- else {
- Object arr [] = (Object[]) o;
- for (int i = 0; i < arr.length; i++) {
- Entry<K1,K2,V> e = (Entry<K1,K2,V>) arr[i];
- if (e != null && e.isEqual(key1, key2, hash)) {
- arr [i] = null;
- count = c;
- break;
- }
- }
- }
- }
- }
- finally {
- unlock();
- }
- }
-
- protected abstract Entry<K1,K2,V> createEntry(K1 key1, K2 key2, int hash);
- }
-
- interface Entry<K1, K2, V> extends AbstractConcurrentMapBase.Entry<V>{
- boolean isEqual(K1 key1, K2 key2, int hash);
- }
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/05e9f563/src/main/org/codehaus/groovy/util/DoubleKeyHashMap.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/util/DoubleKeyHashMap.java b/src/main/org/codehaus/groovy/util/DoubleKeyHashMap.java
deleted file mode 100644
index 7d67a79..0000000
--- a/src/main/org/codehaus/groovy/util/DoubleKeyHashMap.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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;
-
-public class DoubleKeyHashMap extends ComplexKeyHashMap
-{
- public static class Entry extends ComplexKeyHashMap.Entry{
- public Object key1, key2;
- }
-
- public final Object get(Object key1, Object key2) {
- int h = hash (31*key1.hashCode()+key2.hashCode());
- ComplexKeyHashMap.Entry e = table [h & (table.length-1)];
- for (; e != null; e = e.next)
- if (e.hash == h && checkEquals(e, key1, key2))
- return e;
-
- return null;
- }
-
- public boolean checkEquals(ComplexKeyHashMap.Entry e, Object key1, Object key2) {
- Entry ee = (Entry) e;
- return ee.key1 == key1 && ee.key2 == key2;
- }
-
- public Entry getOrPut(Object key1, Object key2)
- {
- int h = hash (31*key1.hashCode()+key2.hashCode());
- final int index = h & (table.length - 1);
- ComplexKeyHashMap.Entry e = table [index];
- for (; e != null; e = e.next)
- if (e.hash == h && checkEquals( e, key1, key2))
- return (Entry) e;
-
- ComplexKeyHashMap.Entry entry = createEntry(key1, key2, h, index);
- table [index] = entry;
-
- if ( ++size == threshold )
- resize(2*table.length);
-
- return (Entry) entry;
- }
-
- private ComplexKeyHashMap.Entry createEntry(Object key1, Object key2, int h, int index)
- {
- Entry entry = createEntry ();
- entry.next = table [index];
- entry.hash = h;
- entry.key1 = key1;
- entry.key2 = key2;
- return entry;
- }
-
- public Entry createEntry() {
- return new Entry ();
- }
-
- public final ComplexKeyHashMap.Entry remove(Object key1, Object key2) {
- int h = hash (31*key1.hashCode()+key2.hashCode());
- int index = h & (table.length -1);
- for (ComplexKeyHashMap.Entry e = table [index], prev = null; e != null; prev = e, e = e.next ) {
- if (e.hash == h && checkEquals(e, key1, key2)) {
- if (prev == null)
- table [index] = e.next;
- else
- prev.next = e.next;
- size--;
-
- e.next = null;
- return e;
- }
- }
-
- return null;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/05e9f563/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
deleted file mode 100644
index 9dfe170..0000000
--- a/src/main/org/codehaus/groovy/util/ManagedDoubleKeyMap.java
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * 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;
-
-
-public class ManagedDoubleKeyMap<K1,K2,V> extends AbstractConcurrentDoubleKeyMap<K1,K2,V> {
- public ManagedDoubleKeyMap(ReferenceBundle bundle) {
- super(bundle);
- }
-
- protected AbstractConcurrentDoubleKeyMap.Segment<K1,K2,V> createSegment(Object segmentInfo, int cap) {
- ReferenceBundle bundle = (ReferenceBundle) segmentInfo;
- return new Segment<K1,K2,V>(bundle, cap);
- }
-
- static class Segment<K1,K2,V> extends AbstractConcurrentDoubleKeyMap.Segment<K1,K2,V>{
- private ReferenceBundle bundle;
- public Segment(ReferenceBundle bundle, int cap) {
- super(cap);
- this.bundle = bundle;
- }
-
- protected AbstractConcurrentDoubleKeyMap.Entry<K1,K2,V> createEntry(K1 key1, K2 key2, int hash) {
- return new EntryWithValue(bundle, key1, key2, hash, this);
- }
- }
-
- static class Ref<K> extends ManagedReference<K> {
- final Entry entry;
- public Ref(ReferenceBundle bundle, K referent, Entry entry) {
- super(bundle, referent);
- this.entry = entry;
- }
-
- @Override
- public void finalizeReference() {
- this.entry.clean();
- super.finalizeReference();
- }
- }
-
- public static class Entry<K1,K2, V> implements AbstractConcurrentDoubleKeyMap.Entry<K1,K2,V> {
- private final int hash;
- final Ref<K1> ref1;
- final Ref<K2> ref2;
- final Segment segment;
-
- public Entry(ReferenceBundle bundle, K1 key1, K2 key2, int hash, Segment segment) {
- this.hash = hash;
- this.segment = segment;
- ref1 = new Ref(bundle, key1, this);
- ref2 = new Ref(bundle, key2, this);
- }
-
- public boolean isValid() {
- return ref1.get() != null && ref2.get () != null;
- }
-
- public boolean isEqual(K1 key1, K2 key2, int hash) {
- return this.hash == hash && ref1.get() == key1 && ref2.get() == key2;
- }
-
- public V getValue() {
- return (V)this;
- }
-
- public void setValue(V value) {
- }
-
- public int getHash() {
- return hash;
- }
-
- public void clean() {
- segment.removeEntry(this);
- }
- }
-
- private static class EntryWithValue<K1,K2,V> extends Entry<K1,K2,V> {
- private V value;
-
- public EntryWithValue(ReferenceBundle bundle, K1 key1, K2 key2, int hash, Segment segment) {
- 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() {
- value = null;
- super.clean();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/05e9f563/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
deleted file mode 100644
index a7e1a97..0000000
--- a/src/test/org/codehaus/groovy/util/ManagedDoubleKeyMapTest.groovy
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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
- }
-
- void testPutForSameHashBucket() {
- Object obj1 = new Object() { @Override public int hashCode() { return 42; } }
- Object obj2 = new Object() { @Override public int hashCode() { return 42; } }
- Object obj3 = new Object() { @Override public int hashCode() { return 42; } }
-
- map.put(obj1, obj2, 'obj1')
- map.put(obj2, obj1, 'obj2')
- map.put(obj3, obj2, 'obj3')
-
- assert map.size() == 3
- assert map.fullSize() == 3
-
- assert map.get(obj1, obj2) == 'obj1'
- assert map.get(obj2, obj1) == 'obj2'
- assert map.get(obj3, obj2) == 'obj3'
- }
-
-}