diff options
author | Sven Gothel <[email protected]> | 2010-10-23 06:26:34 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-10-23 06:26:34 +0200 |
commit | 03b9741af9d3088afe6833553be54b14469922fc (patch) | |
tree | 795f21fbfa30966ddf5309f8c98749c2d90168fe /src/java/com/jogamp/common/util/ArrayHashSet.java | |
parent | 68d1b38046e1c40bc937ba17ed9aac6e99f608b8 (diff) |
ArrayHashSet: Add 'getOrAdd(key)' identity method to conveniently get the identity or add it, if not exist yet
Diffstat (limited to 'src/java/com/jogamp/common/util/ArrayHashSet.java')
-rw-r--r-- | src/java/com/jogamp/common/util/ArrayHashSet.java | 33 |
1 files changed, 28 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. * <br> * 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 <code>ident</code> Object + * @param key 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 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.<br> + * If the <code>key</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 + * @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); + 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; } } |