summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2015-04-01 15:17:10 +0200
committerSven Gothel <[email protected]>2015-04-01 15:17:10 +0200
commitc156343fec33ceea7f238b9766a9f4985fb92687 (patch)
tree1fd405c3cceaf18fe1c840aa3c2462324e7df6fa /src
parentab684608247912ace165923f36ea443fda0e838a (diff)
Add ArrayHashMap; Use 'supportNullValue' optimizing ArrayHashSet and ArrayHashMap; Unify ctor for both impl.
Add/Enhance unit tests for both.
Diffstat (limited to 'src')
-rw-r--r--src/java/com/jogamp/common/util/ArrayHashMap.java305
-rw-r--r--src/java/com/jogamp/common/util/ArrayHashSet.java206
-rw-r--r--src/junit/com/jogamp/common/util/TestArrayHashMap01.java186
-rw-r--r--src/junit/com/jogamp/common/util/TestArrayHashSet01.java123
4 files changed, 758 insertions, 62 deletions
diff --git a/src/java/com/jogamp/common/util/ArrayHashMap.java b/src/java/com/jogamp/common/util/ArrayHashMap.java
new file mode 100644
index 0000000..35a484b
--- /dev/null
+++ b/src/java/com/jogamp/common/util/ArrayHashMap.java
@@ -0,0 +1,305 @@
+/**
+ * Copyright 2015 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.common.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * {@link HashMap} implementation backed by an {@link ArrayList} to preserve order of values.
+ *
+ * Implementation properties are:
+ * <ul>
+ * <li> Unique elements utilizing {@link java.lang.Object#hashCode()} for O(1) operations, see below.</li>
+ * <li> Java 1.5 compatible</li>
+ * </ul>
+ *
+ * O(1) operations:
+ * <ul>
+ * <li> put new key-value-pair(s) </li>
+ * <li> test for containment </li>
+ * <li> trying to remove non existent elements </li>
+ * </ul>
+ *
+ * O(n) operations:
+ * <ul>
+ * <li> put existing key-value-pair(s) </li>
+ * <li> removing existing elements</li>
+ * </ul>
+ *
+ * For thread safety, the application shall decorate access to instances via
+ * {@link com.jogamp.common.util.locks.RecursiveLock}.
+ *
+*/
+public class ArrayHashMap<K, V>
+ implements Cloneable, Map<K, V>
+{
+ /**
+ * Default load factor: {@value}
+ */
+ public static final float DEFAULT_LOAD_FACTOR = 0.75f;
+ /**
+ * The default initial capacity: {@value}
+ */
+ public static final int DEFAULT_INITIAL_CAPACITY = 16;
+
+ private final HashMap<K,V> map; // key -> object
+ private final ArrayList<V> data; // list of objects
+ private final boolean supportNullValue;
+
+ /**
+ *
+ * @param supportNullValue Use {@code true} for default behavior, i.e. {@code null} can be a valid value.
+ * Use {@code false} if {@code null} is not a valid value,
+ * here {@link #put(Object, Object)} and {@link #remove(Object)} will be optimized.
+ * @param initialCapacity use {@link #DEFAULT_INITIAL_CAPACITY} for default
+ * @param loadFactor use {@link #DEFAULT_LOAD_FACTOR} for default
+ * @see #supportsNullValue()
+ */
+ public ArrayHashMap(final boolean supportNullValue, final int initialCapacity, final float loadFactor) {
+ this.map = new HashMap<K,V>(initialCapacity, loadFactor);
+ this.data = new ArrayList<V>(initialCapacity);
+ this.supportNullValue = supportNullValue;
+ }
+
+ /**
+ * @return a shallow copy of this ArrayHashMap, elements are not copied.
+ */
+ public ArrayHashMap(final ArrayHashMap<K, V> o) {
+ map = new HashMap<K, V>(o.map);
+ data = new ArrayList<V>(o.data);
+ supportNullValue = o.supportNullValue;
+ }
+
+ /**
+ * Returns {@code true} for default behavior, i.e. {@code null} can be a valid value.
+ * <p>
+ * Returns {@code false} if {@code null} is not a valid value,
+ * here {@link #put(Object, Object)} and {@link #remove(Object)} are optimized operations.
+ * </p>
+ * @see #ArrayHashMap(boolean, int, float)
+ */
+ public final boolean supportsNullValue() { return supportNullValue; }
+
+ //
+ // Cloneable
+ //
+
+ /**
+ * Implementation uses {@link #ArrayHashMap(ArrayHashMap)}.
+ * @return a shallow copy of this ArrayHashMap, elements are not copied.
+ */
+ @Override
+ public final Object clone() {
+ return new ArrayHashMap<K, V>(this);
+ }
+
+ /**
+ * Returns this object ordered ArrayList. Use w/ care, it's not a copy.
+ * @see #toArrayList()
+ */
+ public final ArrayList<V> getData() { return data; }
+
+ /**
+ * @return a shallow copy of this ArrayHashMap's ArrayList, elements are not copied.
+ * @see #getData()
+ */
+ public final ArrayList<V> toArrayList() {
+ return new ArrayList<V>(data);
+ }
+
+ /** Returns this object hash map. Use w/ care, it's not a copy. */
+ public final HashMap<K,V> getMap() { return map; }
+
+ @Override
+ public final String toString() { return data.toString(); }
+
+ //
+ // Map
+ //
+
+ @Override
+ public final void clear() {
+ data.clear();
+ map.clear();
+ }
+
+ @Override
+ public Set<K> keySet() {
+ return map.keySet();
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * See {@link #getData()} and {@link #toArrayList()}.
+ * </p>
+ * @see #getData()
+ * @see #toArrayList()
+ */
+ @Override
+ public Collection<V> values() {
+ return map.values();
+ }
+
+ @Override
+ public Set<java.util.Map.Entry<K, V>> entrySet() {
+ return map.entrySet();
+ }
+
+ @Override
+ public final V get(final Object key) {
+ return map.get(key);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * This is an O(1) operation, in case the key does not exist,
+ * otherwise O(n).
+ * </p>
+ * @throws NullPointerException if {@code value} is {@code null} but {@link #supportsNullValue()} == {@code false}
+ */
+ @Override
+ public final V put(final K key, final V value) throws NullPointerException {
+ final V oldValue;
+ if( supportNullValue ) {
+ // slow path
+ final boolean exists = map.containsKey(key);
+ if(!exists) {
+ // !exists
+ if( null != ( oldValue = map.put(key, value) ) ) {
+ // slips a valid null ..
+ throw new InternalError("Already existing, but checked before: "+key+" -> "+oldValue);
+ }
+ } else {
+ // exists
+ oldValue = map.put(key, value);
+ if( !data.remove(oldValue) ) {
+ throw new InternalError("Already existing, but not in list: "+oldValue);
+ }
+ }
+ } else {
+ checkNullValue(value);
+ // fast path
+ if( null != ( oldValue = map.put(key, value) ) ) {
+ // exists
+ if( !data.remove(oldValue) ) {
+ throw new InternalError("Already existing, but not in list: "+oldValue);
+ }
+ }
+ }
+ if(!data.add(value)) {
+ throw new InternalError("Couldn't add value to list: "+value);
+ }
+ return oldValue;
+ }
+
+ @Override
+ public void putAll(final Map<? extends K, ? extends V> m) {
+ for (final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
+ final Map.Entry<? extends K, ? extends V> e = i.next();
+ put(e.getKey(), e.getValue());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * This is an O(1) operation, in case the key does not exist,
+ * otherwise O(n).
+ * </p>
+ */
+ @Override
+ public final V remove(final Object key) {
+ if( supportNullValue ) {
+ if( map.containsKey(key) ) {
+ // exists
+ final V oldValue = map.remove(key);
+ if ( !data.remove(oldValue) ) {
+ throw new InternalError("Couldn't remove prev mapped pair: "+key+" -> "+oldValue);
+ }
+ return oldValue;
+ }
+ } else {
+ final V oldValue;
+ if ( null != (oldValue = map.remove(key) ) ) {
+ // exists
+ if ( !data.remove(oldValue) ) {
+ throw new InternalError("Couldn't remove prev mapped pair: "+key+" -> "+oldValue);
+ }
+ }
+ return oldValue;
+ }
+ return null;
+ }
+
+ @Override
+ public final boolean containsKey(final Object key) {
+ return map.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(final Object value) {
+ return map.containsValue(value);
+ }
+
+ @Override
+ public final boolean equals(final Object arrayHashMap) {
+ if ( !(arrayHashMap instanceof ArrayHashMap) ) {
+ return false;
+ }
+ return map.equals(((ArrayHashMap<?,?>)arrayHashMap).map);
+ }
+
+ @Override
+ public final int hashCode() {
+ return map.hashCode();
+ }
+
+ @Override
+ public final boolean isEmpty() {
+ return data.isEmpty();
+ }
+
+ @Override
+ public final int size() {
+ return data.size();
+ }
+
+ private static final void checkNullValue(final Object value) throws NullPointerException {
+ if( null == value ) {
+ throw new NullPointerException("Null value not supported");
+ }
+ }
+}
diff --git a/src/java/com/jogamp/common/util/ArrayHashSet.java b/src/java/com/jogamp/common/util/ArrayHashSet.java
index 34e84c4..8f61a8a 100644
--- a/src/java/com/jogamp/common/util/ArrayHashSet.java
+++ b/src/java/com/jogamp/common/util/ArrayHashSet.java
@@ -68,24 +68,77 @@ import java.util.ListIterator;
public class ArrayHashSet<E>
implements Cloneable, Collection<E>, List<E>
{
+ /**
+ * Default load factor: {@value}
+ */
+ public static final float DEFAULT_LOAD_FACTOR = 0.75f;
+ /**
+ * The default initial capacity: {@value}
+ */
+ public static final int DEFAULT_INITIAL_CAPACITY = 16;
+
private final HashMap<E,E> map; // object -> object
private final ArrayList<E> data; // list of objects
+ private final boolean supportNullValue;
+ /**
+ * @deprecated Use {@link #ArrayHashSet(boolean, int, float)}
+ */
public ArrayHashSet() {
- map = new HashMap<E,E>();
- data = new ArrayList<E>();
+ this(true, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
+ /**
+ * @param initialCapacity
+ * @deprecated Use {@link #ArrayHashSet(boolean, int, float)}
+ */
public ArrayHashSet(final int initialCapacity) {
- map = new HashMap<E,E>(initialCapacity);
- data = new ArrayList<E>(initialCapacity);
+ this(true, initialCapacity, DEFAULT_LOAD_FACTOR);
}
+ /**
+ * @param initialCapacity
+ * @param loadFactor
+ * @deprecated Use {@link #ArrayHashSet(boolean, int, float)}
+ */
public ArrayHashSet(final int initialCapacity, final float loadFactor) {
- map = new HashMap<E,E>(initialCapacity, loadFactor);
- data = new ArrayList<E>(initialCapacity);
+ this(true, initialCapacity, loadFactor);
}
+ /**
+ *
+ * @param supportNullValue Use {@code true} for default behavior, i.e. {@code null} can be a valid value.
+ * Use {@code false} if {@code null} is not a valid value,
+ * here {@link #remove(E)} and {@link #getOrAdd(Object)} will be optimized.
+ * @param initialCapacity use {@link #DEFAULT_INITIAL_CAPACITY} for default
+ * @param loadFactor use {@link #DEFAULT_LOAD_FACTOR} for default
+ * @see #supportsNullValue()
+ */
+ public ArrayHashSet(final boolean supportNullValue, final int initialCapacity, final float loadFactor) {
+ this.map = new HashMap<E,E>(initialCapacity, loadFactor);
+ this.data = new ArrayList<E>(initialCapacity);
+ this.supportNullValue = supportNullValue;
+ }
+
+ /**
+ * @return a shallow copy of this ArrayHashSet, elements are not copied.
+ */
+ public ArrayHashSet(final ArrayHashSet<E> o) {
+ map = new HashMap<E, E>(o.map);
+ data = new ArrayList<E>(o.data);
+ supportNullValue = o.supportNullValue;
+ }
+
+ /**
+ * Returns {@code true} for default behavior, i.e. {@code null} can be a valid value.
+ * <p>
+ * Returns {@code false} if {@code null} is not a valid value,
+ * here {@link #remove(E)} and {@link #getOrAdd(Object)} are optimized operations.
+ * </p>
+ * @see #ArrayHashSet(boolean, int, float)
+ */
+ public final boolean supportsNullValue() { return supportNullValue; }
+
//
// Cloneable
//
@@ -95,12 +148,7 @@ public class ArrayHashSet<E>
*/
@Override
public final Object clone() {
- final ArrayList<E> clonedList = new ArrayList<E>(data);
-
- final ArrayHashSet<E> newObj = new ArrayHashSet<E>();
- newObj.addAll(clonedList);
-
- return newObj;
+ return new ArrayHashSet<E>(this);
}
/** Returns this object ordered ArrayList. Use w/ care, it's not a copy. */
@@ -125,40 +173,66 @@ public class ArrayHashSet<E>
* Add element at the end of this list, if it is not contained yet.
* <br>
* This is an O(1) operation
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if the element was added to this list,
* otherwise false (already contained).
+ * @throws NullPointerException if {@code element} is {@code null} but {@link #supportsNullValue()} == {@code false}
*/
@Override
- public final boolean add(final E element) {
- final boolean exists = map.containsKey(element);
- if(!exists) {
+ public final boolean add(final E element) throws NullPointerException {
+ if( !supportNullValue ) {
+ checkNull(element);
+ }
+ if( !map.containsKey(element) ) {
+ // !exists
if(null != map.put(element, element)) {
+ // slips a valid null ..
throw new InternalError("Already existing, but checked before: "+element);
}
if(!data.add(element)) {
throw new InternalError("Couldn't add element: "+element);
}
+ return true;
}
- return !exists;
+ return false;
}
/**
* Remove element from this list.
* <br>
- * This is an O(1) operation, in case it does not exist,
+ * This is an O(1) operation, in case the element does not exist,
* otherwise O(n).
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if the element was removed from this list,
* otherwise false (not contained).
+ * @throws NullPointerException if {@code element} is {@code null} but {@link #supportsNullValue()} == {@code false}
*/
@Override
- public final boolean remove(final Object element) {
- if ( null != map.remove(element) ) {
- if ( ! data.remove(element) ) {
- throw new InternalError("Couldn't remove prev mapped element: "+element);
+ public final boolean remove(final Object element) throws NullPointerException {
+ if( supportNullValue ) {
+ if( map.containsKey(element) ) {
+ // exists
+ map.remove(element);
+ if ( !data.remove(element) ) {
+ throw new InternalError("Couldn't remove prev mapped element: "+element);
+ }
+ return true;
+ }
+ } else {
+ checkNull(element);
+ if ( null != map.remove(element) ) {
+ // exists
+ if ( !data.remove(element) ) {
+ throw new InternalError("Couldn't remove prev mapped element: "+element);
+ }
+ return true;
}
- return true;
}
return false;
}
@@ -167,6 +241,9 @@ public class ArrayHashSet<E>
* Add all elements of given {@link java.util.Collection} at the end of this list.
* <br>
* This is an O(n) operation, over the given Collection size.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if at least one element was added to this list,
* otherwise false (completely container).
@@ -184,6 +261,9 @@ public class ArrayHashSet<E>
* Test for containment
* <br>
* This is an O(1) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if the given element is contained by this list using fast hash map,
* otherwise false.
@@ -197,6 +277,9 @@ public class ArrayHashSet<E>
* Test for containment of given {@link java.util.Collection}
* <br>
* This is an O(n) operation, over the given Collection size.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if the given Collection is completly contained by this list using hash map,
* otherwise false.
@@ -215,6 +298,9 @@ public class ArrayHashSet<E>
* Remove all elements of given {@link java.util.Collection} from this list.
* <br>
* This is an O(n) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if at least one element of this list was removed,
* otherwise false.
@@ -233,6 +319,9 @@ public class ArrayHashSet<E>
* remove all elements not contained by the given {@link java.util.Collection} c.
* <br>
* This is an O(n) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if at least one element of this list was removed,
* otherwise false.
@@ -250,6 +339,9 @@ public class ArrayHashSet<E>
/**
* This is an O(n) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return true if arrayHashSet is of type ArrayHashSet and all entries are equal
* Performance: arrayHashSet(1)
@@ -264,6 +356,9 @@ public class ArrayHashSet<E>
/**
* This is an O(n) operation over the size of this list.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return the hash code of this list as define in {@link java.util.List#hashCode()},
* ie hashing all elements of this list.
@@ -316,30 +411,44 @@ public class ArrayHashSet<E>
* Add element at the given index in this list, if it is not contained yet.
* <br>
* This is an O(1) operation
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @throws IllegalArgumentException if the given element was already contained
+ * @throws NullPointerException if {@code element} is {@code null} but {@link #supportsNullValue()} == {@code false}
*/
@Override
- public final void add(final int index, final E element) {
+ public final void add(final int index, final E element) throws IllegalArgumentException, NullPointerException {
+ if( !supportNullValue ) {
+ checkNull(element);
+ }
if ( map.containsKey(element) ) {
throw new IllegalArgumentException("Element "+element+" is already contained");
}
if(null != map.put(element, element)) {
+ // slips a valid null ..
throw new InternalError("Already existing, but checked before: "+element);
}
+ // !exists
data.add(index, element);
}
/**
+ * <p>
+ * {@inheritDoc}
+ * </p>
* @throws UnsupportedOperationException
*/
@Override
- public final boolean addAll(final int index, final Collection<? extends E> c) {
+ public final boolean addAll(final int index, final Collection<? extends E> c) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Not supported yet.");
}
/**
- * @throws UnsupportedOperationException
+ * <p>
+ * {@inheritDoc}
+ * </p>
*/
@Override
public final E set(final int index, final E element) {
@@ -354,6 +463,9 @@ public class ArrayHashSet<E>
* Remove element at given index from this list.
* <br>
* This is an O(n) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return the removed object
*/
@@ -370,6 +482,9 @@ public class ArrayHashSet<E>
* Since this list is unique, equivalent to {@link #indexOf(java.lang.Object)}.
* <br>
* This is an O(n) operation.
+ * <p>
+ * {@inheritDoc}
+ * </p>
*
* @return index of element, or -1 if not found
*/
@@ -409,34 +524,44 @@ public class ArrayHashSet<E>
* <br>
* This is an O(1) operation.
*
- * @param key hash source to find the identical Object within this list
+ * @param element hash source to find the identical Object within this list
* @return object from this list, identical to the given <code>key</code> hash code,
* or null if not contained
*/
- public final E get(final Object key) {
- return map.get(key);
+ public final E get(final Object element) {
+ return map.get(element);
}
/**
* Identity method allowing to get the identical object, using the internal hash map.<br>
- * If the <code>key</code> is not yet contained, add it.
+ * If the <code>element</code> is not yet contained, add it.
* <br>
* This is an O(1) operation.
*
- * @param key hash source to find the identical Object within this list
+ * @param element hash source to find the identical Object within this list
* @return object from this list, identical to the given <code>key</code> hash code,
* or add the given <code>key</code> and return it.
+ * @throws NullPointerException if {@code element} is {@code null} but {@link #supportsNullValue()} == {@code false}
*/
- public final E getOrAdd(final E key) {
- final E identity = get(key);
- if(null == identity) {
- // object not contained yet, add it
- if(!this.add(key)) {
- throw new InternalError("Key not mapped, but contained in list: "+key);
+ public final E getOrAdd(final E element) throws NullPointerException {
+ if( supportNullValue ) {
+ if( map.containsKey(element) ) {
+ // existent
+ return map.get(element);
}
- return key;
+ } else {
+ checkNull(element);
+ final E identity = map.get(element);
+ if(null != identity) {
+ // existent
+ return identity;
+ }
+ }
+ // !existent
+ if(!this.add(element)) {
+ throw new InternalError("Element not mapped, but contained in list: "+element);
}
- return identity;
+ return element;
}
/**
@@ -455,4 +580,9 @@ public class ArrayHashSet<E>
return data.contains(element);
}
+ private static final void checkNull(final Object element) throws NullPointerException {
+ if( null == element ) {
+ throw new NullPointerException("Null element not supported");
+ }
+ }
}
diff --git a/src/junit/com/jogamp/common/util/TestArrayHashMap01.java b/src/junit/com/jogamp/common/util/TestArrayHashMap01.java
new file mode 100644
index 0000000..529612c
--- /dev/null
+++ b/src/junit/com/jogamp/common/util/TestArrayHashMap01.java
@@ -0,0 +1,186 @@
+/**
+ * Copyright 2015 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.common.util;
+
+import java.util.*;
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.jogamp.junit.util.SingletonJunitCase;
+
+import org.junit.FixMethodOrder;
+import org.junit.runners.MethodSorters;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestArrayHashMap01 extends SingletonJunitCase {
+
+ public static class Dummy {
+ int i1, i2, i3;
+
+ public Dummy(final int i1, final int i2, final int i3) {
+ this.i1 = i1;
+ this.i2 = i2;
+ this.i3 = i3;
+ }
+
+ public boolean equals(final Object o) {
+ if(o instanceof Dummy) {
+ final Dummy d = (Dummy)o;
+ return this.i1 == d.i1 &&
+ this.i2 == d.i2 &&
+ this.i3 == d.i3 ;
+ }
+ return false;
+ }
+
+ public final int hashCode() {
+ // 31 * x == (x << 5) - x
+ int hash = 31 + i1;
+ hash = ((hash << 5) - hash) + i2;
+ hash = ((hash << 5) - hash) + i3;
+ return hash;
+ }
+
+ public String toString() {
+ return "Dummy["+super.toString()+": "+i1+", "+i2+", "+i3+"]";
+ }
+ }
+
+ void populate(final Map<Integer, Dummy> l, final int start, final int len,
+ final int i2, final int i3, final int expectedPlusSize) {
+ final int oldSize = l.size();
+ for(int pos = start+len-1; pos>=start; pos--) {
+ l.put(pos, new Dummy(pos, i2, i3));
+ }
+ Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
+ }
+ boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) {
+ for(int i=0; i<len; i++) {
+ final Dummy d = l.get(startIdx+i);
+ final int i1 = start+len-1-i;
+ if( d.i1 != i1 ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Test
+ public void test01ArrayHashMapWithNullValue() {
+ testArrayHashMapImpl(true);
+ }
+ @Test
+ public void test02ArrayHashSetWithoutNullValue() {
+ testArrayHashMapImpl(false);
+ }
+ void testArrayHashMapImpl(final boolean supportNullValue) {
+ final ArrayHashMap<Integer, Dummy> l =
+ new ArrayHashMap<Integer, Dummy>(supportNullValue,
+ ArrayHashSet.DEFAULT_INITIAL_CAPACITY,
+ ArrayHashSet.DEFAULT_LOAD_FACTOR);
+ Assert.assertEquals(supportNullValue, l.supportsNullValue());
+ final int p7_22_34_key, p7_22_34_idx;
+ final Dummy p7_22_34_orig;
+ final int p6_22_34_key, p6_22_34_idx;
+ final Dummy p6_22_34_orig;
+ {
+ populate(l, 10, 100, 22, 34, 100); // [109 .. 10]
+ Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
+ populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
+ Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
+ populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
+ Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
+ p7_22_34_idx = l.size() - 2;
+ p7_22_34_key = 7;
+ p7_22_34_orig = l.get(p7_22_34_key);
+ p6_22_34_idx = l.size() - 1;
+ p6_22_34_key = 6;
+ p6_22_34_orig = l.get(p6_22_34_key);
+ }
+ Assert.assertNotNull(p7_22_34_orig);
+ Assert.assertEquals(7, p7_22_34_orig.i1);
+ Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
+ Assert.assertNotNull(p6_22_34_orig);
+ Assert.assertEquals(6, p6_22_34_orig.i1);
+ Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
+
+ final Dummy p7_22_34_other = new Dummy(7, 22, 34);
+ Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
+ Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
+ Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
+ final Dummy p6_22_34_other = new Dummy(6, 22, 34);
+ Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
+ Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
+ Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
+
+ // fast identity ..
+ Dummy q = l.get(p6_22_34_key);
+ Assert.assertNotNull(q);
+ Assert.assertEquals(p6_22_34_other, q);
+ Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
+ Assert.assertTrue(p6_22_34_other != q); // diff reference
+ Assert.assertTrue(p6_22_34_orig == q); // same reference
+
+ Assert.assertTrue(l.containsValue(q));
+ Assert.assertTrue(l.containsValue(p6_22_34_other)); // add equivalent
+
+ q = l.put(p6_22_34_key, p6_22_34_other); // override w/ diff hash-obj
+ Assert.assertNotNull(q);
+ Assert.assertEquals(p6_22_34_other, q);
+ Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
+ Assert.assertTrue(p6_22_34_other != q); // diff reference new != old (q)
+ Assert.assertTrue(p6_22_34_orig == q); // same reference orig == old (q)
+ Assert.assertTrue(checkOrder(l.getData(), 0, 10, 100));
+ Assert.assertTrue(checkOrder(l.getData(), 100, 6, 4));
+
+ final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
+ q = l.put(1, p1_2_3); // added test
+ Assert.assertNull(q);
+
+ final Dummy pNull = null;
+ NullPointerException npe = null;
+ try {
+ q = l.put(0, pNull);
+ Assert.assertNull(q);
+ } catch (final NullPointerException _npe) { npe = _npe; }
+ if( l.supportsNullValue() ) {
+ Assert.assertNull(npe);
+ } else {
+ Assert.assertNotNull(npe);
+ }
+ }
+
+ public static void main(final String args[]) throws IOException {
+ final String tstname = TestArrayHashMap01.class.getName();
+ org.junit.runner.JUnitCore.main(tstname);
+ }
+
+}
diff --git a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java
index 51db2c7..3e8fbc0 100644
--- a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java
+++ b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java
@@ -74,47 +74,99 @@ public class TestArrayHashSet01 extends SingletonJunitCase {
}
}
- public void populate(final List<Dummy> l, final int start, final int len, final int i2, final int i3, final int expectedPlusSize) {
+ void populate(final List<Dummy> l, final int start, final int len,
+ final int i2, final int i3, final int expectedPlusSize) {
final int oldSize = l.size();
- int pos = start+len-1;
- while(pos>=start) {
- l.add(new Dummy(pos--, i2, i3));
+ for(int pos = start+len-1; pos>=start; pos--) {
+ l.add(new Dummy(pos, i2, i3));
}
Assert.assertEquals(expectedPlusSize, l.size() - oldSize);
}
+ boolean checkOrder(final List<Dummy> l, final int startIdx, final int start, final int len) {
+ for(int i=0; i<len; i++) {
+ final Dummy d = l.get(startIdx+i);
+ final int i1 = start+len-1-i;
+ if( d.i1 != i1 ) {
+ return false;
+ }
+ }
+ return true;
+ }
@Test
- public void test01ArrayHashSet() {
- final 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
-
- final Dummy p6_22_34 = new Dummy(6, 22, 34);
+ public void test01ArrayHashSetWithNullValue() {
+ testArrayHashSetImpl(true);
+ }
+ @Test
+ public void test02ArrayHashSetWithoutNullValue() {
+ testArrayHashSetImpl(false);
+ }
+ void testArrayHashSetImpl(final boolean supportNullValue) {
+ final ArrayHashSet<Dummy> l =
+ new ArrayHashSet<Dummy>(supportNullValue,
+ ArrayHashSet.DEFAULT_INITIAL_CAPACITY,
+ ArrayHashSet.DEFAULT_LOAD_FACTOR);
+ Assert.assertEquals(supportNullValue, l.supportsNullValue());
+ final int p7_22_34_idx;
+ final Dummy p7_22_34_orig;
+ final int p6_22_34_idx;
+ final Dummy p6_22_34_orig;
+ {
+ populate(l, 10, 100, 22, 34, 100); // [109 .. 10]
+ Assert.assertTrue(checkOrder(l, 0, 10, 100));
+ populate(l, 10, 100, 22, 34, 0); // [109 .. 10]
+ Assert.assertTrue(checkOrder(l, 0, 10, 100));
+ populate(l, 6, 5, 22, 34, 4); // [ 9 .. 6], 10 already exists
+ Assert.assertTrue(checkOrder(l, 100, 6, 4));
+ p7_22_34_idx = l.size() - 2;
+ p7_22_34_orig = l.get(p7_22_34_idx);
+ p6_22_34_idx = l.size() - 1;
+ p6_22_34_orig = l.get(p6_22_34_idx);
+ }
+ Assert.assertNotNull(p7_22_34_orig);
+ Assert.assertEquals(7, p7_22_34_orig.i1);
+ Assert.assertEquals(l.getData().get(p7_22_34_idx), p7_22_34_orig);
+ Assert.assertNotNull(p6_22_34_orig);
+ Assert.assertEquals(6, p6_22_34_orig.i1);
+ Assert.assertEquals(l.getData().get(p6_22_34_idx), p6_22_34_orig);
+
+ final Dummy p7_22_34_other = new Dummy(7, 22, 34);
+ Assert.assertEquals(p7_22_34_other, p7_22_34_orig);
+ Assert.assertTrue(p7_22_34_other.hashCode() == p7_22_34_orig.hashCode());
+ Assert.assertTrue(p7_22_34_other != p7_22_34_orig); // diff reference
+ final Dummy p6_22_34_other = new Dummy(6, 22, 34);
+ Assert.assertEquals(p6_22_34_other, p6_22_34_orig);
+ Assert.assertTrue(p6_22_34_other.hashCode() == p6_22_34_orig.hashCode());
+ Assert.assertTrue(p6_22_34_other != p6_22_34_orig); // diff reference
// slow get on position ..
- final int i = l.indexOf(p6_22_34);
+ final int i = l.indexOf(p6_22_34_other);
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
+ Assert.assertEquals(p6_22_34_other, q);
+ Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
+ Assert.assertTrue(p6_22_34_other != q); // diff reference
+ Assert.assertTrue(p6_22_34_orig == q); // same reference
// fast identity ..
- q = l.get(p6_22_34);
+ q = l.get(p6_22_34_other);
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
+ Assert.assertEquals(p6_22_34_other, q);
+ Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
+ Assert.assertTrue(p6_22_34_other != q); // diff reference
+ Assert.assertTrue(p6_22_34_orig == q); // same reference
Assert.assertTrue(!l.add(q)); // add same
- Assert.assertTrue(!l.add(p6_22_34)); // add equivalent
+ Assert.assertTrue(!l.add(p6_22_34_other)); // add equivalent
- q = l.getOrAdd(p6_22_34); // not added test
+ q = l.getOrAdd(p6_22_34_other); // not added test w/ diff hash-obj
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
+ Assert.assertEquals(p6_22_34_other, q);
+ Assert.assertTrue(p6_22_34_other.hashCode() == q.hashCode());
+ Assert.assertTrue(p6_22_34_other != q); // diff reference
+ Assert.assertTrue(p6_22_34_orig == q); // same reference
+ Assert.assertTrue(checkOrder(l, 0, 10, 100));
+ Assert.assertTrue(checkOrder(l, 100, 6, 4));
final Dummy p1_2_3 = new Dummy(1, 2, 3); // a new one ..
q = l.getOrAdd(p1_2_3); // added test
@@ -122,6 +174,29 @@ public class TestArrayHashSet01 extends SingletonJunitCase {
Assert.assertEquals(p1_2_3, q);
Assert.assertTrue(p1_2_3.hashCode() == q.hashCode());
Assert.assertTrue(p1_2_3 == q); // _same_ reference, since getOrAdd added it
+ Assert.assertTrue(checkOrder(l, 0, 10, 100));
+ Assert.assertTrue(checkOrder(l, 100, 6, 4));
+
+ final Dummy pNull = null;
+ NullPointerException npe = null;
+ try {
+ q = l.getOrAdd(pNull);
+ Assert.assertNull(q);
+ } catch (final NullPointerException _npe) { npe = _npe; }
+ if( l.supportsNullValue() ) {
+ Assert.assertNull(npe);
+ } else {
+ Assert.assertNotNull(npe);
+ }
+
+ try {
+ Assert.assertTrue(l.remove(pNull));
+ } catch (final NullPointerException _npe) { npe = _npe; }
+ if( l.supportsNullValue() ) {
+ Assert.assertNull(npe);
+ } else {
+ Assert.assertNotNull(npe);
+ }
}
public static void main(final String args[]) throws IOException {