From 03b9741af9d3088afe6833553be54b14469922fc Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 23 Oct 2010 06:26:34 +0200 Subject: ArrayHashSet: Add 'getOrAdd(key)' identity method to conveniently get the identity or add it, if not exist yet --- src/java/com/jogamp/common/util/ArrayHashSet.java | 33 ++++++++++++++++++---- .../com/jogamp/common/util/TestArrayHashSet01.java | 13 +++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/java/com/jogamp/common/util/ArrayHashSet.java b/src/java/com/jogamp/common/util/ArrayHashSet.java index af1c0bd..d1e555f 100644 --- a/src/java/com/jogamp/common/util/ArrayHashSet.java +++ b/src/java/com/jogamp/common/util/ArrayHashSet.java @@ -358,15 +358,38 @@ public class ArrayHashSet // /** - * Identity method allowing to fetch an equivalent object, using the internal hash map. + * Identity method allowing to get the identical object, using the internal hash map. *
* This is an O(1) operation. * - * @param ident argument to find an equivalent Object within this list - * @return the Object contained in this list equivalent to the given ident Object + * @param key hash source to find the identical Object within this list + * @return object from this list, identical to the given key hash code, + * or null if not contained */ - public final Object get(Object ident) { - return map.get(ident); + public final Object get(Object key) { + return map.get(key); + } + + /** + * Identity method allowing to get the identical object, using the internal hash map.
+ * If the key is not yet contained, add it. + *
+ * This is an O(1) operation. + * + * @param key hash source to find the identical Object within this list + * @return object from this list, identical to the given key hash code, + * or add the given key and return it. + */ + public final Object getOrAdd(Object key) { + Object 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); + } + identity = key; + } + return identity; } } diff --git a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java index 5486671..2946959 100644 --- a/src/junit/com/jogamp/common/util/TestArrayHashSet01.java +++ b/src/junit/com/jogamp/common/util/TestArrayHashSet01.java @@ -107,6 +107,19 @@ 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 + 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 + Assert.assertNotNull(q); + 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 } public static void main(String args[]) throws IOException { -- cgit v1.2.3