This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY-11520 in repository https://gitbox.apache.org/repos/asf/groovy.git
commit e28faefa6e3024cbf92c32a2f440049db43bc65c Author: Daniel Sun <[email protected]> AuthorDate: Sun Nov 10 12:07:21 2024 +0800 GROOVY-11520: Remove deprecated collections --- src/main/java/groovy/lang/MetaClassImpl.java | 78 ----- .../groovy/reflection/GroovyClassValueFactory.java | 13 +- .../reflection/GroovyClassValuePreJava7.java | 107 ------- .../groovy/util/AbstractConcurrentMap.java | 209 ------------- .../groovy/util/AbstractConcurrentMapBase.java | 337 --------------------- .../codehaus/groovy/util/ComplexKeyHashMap.java | 179 ----------- .../codehaus/groovy/util/ManagedConcurrentMap.java | 127 -------- .../codehaus/groovy/util/ManagedLinkedList.java | 147 --------- .../org/codehaus/groovy/util/SingleKeyHashMap.java | 162 ---------- .../org/codehaus/groovy/util/TripleKeyHashMap.java | 87 ------ .../util/AbstractConcurrentMapSegmentTest.groovy | 193 ------------ .../groovy/util/ManagedConcurrentMapTest.groovy | 43 --- .../groovy/util/ManagedLinkedlistTest.groovy | 71 ----- 13 files changed, 1 insertion(+), 1752 deletions(-) diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java index a7669ab88c..bc6234617c 100644 --- a/src/main/java/groovy/lang/MetaClassImpl.java +++ b/src/main/java/groovy/lang/MetaClassImpl.java @@ -75,9 +75,7 @@ import org.codehaus.groovy.runtime.metaclass.TransformMetaMethod; import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; import org.codehaus.groovy.runtime.typehandling.NumberMathModificationInfo; import org.codehaus.groovy.runtime.wrappers.Wrapper; -import org.codehaus.groovy.util.ComplexKeyHashMap; import org.codehaus.groovy.util.FastArray; -import org.codehaus.groovy.util.SingleKeyHashMap; import org.codehaus.groovy.vmplugin.VMPlugin; import org.codehaus.groovy.vmplugin.VMPluginFactory; import org.objectweb.asm.Opcodes; @@ -3895,82 +3893,6 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass { metaMethodIndex.clearCaches(); } - @Deprecated - private static final SingleKeyHashMap.Copier NAME_INDEX_COPIER = value -> { - if (value instanceof FastArray) { - return ((FastArray) value).copy(); - } else { - return value; - } - }; - - @Deprecated - private static final SingleKeyHashMap.Copier METHOD_INDEX_COPIER = value -> SingleKeyHashMap.copy(new SingleKeyHashMap(false), (SingleKeyHashMap) value, NAME_INDEX_COPIER); - - /** - * @deprecated use {@link LinkedHashMap} instead - */ - @Deprecated - static class MethodIndex extends Index { - MethodIndex(boolean b) { - super(false); - } - - MethodIndex(int size) { - super(size); - } - - MethodIndex() { - super(); - } - - MethodIndex copy() { - return (MethodIndex) SingleKeyHashMap.copy(new MethodIndex(false), this, METHOD_INDEX_COPIER); - } - - @Override - protected Object clone() throws CloneNotSupportedException { - return super.clone(); - } - } - - /** - * @deprecated use {@link LinkedHashMap} instead - */ - @Deprecated - public static class Index extends SingleKeyHashMap { - - public Index(int size) { - } - - public Index() { - } - - public Index(boolean size) { - super(false); - } - - public SingleKeyHashMap getNotNull(CachedClass key) { - Entry res = getOrPut(key); - if (res.value == null) { - res.value = new SingleKeyHashMap(); - } - return (SingleKeyHashMap) res.value; - } - - public void put(CachedClass key, SingleKeyHashMap value) { - getOrPut(key).value = value; - } - - public SingleKeyHashMap getNullable(CachedClass clazz) { - return (SingleKeyHashMap) get(clazz); - } - - public boolean checkEquals(ComplexKeyHashMap.Entry e, Object key) { - return ((Entry) e).key.equals(key); - } - } - private static class DummyMetaMethod extends MetaMethod { @Override diff --git a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValueFactory.java b/src/main/java/org/codehaus/groovy/reflection/GroovyClassValueFactory.java index 0565f92716..26513309e7 100644 --- a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValueFactory.java +++ b/src/main/java/org/codehaus/groovy/reflection/GroovyClassValueFactory.java @@ -18,22 +18,11 @@ */ package org.codehaus.groovy.reflection; -import org.apache.groovy.util.SystemUtil; import org.codehaus.groovy.reflection.GroovyClassValue.ComputeValue; import org.codehaus.groovy.reflection.v7.GroovyClassValueJava7; class GroovyClassValueFactory { - /** - * This flag is introduced as a (hopefully) temporary workaround for a JVM bug, that is to say that using - * ClassValue prevents the classes and classloaders from being unloaded. - * See https://bugs.openjdk.java.net/browse/JDK-8136353 - * This issue does not exist on IBM Java (J9) so use ClassValue by default on that JVM. - */ - private static final boolean USE_CLASSVALUE = Boolean.parseBoolean(SystemUtil.getSystemPropertySafe("groovy.use.classvalue", "true")); - public static <T> GroovyClassValue<T> createGroovyClassValue(ComputeValue<T> computeValue) { - return (USE_CLASSVALUE) - ? new GroovyClassValueJava7<>(computeValue) - : new GroovyClassValuePreJava7<>(computeValue); + return new GroovyClassValueJava7<>(computeValue); } } diff --git a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java b/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java deleted file mode 100644 index 7b95d6447f..0000000000 --- a/src/main/java/org/codehaus/groovy/reflection/GroovyClassValuePreJava7.java +++ /dev/null @@ -1,107 +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.reflection; - -import org.codehaus.groovy.util.Finalizable; -import org.codehaus.groovy.util.ManagedConcurrentMap; -import org.codehaus.groovy.util.ReferenceBundle; - -/** Approximation of Java 7's {@link java.lang.ClassValue} that works on earlier versions of Java. - * Note that this implementation isn't as good at Java 7's; it doesn't allow for some GC'ing that Java 7 would allow. - * But, it's good enough for our use. - * - * @param <T> - */ -@Deprecated -class GroovyClassValuePreJava7<T> implements GroovyClassValue<T> { - private static final ReferenceBundle weakBundle = ReferenceBundle.getWeakBundle(); - - private class EntryWithValue extends ManagedConcurrentMap.EntryWithValue<Class<?>,T>{ - - public EntryWithValue(GroovyClassValuePreJava7Segment segment, Class<?> key, int hash) { - super(weakBundle, segment, key, hash, computeValue.computeValue(key)); - } - - @Override - public void setValue(T value) { - if(value!=null) super.setValue(value); - } - - @Override - public void finalizeReference() { - T value = getValue(); - if (value instanceof Finalizable) { - ((Finalizable) value).finalizeReference(); - } - super.finalizeReference(); - } - } - - private class GroovyClassValuePreJava7Segment extends ManagedConcurrentMap.Segment<Class<?>,T> { - - private static final long serialVersionUID = 1289753977947029168L; - - GroovyClassValuePreJava7Segment(ReferenceBundle bundle, int initialCapacity) { - super(bundle, initialCapacity); - } - - @Override - protected EntryWithValue createEntry(Class<?> key, int hash, - T unused) { - return new EntryWithValue(this, key, hash); - } - } - - private class GroovyClassValuePreJava7Map extends ManagedConcurrentMap<Class<?>,T> { - - public GroovyClassValuePreJava7Map() { - super(weakBundle); - } - - @Override - protected GroovyClassValuePreJava7Segment createSegment(Object segmentInfo, int cap) { - ReferenceBundle bundle = (ReferenceBundle) segmentInfo; - if (bundle==null) throw new IllegalArgumentException("bundle must not be null "); - return new GroovyClassValuePreJava7Segment(bundle, cap); - } - - } - - private final ComputeValue<T> computeValue; - - private final GroovyClassValuePreJava7Map map = new GroovyClassValuePreJava7Map(); - - public GroovyClassValuePreJava7(ComputeValue<T> computeValue){ - this.computeValue = computeValue; - } - - @Override - public T get(Class<?> type) { - // the value isn't use in the getOrPut call - see the EntryWithValue constructor above - 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; - } - - @Override - public void remove(Class<?> type) { - map.remove(type); - } - -} diff --git a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java deleted file mode 100644 index 6bd87ad703..0000000000 --- a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMap.java +++ /dev/null @@ -1,209 +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; - -@Deprecated -public abstract class AbstractConcurrentMap<K, V> extends AbstractConcurrentMapBase { - - public AbstractConcurrentMap(Object segmentInfo) { - super(segmentInfo); - } - - @Override - public Segment segmentFor (int hash) { - return (Segment) super.segmentFor(hash); - } - - public V get(K key) { - int hash = hash(key); - return (V) segmentFor(hash).get(key, hash); - } - - public Entry<K,V> getOrPut(K key, V value) { - int hash = hash(key); - return segmentFor(hash).getOrPut(key, hash, value); - } - - public void put(K key, V value) { - int hash = hash(key); - segmentFor(hash).put(key, hash, value); - } - - public void remove(K key) { - int hash = hash(key); - segmentFor(hash).remove(key, hash); - } - - public abstract static class Segment<K,V> extends AbstractConcurrentMapBase.Segment { - - private static final long serialVersionUID = -2392526467736920612L; - - protected Segment(int initialCapacity) { - super(initialCapacity); - } - - public final V get(K key, int hash) { - Object[] tab = table; - Object o = tab[hash & (tab.length - 1)]; - if (o != null) { - if (o instanceof Entry) { - Entry<K,V> e = (Entry) o; - if (e.isEqual(key, hash)) { - return e.getValue(); - } - } - else { - Object[] arr = (Object[]) o; - for (Object value : arr) { - Entry<K, V> e = (Entry<K, V>) value; - if (e != null && e.isEqual(key, hash)) { - return e.getValue(); - } - } - } - } - return null; - } - - public final Entry<K,V> getOrPut(K key, int hash, V value) { - Object[] tab = table; - Object o = tab[hash & (tab.length - 1)]; - if (o != null) { - if (o instanceof Entry) { - Entry<K,V> e = (Entry) o; - if (e.isEqual(key, hash)) { - return e; - } - } - else { - Object[] arr = (Object[]) o; - for (Object item : arr) { - Entry<K, V> e = (Entry<K, V>) item; - if (e != null && e.isEqual(key, hash)) { - return e; - } - } - } - } - return put(key, hash, value); - } - - public final Entry put(K key, int hash, V value) { - lock(); - try { - rehashIfThresholdExceeded(); - - Object[] tab = table; - int index = hash & (tab.length - 1); - Object o = tab[index]; - if (o != null) { - if (o instanceof Entry) { - Entry e = (Entry) o; - if (e.isEqual(key, hash)) { - e.setValue(value); - return e; - } - else { - Object[] arr = new Object [2]; - final Entry ee = createEntry(key, hash, value); - arr [0] = ee; - arr [1] = e; - tab[index] = arr; - count++; - return ee; - } - } - else { - Object[] arr = (Object[]) o; - for (Object item : arr) { - Entry e = (Entry) item; - if (e != null && e.isEqual(key, hash)) { - e.setValue(value); - return e; - } - } - - final Entry ee = createEntry(key, hash, value); - for (int i = 0; i < arr.length; i++) { - Entry e = (Entry) arr[i]; - if (e == null) { - arr [i] = ee; - count++; - return ee; - } - } - - Object[] newArr = new Object[arr.length+1]; - newArr [0] = ee; - System.arraycopy(arr, 0, newArr, 1, arr.length); - tab [index] = newArr; - count++; - return ee; - } - } - - Entry e = createEntry(key, hash, value); - tab[index] = e; - count++; // write-volatile - return e; - } finally { - unlock(); - } - } - - public void remove(K key, 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<K,V>)o).isEqual(key, hash)) { - tab[index] = null; - count = c; - } - } - else { - Object[] arr = (Object[]) o; - for (int i = 0; i < arr.length; i++) { - Entry<K,V> e = (Entry<K,V>) arr[i]; - if (e != null && e.isEqual(key, hash)) { - arr [i] = null; - count = c; - break; - } - } - } - } - } - finally { - unlock(); - } - } - - protected abstract Entry<K,V> createEntry(K key, int hash, V value); - } - - public interface Entry<K, V> extends AbstractConcurrentMapBase.Entry<V>{ - boolean isEqual(K key, int hash); - } -} diff --git a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java b/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java deleted file mode 100644 index 57ac68ea73..0000000000 --- a/src/main/java/org/codehaus/groovy/util/AbstractConcurrentMapBase.java +++ /dev/null @@ -1,337 +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; - -import java.util.Collection; -import java.util.LinkedList; - -@Deprecated -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; - final int segmentMask; - final int segmentShift; - protected final Segment[] segments; - - public AbstractConcurrentMapBase(Object segmentInfo) { - int sshift = 0; - int ssize = 1; - while (ssize < 16) { - ++sshift; - ssize <<= 1; - } - segmentShift = 32 - sshift; - segmentMask = ssize - 1; - this.segments = new Segment[ssize]; - - int c = 512 / ssize; - if (c * ssize < 512) - ++c; - int cap = 1; - while (cap < c) - cap <<= 1; - - for (int i = 0; i < this.segments.length; ++i) - this.segments[i] = createSegment(segmentInfo, cap); - } - - protected abstract Segment createSegment(Object segmentInfo, int cap); - - protected static <K> int hash(K key) { - int h = System.identityHashCode(key); - h += ~(h << 9); - h ^= (h >>> 14); - h += (h << 4); - h ^= (h >>> 10); - return h; - } - - public Segment segmentFor(int hash) { - return segments[(hash >>> segmentShift) & segmentMask]; - } - - public int fullSize() { - int count = 0; - for (Segment segment : segments) { - segment.lock(); - try { - for (int j = 0; j < segment.table.length; j++) { - Object o = segment.table[j]; - if (o != null) { - if (o instanceof Entry) { - count++; - } else { - Object[] arr = (Object[]) o; - count += arr.length; - } - } - } - } finally { - segment.unlock(); - } - } - return count; - } - - public int size() { - int count = 0; - for (Segment segment : segments) { - segment.lock(); - try { - for (int j = 0; j < segment.table.length; j++) { - Object o = segment.table[j]; - if (o != null) { - if (o instanceof Entry) { - Entry e = (Entry) o; - if (e.isValid()) - count++; - } else { - Object[] arr = (Object[]) o; - for (Object value : arr) { - Entry info = (Entry) value; - if (info != null && info.isValid()) - count++; - } - } - } - } - } finally { - segment.unlock(); - } - } - return count; - } - - public Collection values() { - Collection result = new LinkedList(); - for (Segment segment : segments) { - segment.lock(); - try { - for (int j = 0; j < segment.table.length; j++) { - Object o = segment.table[j]; - if (o != null) { - if (o instanceof Entry) { - Entry e = (Entry) o; - if (e.isValid()) - result.add(e); - } else { - Object[] arr = (Object[]) o; - for (Object value : arr) { - Entry info = (Entry) value; - if (info != null && info.isValid()) - result.add(info); - } - } - } - } - } finally { - segment.unlock(); - } - } - return result; - } - - public static class Segment extends LockableObject { - private static final long serialVersionUID = -4128828550135386431L; - volatile int count; - - int threshold; - - protected volatile Object[] table; - - protected Segment(int initialCapacity) { - setTable(new Object[initialCapacity]); - } - - void setTable(Object[] newTable) { - threshold = (int) (newTable.length * 0.75f); - table = newTable; - } - - void removeEntry (Entry e) { - lock (); - int newCount = count; - try { - Object [] tab = table; - int index = e.getHash() & (tab.length-1); - Object o = tab[index]; - if (o != null) { - if (o instanceof Entry) { - if (o == e) { - tab [index] = null; - newCount--; - } - } - else { - Object[] arr = (Object[]) o; - Object res = null; - for (Object value : arr) { - Entry info = (Entry) value; - if (info != null) { - if (info != e) { - if (info.isValid()) { - res = put(info, res); - } else { - newCount--; - } - } else { - newCount--; - } - } - } - tab [index] = res; - } - count = newCount; - } - } - finally { - unlock(); - } - } - - void rehashIfThresholdExceeded() { - if(count > threshold) { - rehash(); - } - } - - void rehash() { - Object[] oldTable = table; - int oldCapacity = oldTable.length; - if (oldCapacity >= MAXIMUM_CAPACITY) - return; - - int newCount = 0; - for (int i = 0; i < oldCapacity ; i++) { - Object o = oldTable [i]; - if (o != null) { - if (o instanceof Entry) { - Entry e = (Entry) o; - if (e.isValid()) { - newCount++; - } - else { - oldTable[i] = null; - } - } - else { - Object[] arr = (Object[]) o; - int localCount = 0; - for (int index = 0; index < arr.length; index++) { - Entry e = (Entry) arr[index]; - if (e != null && e.isValid()) { - localCount++; - } - else { - arr [index] = null; - } - } - if (localCount == 0) - oldTable[i] = null; - else - newCount += localCount; - } - } - } - - Object[] newTable = new Object[newCount+1 < threshold ? oldCapacity : oldCapacity << 1]; - int sizeMask = newTable.length - 1; - newCount = 0; - for (Object o : oldTable) { - if (o != null) { - if (o instanceof Entry) { - Entry e = (Entry) o; - if (e.isValid()) { - int index = e.getHash() & sizeMask; - put(e, index, newTable); - newCount++; - } - } else { - Object[] arr = (Object[]) o; - for (Object value : arr) { - Entry e = (Entry) value; - if (e != null && e.isValid()) { - int index = e.getHash() & sizeMask; - put(e, index, newTable); - newCount++; - } - } - } - } - } - - threshold = (int)(newTable.length * 0.75f); - - table = newTable; - count = newCount; - } - - private static void put(Entry ee, int index, Object[] tab) { - Object o = tab[index]; - if (o != null) { - if (o instanceof Entry) { - Object[] arr = new Object [2]; - arr [0] = ee; - arr [1] = o; - tab[index] = arr; - return; - } - else { - Object[] arr = (Object[]) o; - Object[] newArr = new Object[arr.length+1]; - newArr [0] = ee; - System.arraycopy(arr, 0, newArr, 1, arr.length); - tab [index] = newArr; - return; - } - } - tab[index] = ee; - } - - private static Object put(Entry ee, Object o) { - if (o != null) { - if (o instanceof Entry) { - Object[] arr = new Object [2]; - arr [0] = ee; - arr [1] = o; - return arr; - } - else { - Object[] arr = (Object[]) o; - Object[] newArr = new Object[arr.length+1]; - newArr [0] = ee; - System.arraycopy(arr, 0, newArr, 1, arr.length); - return newArr; - } - } - return ee; - } - } - - public interface Entry<V> { - V getValue(); - - void setValue(V value); - - int getHash(); - - boolean isValid(); - } -} diff --git a/src/main/java/org/codehaus/groovy/util/ComplexKeyHashMap.java b/src/main/java/org/codehaus/groovy/util/ComplexKeyHashMap.java deleted file mode 100644 index 52780f4905..0000000000 --- a/src/main/java/org/codehaus/groovy/util/ComplexKeyHashMap.java +++ /dev/null @@ -1,179 +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; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -@Deprecated -public class ComplexKeyHashMap -{ - public static class Entry { - public int hash; - public Entry next; - public Object value; - - public Object getValue() { - return value; - } - - public void setValue(Object value) { - this.value = value; - } - } - - protected Entry[] table; - - protected static final int DEFAULT_CAPACITY = 32; - protected static final int MINIMUM_CAPACITY = 4; - protected static final int MAXIMUM_CAPACITY = 1 << 28; - - protected int size; - protected transient int threshold; - - public ComplexKeyHashMap() { - init(DEFAULT_CAPACITY); - } - - public ComplexKeyHashMap(boolean b) { - } - - public ComplexKeyHashMap(int expectedMaxSize) { - init (capacity(expectedMaxSize)); - } - - public static int hash(int h) { - h += ~(h << 9); - h ^= (h >>> 14); - h += (h << 4); - h ^= (h >>> 10); - return h; - } - - public int size() { - return size; - } - - public boolean isEmpty() { - return size == 0; - } - - public void clear() { - Object[] tab = table; - Arrays.fill(tab, null); - size = 0; - } - - public void init(int initCapacity) { - threshold = (initCapacity * 6)/8; - table = new Entry[initCapacity]; - } - - public void resize(int newLength) { - Entry[] oldTable = table; - int oldLength = table.length; - - Entry[] newTable = new Entry[newLength]; - - for (int j = 0; j < oldLength; j++) { - - for (Entry e = oldTable [j]; e != null;) { - Entry next = e.next; - int index = e.hash & (newLength-1); - - e.next = newTable[index]; - newTable [index] = e; - - e = next; - } - } - - table = newTable; - threshold = (6 * newLength) / 8; - } - - private static int capacity(int expectedMaxSize) { - // Compute min capacity for expectedMaxSize given a load factor of 3/4 - int minCapacity = (8 * expectedMaxSize)/6; - - // Compute the appropriate capacity - int result; - if (minCapacity > MAXIMUM_CAPACITY || minCapacity < 0) { - result = MAXIMUM_CAPACITY; - } else { - result = MINIMUM_CAPACITY; - while (result < minCapacity) - result <<= 1; - } - return result; - } - - public interface EntryIterator { - boolean hasNext (); - Entry next (); - } - - - public ComplexKeyHashMap.Entry[] getTable() { - return table; - } - - public EntryIterator getEntrySetIterator() { - return new EntryIterator() { - Entry next; // next entry to return - int index; // current slot - - { - Entry[] t = table; - int i = t.length; - Entry n = null; - if (size != 0) { // advance to first entry - while (i > 0 && (n = t[--i]) == null) {} - } - next = n; - index = i; - } - - @Override - public boolean hasNext() { - return next != null; - } - - @Override - public Entry next() { - return nextEntry(); - } - - Entry nextEntry() { - Entry e = next; - if (e == null) - throw new NoSuchElementException(); - - Entry n = e.next; - Entry[] t = table; - int i = index; - while (n == null && i > 0) - n = t[--i]; - index = i; - next = n; - return e; - } - }; - } -} diff --git a/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java b/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java deleted file mode 100644 index 9d070ad8f3..0000000000 --- a/src/main/java/org/codehaus/groovy/util/ManagedConcurrentMap.java +++ /dev/null @@ -1,127 +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; - -@Deprecated -public class ManagedConcurrentMap<K,V> extends AbstractConcurrentMap<K,V> { - protected ReferenceBundle bundle; - public ManagedConcurrentMap(ReferenceBundle bundle) { - super(bundle); - this.bundle = bundle; - if (bundle==null) throw new IllegalArgumentException("bundle must not be null"); - } - - @Override - 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); - } - - public static class Segment<K,V> extends AbstractConcurrentMap.Segment<K,V>{ - private static final long serialVersionUID = 2742952509311037869L; - protected final ReferenceBundle bundle; - public Segment(ReferenceBundle bundle, int cap) { - super(cap); - this.bundle = bundle; - if (bundle==null) throw new IllegalArgumentException("bundle must not be null"); - - } - - @Override - protected AbstractConcurrentMap.Entry<K,V> createEntry(K key, int hash, V value) { - if (bundle==null) throw new IllegalArgumentException("bundle must not be null"); - return new EntryWithValue<K,V>(bundle, this, key, hash, value); - } - } - - public static class Entry<K,V> extends ManagedReference<K> implements AbstractConcurrentMap.Entry<K,V> { - private final Segment segment; - private final int hash; - - public Entry(ReferenceBundle bundle, Segment segment, K key, int hash) { - super(bundle, key); - this.segment = segment; - this.hash = hash; - } - - @Override - public boolean isValid() { - return get() != null; - } - - @Override - public boolean isEqual(K key, int hash) { - return this.hash == hash && get() == key; - } - - @Override - public V getValue() { - return (V)this; - } - - @Override - public void setValue(V value) { - } - - @Override - public int getHash() { - return hash; - } - - @Override - public void finalizeReference() { - segment.removeEntry(this); - super.finalizeReference(); - } - - /** - * @deprecated use finalizeReference - */ - @Deprecated - public void finalizeRef() { - finalizeReference(); - } - } - - public static class EntryWithValue<K,V> extends Entry<K,V> { - private V value; - - public EntryWithValue(ReferenceBundle bundle, Segment segment, K key, int hash, V value) { - super(bundle, segment, key, hash); - setValue(value); - } - - @Override - public V getValue() { - return value; - } - - @Override - public void setValue(V value) { - this.value = value; - } - - @Override - public void finalizeReference() { - value = null; - super.finalizeReference(); - } - } -} diff --git a/src/main/java/org/codehaus/groovy/util/ManagedLinkedList.java b/src/main/java/org/codehaus/groovy/util/ManagedLinkedList.java deleted file mode 100644 index 72b3f4d604..0000000000 --- a/src/main/java/org/codehaus/groovy/util/ManagedLinkedList.java +++ /dev/null @@ -1,147 +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; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * This class provides a very simple linked list of memory managed elements. - * This class does not support concurrent modifications nor will it check - * for them. This class is also not thread safe. - * - * @since 1.6 - * @deprecated replaced by {@link ManagedConcurrentLinkedQueue} - */ -@Deprecated -public class ManagedLinkedList<T> { - - private final class Element<V> extends ManagedReference<V> { - Element next; - Element previous; - - public Element(ReferenceBundle bundle, V value) { - super(bundle, value); - } - - @Override - public void finalizeReference() { - if (previous != null && previous.next != null) { - previous.next = next; - } - if (next != null && next.previous != null) { - next.previous = previous; - } - if (this == head) head = next; - next = null; - if (this == tail) tail = previous; - previous = null; - super.finalizeReference(); - } - } - - private final class Iter implements Iterator<T> { - private Element<T> current; - private boolean currentHandled = false; - - Iter() { - current = head; - } - - @Override - public boolean hasNext() { - if (current == null) return false; - if (currentHandled) { - return current.next != null; - } else { - return true; - } - } - - @Override - public T next() { - if (currentHandled) current = current.next; - currentHandled = true; - if (current == null) return null; - return current.get(); - } - - @Override - public void remove() { - if (current != null) current.finalizeReference(); - } - } - - private Element<T> tail; - private Element<T> head; - private final ReferenceBundle bundle; - - public ManagedLinkedList(ReferenceBundle bundle) { - this.bundle = bundle; - } - - /** - * adds a value to the list - * - * @param value the value - */ - public void add(T value) { - Element<T> element = new Element<T>(bundle, value); - element.previous = tail; - if (tail != null) tail.next = element; - tail = element; - if (head == null) head = element; - } - - /** - * returns an iterator, which allows the removal of elements. - * The next() method of the iterator may return null values. This - * is especially the case if the value was removed. - * - * @return the Iterator - */ - public Iterator<T> iterator() { - return new Iter(); - } - - /** - * Returns an array of non-null elements from the source array. - * - * @param tArray the source array - * @return the array - */ - public T[] toArray(T[] tArray) { - List<T> array = new ArrayList<T>(100); - for (Iterator<T> it = iterator(); it.hasNext();) { - T val = it.next(); - if (val != null) array.add(val); - } - return array.toArray(tArray); - } - - /** - * returns if the list is empty - * - * @return true if the list is empty - */ - public boolean isEmpty() { - return head == null; - } -} diff --git a/src/main/java/org/codehaus/groovy/util/SingleKeyHashMap.java b/src/main/java/org/codehaus/groovy/util/SingleKeyHashMap.java deleted file mode 100644 index 86e0990040..0000000000 --- a/src/main/java/org/codehaus/groovy/util/SingleKeyHashMap.java +++ /dev/null @@ -1,162 +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; - -@Deprecated -public class SingleKeyHashMap extends ComplexKeyHashMap -{ - public static class Entry extends ComplexKeyHashMap.Entry{ - public Object key; - - public Object getKey() { - return key; - } - } - - public SingleKeyHashMap () { - super (); - } - - public SingleKeyHashMap (boolean b) { - super (false); - } - - public boolean containsKey(String name) { - return get(name) != null; - } - - public void put(Object key, Object value) { - getOrPut(key).value = value; - } - - public final Object get(Object key) { - int h = hash (key.hashCode()); - ComplexKeyHashMap.Entry e = table [h & (table.length-1)]; - for (; e != null; e = e.next) - if (e.hash == h && ((Entry) e).key.equals(key)) - return ((Entry)e).value; - - return null; - } - - public Entry getOrPut(Object key) - { - int h = hash (key.hashCode()); - final ComplexKeyHashMap.Entry[] t = table; - final int index = h & (t.length - 1); - ComplexKeyHashMap.Entry e = t[index]; - for (; e != null; e = e.next) - if (e.hash == h && ((Entry) e).key.equals(key)) - return (Entry) e; - - Entry entry = new Entry(); - entry.next = t [index]; - entry.hash = h; - entry.key = key; - t[index] = entry; - - if ( ++size == threshold ) - resize(2* t.length); - - return entry; - } - - public Entry getOrPutEntry(Entry element) { - Object key = element.key; - int h = element.hash; - final ComplexKeyHashMap.Entry[] t = table; - final int index = h & (t.length - 1); - ComplexKeyHashMap.Entry e = t[index]; - for (; e != null; e = e.next) - if (e.hash == h && ((Entry) e).key.equals(key)) - return (Entry) e; - - Entry entry = new Entry(); - entry.next = t [index]; - entry.hash = h; - entry.key = key; - t[index] = entry; - - if ( ++size == threshold ) - resize(2* t.length); - - return entry; - } - - public Entry putCopyOfUnexisting(Entry ee) - { - int h = ee.hash; - final ComplexKeyHashMap.Entry[] t = table; - final int index = h & (t.length - 1); - - Entry entry = new Entry(); - entry.next = t [index]; - entry.hash = h; - entry.key = ee.key; - entry.value = ee.value; - t[index] = entry; - - if ( ++size == threshold ) - resize(2* t.length); - - return entry; - } - - public final ComplexKeyHashMap.Entry remove(Object key) { - int h = hash (key.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 && ((Entry) e).key.equals(key)) { - if (prev == null) - table [index] = e.next; - else - prev.next = e.next; - size--; - - e.next = null; - return e; - } - } - - return null; - } - - public static SingleKeyHashMap copy (SingleKeyHashMap dst, SingleKeyHashMap src, Copier copier) { - dst.threshold = src.threshold; - dst.size = src.size; - final int len = src.table.length; - final ComplexKeyHashMap.Entry[] t = new ComplexKeyHashMap.Entry[len], tt = src.table; - for (int i = 0; i != len; ++i) { - for (Entry e = (Entry) tt[i]; e != null; e = (Entry) e.next) { - Entry ee = new Entry(); - ee.hash = e.hash; - ee.key = e.key; - ee.value = copier.copy(e.value); - ee.next = t [i]; - t [i] = ee; - } - } - dst.table = t; - return dst; - } - - public interface Copier { - Object copy (Object value); - } -} diff --git a/src/main/java/org/codehaus/groovy/util/TripleKeyHashMap.java b/src/main/java/org/codehaus/groovy/util/TripleKeyHashMap.java deleted file mode 100644 index 668db9ab25..0000000000 --- a/src/main/java/org/codehaus/groovy/util/TripleKeyHashMap.java +++ /dev/null @@ -1,87 +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; - -@Deprecated -public class TripleKeyHashMap extends ComplexKeyHashMap -{ - public static class Entry extends ComplexKeyHashMap.Entry{ - public Object key1, key2, key3; - } - - public final Object get(Object key1, Object key2, Object key3) { - int h = hash (31*(31*key1.hashCode()+key2.hashCode())+key3.hashCode()); - ComplexKeyHashMap.Entry e = table [h & (table.length-1)]; - for (; e != null; e = e.next) - if (e.hash == h && checkEquals((Entry) e, key1, key2, key3)) - return e; - - return null; - } - - public boolean checkEquals(Entry e, Object key1, Object key2, Object key3) { - return e.key1.equals(key1) && e.key2.equals(key2) && e.key3.equals(key3); - } - - public Entry getOrPut(Object key1, Object key2, Object key3) - { - int h = hash (31*(31*key1.hashCode()+key2.hashCode())+key3.hashCode()); - final int index = h & (table.length - 1); - ComplexKeyHashMap.Entry e = table [index]; - for (; e != null; e = e.next) - if (e.hash == h && checkEquals((Entry) e, key1, key2, key3)) - return (Entry) e; - - Entry entry = createEntry (); - entry.next = table [index]; - entry.hash = h; - entry.key1 = key1; - entry.key2 = key2; - entry.key3 = key3; - table [index] = entry; - - if ( ++size == threshold ) - resize(2*table.length); - - return entry; - } - - public Entry createEntry() { - return new Entry (); - } - - public final ComplexKeyHashMap.Entry remove(Object key1, Object key2, Object key3) { - int h = hash (31*(31*key1.hashCode()+key2.hashCode())+key3.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((Entry) e, key1, key2, key3)) { - if (prev == null) - table [index] = e.next; - else - prev.next = e.next; - size--; - - e.next = null; - return e; - } - } - - return null; - } -} diff --git a/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy b/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy deleted file mode 100644 index 97848eb12d..0000000000 --- a/src/test/org/codehaus/groovy/util/AbstractConcurrentMapSegmentTest.groovy +++ /dev/null @@ -1,193 +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 - -import org.junit.Before -import org.junit.Test - -class AbstractConcurrentMapSegmentTest { - private static final Integer INITIAL_SEGMENT_SIZE = 100 - private static final Integer SEGMENT_THRESHOLD = 0.75f * INITIAL_SEGMENT_SIZE - - // Incrementing counter used to generate unique key names for TestEntry objects - // across all test methods in this class - private static int keyId - - TestSegment segment - List<TestEntry> entries = [] - int rehashCount = 0 - - @Before - public void setUp() throws Exception { - segment = new TestSegment(INITIAL_SEGMENT_SIZE) - } - - @Test - public void testSegmentWillNotRehash() { - whenIAddElements(50) - thenRehashHappenedTimes(0) - thenSegmentExpands(false) - } - - @Test - public void testSegmentWillNotRehashEdgeCase() { - whenIAddElements(SEGMENT_THRESHOLD + 1) - thenRehashHappenedTimes(0) - thenSegmentExpands(false) - } - - @Test - public void testSegmentWillRehashAndExpand() { - whenIAddElements(SEGMENT_THRESHOLD + 2) - thenRehashHappenedTimes(1) - thenSegmentExpands(true) - } - - @Test - public void testSegmentWillRehashAndExpandManyTimes() { - int elementCount = (SEGMENT_THRESHOLD + 1 ) * 6 - whenIAddElements(elementCount) - //456 elements fit into segment of size 800, which is 100 * 2 * 2 * 2 - thenSegmentSizeIs(INITIAL_SEGMENT_SIZE * 2 * 2 * 2) - thenRehashHappenedTimes(3) - } - - @Test - public void testSegmentWillRehashWithNoExpansion() { - whenIAddElements(SEGMENT_THRESHOLD) - whenISetElementsAsInvalid(50) - whenIAddElements(50) - thenRehashHappenedTimes(1) - thenSegmentExpands(false) - } - - @Test - public void testSegmentWillRehashAndEventuallyExpand() { - whenIAddElements(SEGMENT_THRESHOLD) - - // 1-st rehash - whenISetElementsAsInvalid(50) - whenIAddElements(50) - thenSegmentExpands(false) - - // 2-nd rehash - whenISetElementsAsInvalid(30) - whenIAddElements(30) - thenSegmentExpands(false) - - // 3-nd rehash - whenISetElementsAsInvalid(20) - whenIAddElements(20) - thenSegmentExpands(false) - - // 4-th rehash with none invalid => expansion: segment * 2 - whenIAddElements(SEGMENT_THRESHOLD) - - thenRehashHappenedTimes(4) - thenSegmentSizeIs(INITIAL_SEGMENT_SIZE * 2) - } - - private void whenIAddElements(int count) { - count.times { - String key = "k:${++keyId}-${it}" - segment.put(key, key.hashCode(), "v${it}") - } - } - - private void whenISetElementsAsInvalid(int count) { - List<TestEntry> validEntries = entries.findAll { it.isValid() } - count.times { - validEntries.get(it).setValid(false) - } - } - - private void thenRehashHappenedTimes(int expectedRehashCount) { - assert rehashCount == expectedRehashCount - } - - private void thenSegmentSizeIs(int expectedSize) { - assert segment.table.length == expectedSize - } - - private void thenSegmentExpands(boolean truth) { - assert segment.table.length > INITIAL_SEGMENT_SIZE == truth - } - - class TestSegment extends org.codehaus.groovy.util.AbstractConcurrentMap.Segment { - - protected TestSegment(int initialCapacity) { - super(initialCapacity) - } - - @Override - protected org.codehaus.groovy.util.AbstractConcurrentMap.Entry createEntry(Object key, int hash, Object value) { - TestEntry entry = new TestEntry(key, hash, value) - entries.add(entry) - return entry - } - - @Override - void rehash() { - rehashCount++ - super.rehash() - } - } -} - -class TestEntry implements org.codehaus.groovy.util.AbstractConcurrentMap.Entry { - Object key - Object value - int hash - boolean valid = true; - - public TestEntry(Object key, int hash, Object value) { - this.key = key - this.hash = hash - this.value = value - } - - @Override - boolean isEqual(Object key, int hash) { - return hash == this.hash && key.equals(this.key) - } - - @Override - Object getValue() { - return value - } - - @Override - void setValue(Object value) { - this.value = value - } - - @Override - int getHash() { - return hash - } - - @Override - boolean isValid() { - return valid - } - - public void setValid(boolean valid) { - this.valid = valid - } -} diff --git a/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy b/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy deleted file mode 100644 index d87704f8d0..0000000000 --- a/src/test/org/codehaus/groovy/util/ManagedConcurrentMapTest.groovy +++ /dev/null @@ -1,43 +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 - -import groovy.test.GroovyTestCase - -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 - } - -} diff --git a/src/test/org/codehaus/groovy/util/ManagedLinkedlistTest.groovy b/src/test/org/codehaus/groovy/util/ManagedLinkedlistTest.groovy deleted file mode 100644 index 73f1dfa509..0000000000 --- a/src/test/org/codehaus/groovy/util/ManagedLinkedlistTest.groovy +++ /dev/null @@ -1,71 +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 - -import groovy.test.GroovyTestCase - -class ManagedLinkedListTest extends GroovyTestCase { - - def list - - void setUp() { - def manager = ReferenceManager.createIdlingManager(null) - def bundle = new ReferenceBundle(manager, ReferenceType.HARD) - list = new ManagedLinkedList(bundle) - } - - void testElementAdd() { - list.add(1) - def i = 0 - list.each { - assert it==1 - i++ - } - assert i ==1 - } - - void testEmptylist() { - assert list.isEmpty() - } - - void testRemoveinTheMiddle() { - list.add(1) - list.add(2) - list.add(3) - list.add(4) - list.add(5) - def iter = list.iterator() - while (iter.hasNext()) { - if (iter.next()==3) iter.remove() - } - def val = list.inject(0){value, it-> value+it} - assert val == 12 - } - - void testAddRemove() { - 10.times { - list.add(it) - def iter = list.iterator() - while (iter.hasNext()) { - if (iter.next()==it) iter.remove() - } - } - assert list.isEmpty() - } -} \ No newline at end of file
