diff options
-rw-r--r-- | src/java/com/jogamp/common/util/ArrayHashSet.java | 71 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestArrayHashSet01.java | 16 |
2 files changed, 40 insertions, 47 deletions
diff --git a/src/java/com/jogamp/common/util/ArrayHashSet.java b/src/java/com/jogamp/common/util/ArrayHashSet.java index 8266b50..5d6d3c8 100644 --- a/src/java/com/jogamp/common/util/ArrayHashSet.java +++ b/src/java/com/jogamp/common/util/ArrayHashSet.java @@ -34,8 +34,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Hashed ArrayList implementation of the List and Collection interface. @@ -47,7 +45,7 @@ import java.util.logging.Logger; * ie {@link java.util.List#indexOf(java.lang.Object)} * and {@link java.util.List#get(int)}, hence object identity can be implemented.</li> * <li> Object identity via {@link #get(java.lang.Object)}</li> - * <li> Java 1.3 compatible</li> + * <li> Java 1.5 compatible</li> * </ul> * * O(1) operations: @@ -67,12 +65,11 @@ import java.util.logging.Logger; * {@link com.jogamp.common.util.locks.RecursiveLock}. * */ - -public class ArrayHashSet - implements Cloneable, Collection, List +public class ArrayHashSet<E> + implements Cloneable, Collection<E>, List<E> { - HashMap map = new HashMap(); // object -> object - ArrayList data = new ArrayList(); // list of objects + HashMap<E,E> map = new HashMap<E,E>(); // object -> object + ArrayList<E> data = new ArrayList<E>(); // list of objects public ArrayHashSet() { clear(); @@ -86,9 +83,9 @@ public class ArrayHashSet * @return a shallow copy of this ArrayHashSet, elements are not copied. */ public final Object clone() { - ArrayList clonedList = (ArrayList)data.clone(); + ArrayList<E> clonedList = (ArrayList<E>)data.clone(); - ArrayHashSet newObj = new ArrayHashSet(); + ArrayHashSet<E> newObj = new ArrayHashSet<E>(); newObj.addAll(clonedList); return newObj; @@ -111,7 +108,7 @@ public class ArrayHashSet * @return true if the element was added to this list, * otherwise false (already contained). */ - public final boolean add(Object element) { + public final boolean add(E element) { boolean exists = map.containsKey(element); if(!exists) { if(null != map.put(element, element)) { @@ -151,9 +148,9 @@ public class ArrayHashSet * @return true if at least one element was added to this list, * otherwise false (completely container). */ - public final boolean addAll(Collection c) { + public final boolean addAll(Collection<? extends E> c) { boolean mod = false; - for (Iterator iter = c.iterator(); iter.hasNext(); ) { + for (Iterator<? extends E> iter = c.iterator(); iter.hasNext(); ) { mod = mod || add(iter.next()) ; } return mod; @@ -179,8 +176,8 @@ public class ArrayHashSet * @return true if the given Collection is completly contained by this list using hash map, * otherwise false. */ - public final boolean containsAll(Collection c) { - for (Iterator iter = c.iterator(); iter.hasNext(); ) { + public final boolean containsAll(Collection<?> c) { + for (Iterator<?> iter = c.iterator(); iter.hasNext(); ) { if (! this.contains(iter.next()) ) { return false; } @@ -196,9 +193,9 @@ public class ArrayHashSet * @return true if at least one element of this list was removed, * otherwise false. */ - public final boolean removeAll(Collection c) { + public final boolean removeAll(Collection<?> c) { boolean mod = false; - for (Iterator iter = c.iterator(); iter.hasNext(); ) { + for (Iterator<?> iter = c.iterator(); iter.hasNext(); ) { mod = this.remove(iter.next()) || mod; } return mod; @@ -213,9 +210,9 @@ public class ArrayHashSet * @return true if at least one element of this list was removed, * otherwise false. */ - public final boolean retainAll(Collection c) { + public final boolean retainAll(Collection<?> c) { boolean mod = false; - for (Iterator iter = this.iterator(); iter.hasNext(); ) { + for (Iterator<?> iter = this.iterator(); iter.hasNext(); ) { Object o = iter.next(); if (! c.contains(o) ) { mod = this.remove(o) || mod; @@ -234,7 +231,7 @@ public class ArrayHashSet if ( !(arrayHashSet instanceof ArrayHashSet) ) { return false; } - return data.equals(((ArrayHashSet)arrayHashSet).data); + return data.equals(((ArrayHashSet<?>)arrayHashSet).data); } /** @@ -251,7 +248,7 @@ public class ArrayHashSet return data.isEmpty(); } - public final Iterator iterator() { + public final Iterator<E> iterator() { return data.iterator(); } @@ -263,7 +260,7 @@ public class ArrayHashSet return data.toArray(); } - public final Object[] toArray(Object[] a) { + public final <T> T[] toArray(T[] a) { return data.toArray(a); } @@ -271,7 +268,7 @@ public class ArrayHashSet // List // - public final Object get(int index) { + public final E get(int index) { return data.get(index); } @@ -286,7 +283,7 @@ public class ArrayHashSet * * @throws IllegalArgumentException if the given element was already contained */ - public final void add(int index, Object element) { + public final void add(int index, E element) { if ( map.containsKey(element) ) { throw new IllegalArgumentException("Element "+element+" is already contained"); } @@ -299,15 +296,15 @@ public class ArrayHashSet /** * @throws UnsupportedOperationException */ - public final boolean addAll(int index, Collection c) { + public final boolean addAll(int index, Collection<? extends E> c) { throw new UnsupportedOperationException("Not supported yet."); } /** * @throws UnsupportedOperationException */ - public final Object set(int index, Object element) { - Object old = remove(index); + public final E set(int index, E element) { + E old = remove(index); if(null!=old) { add(index, element); } @@ -321,8 +318,8 @@ public class ArrayHashSet * * @return the removed object */ - public final Object remove(int index) { - Object o = get(index); + public final E remove(int index) { + E o = get(index); if( null!=o && remove(o) ) { return o; } @@ -340,15 +337,15 @@ public class ArrayHashSet return indexOf(o); } - public final ListIterator listIterator() { + public final ListIterator<E> listIterator() { return data.listIterator(); } - public final ListIterator listIterator(int index) { + public final ListIterator<E> listIterator(int index) { return data.listIterator(index); } - public final List subList(int fromIndex, int toIndex) { + public final List<E> subList(int fromIndex, int toIndex) { return data.subList(fromIndex, toIndex); } @@ -359,8 +356,8 @@ public class ArrayHashSet /** * @return a shallow copy of this ArrayHashSet's ArrayList, elements are not copied. */ - public final ArrayList toArrayList() { - return (ArrayList) data.clone(); + public final ArrayList<E> toArrayList() { + return (ArrayList<E>) data.clone(); } /** @@ -372,7 +369,7 @@ public class ArrayHashSet * @return object from this list, identical to the given <code>key</code> hash code, * or null if not contained */ - public final Object get(Object key) { + public final E get(Object key) { return map.get(key); } @@ -386,8 +383,8 @@ public class ArrayHashSet * @return object from this list, identical to the given <code>key</code> hash code, * or add the given <code>key</code> and return it. */ - public final Object getOrAdd(Object key) { - Object identity = get(key); + public final E getOrAdd(E key) { + E identity = get(key); if(null == identity) { // object not contained yet, add it if(!this.add(key)) { diff --git a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java index 2946959..b16cc74 100644 --- a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java +++ b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java @@ -32,9 +32,6 @@ import java.util.*; import java.io.IOException; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.AfterClass; import org.junit.Test; public class TestArrayHashSet01 { @@ -71,7 +68,7 @@ public class TestArrayHashSet01 { } } - public void populate(List l, int start, int len, int i2, int i3, int expectedPlusSize) { + public void populate(List<Dummy> l, int start, int len, int i2, int i3, int expectedPlusSize) { int oldSize = l.size(); int pos = start+len-1; while(pos>=start) { @@ -82,8 +79,7 @@ public class TestArrayHashSet01 { @Test public void test01ArrayHashSet() { - int sum=0; - ArrayHashSet l = new ArrayHashSet(); + ArrayHashSet<Dummy> l = new ArrayHashSet<Dummy>(); populate(l, 10, 100, 22, 34, 100); // [10 .. 109] populate(l, 10, 100, 22, 34, 0); // [10 .. 109] populate(l, 6, 5, 22, 34, 4); // [ 6 .. 9], 10 already exists @@ -92,14 +88,14 @@ public class TestArrayHashSet01 { // slow get on position .. int i = l.indexOf(p6_22_34); - Dummy q = (Dummy) l.get(i); + Dummy q = l.get(i); Assert.assertNotNull(q); Assert.assertEquals(p6_22_34, q); Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); Assert.assertTrue(p6_22_34 != q); // diff reference // fast identity .. - q = (Dummy) l.get(p6_22_34); + q = l.get(p6_22_34); Assert.assertNotNull(q); Assert.assertEquals(p6_22_34, q); Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); @@ -108,14 +104,14 @@ public class TestArrayHashSet01 { Assert.assertTrue(!l.add(q)); // add same Assert.assertTrue(!l.add(p6_22_34)); // add equivalent - q = (Dummy) l.getOrAdd(p6_22_34); // not added test + q = l.getOrAdd(p6_22_34); // not added test Assert.assertNotNull(q); Assert.assertEquals(p6_22_34, q); Assert.assertTrue(p6_22_34.hashCode() == q.hashCode()); Assert.assertTrue(p6_22_34 != q); // diff reference Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one .. - q = (Dummy) l.getOrAdd(p1_2_3); // added test + q = l.getOrAdd(p1_2_3); // added test Assert.assertNotNull(q); Assert.assertEquals(p1_2_3, q); Assert.assertTrue(p1_2_3.hashCode() == q.hashCode()); |