29package com.jogamp.common.util;
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.HashMap;
34import java.util.Iterator;
65 implements Cloneable, Map<K, V>
76 private final HashMap<K,V> map;
77 private final ArrayList<V> data;
78 private final boolean supportNullValue;
89 public ArrayHashMap(
final boolean supportNullValue,
final int initialCapacity,
final float loadFactor) {
90 this.map =
new HashMap<K,V>(initialCapacity, loadFactor);
91 this.data =
new ArrayList<V>(initialCapacity);
92 this.supportNullValue = supportNullValue;
99 map =
new HashMap<K, V>(o.map);
100 data =
new ArrayList<V>(o.data);
101 supportNullValue = o.supportNullValue;
131 public final ArrayList<V>
getData() {
return data; }
138 return new ArrayList<V>(data);
142 public final HashMap<K,V>
getMap() {
return map; }
145 public final String
toString() {
return data.toString(); }
177 return map.entrySet();
181 public final V
get(
final Object key) {
194 public final V
put(
final K key,
final V value)
throws NullPointerException {
196 if( supportNullValue ) {
198 final boolean exists = map.containsKey(key);
201 if(
null != ( oldValue = map.put(key, value) ) ) {
203 throw new InternalError(
"Already existing, but checked before: "+key+
" -> "+oldValue);
207 oldValue = map.put(key, value);
208 if( !data.remove(oldValue) ) {
209 throw new InternalError(
"Already existing, but not in list: "+oldValue);
213 checkNullValue(value);
215 if(
null != ( oldValue = map.put(key, value) ) ) {
217 if( !data.remove(oldValue) ) {
218 throw new InternalError(
"Already existing, but not in list: "+oldValue);
222 if(!data.add(value)) {
223 throw new InternalError(
"Couldn't add value to list: "+value);
229 public void putAll(
final Map<? extends K, ? extends V> m) {
230 for (
final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
231 final Map.Entry<? extends K, ? extends V> e = i.next();
232 put(e.getKey(), e.getValue());
244 public final V
remove(
final Object key) {
245 if( supportNullValue ) {
246 if( map.containsKey(key) ) {
248 final V oldValue = map.remove(key);
249 if ( !data.remove(oldValue) ) {
250 throw new InternalError(
"Couldn't remove prev mapped pair: "+key+
" -> "+oldValue);
256 if (
null != (oldValue = map.remove(key) ) ) {
258 if ( !data.remove(oldValue) ) {
259 throw new InternalError(
"Couldn't remove prev mapped pair: "+key+
" -> "+oldValue);
269 return map.containsKey(key);
274 return map.containsValue(value);
278 public final boolean equals(
final Object arrayHashMap) {
287 return map.hashCode();
292 return data.isEmpty();
300 private static final void checkNullValue(
final Object value)
throws NullPointerException {
301 if(
null == value ) {
302 throw new NullPointerException(
"Null value not supported");
HashMap implementation backed by an ArrayList to preserve order of values.
final Object clone()
Implementation uses ArrayHashMap(ArrayHashMap).
final boolean containsKey(final Object key)
final boolean supportsNullValue()
Returns true for default behavior, i.e.
boolean containsValue(final Object value)
static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity: {@value}.
final boolean equals(final Object arrayHashMap)
static final float DEFAULT_LOAD_FACTOR
Default load factor: {@value}.
Set< java.util.Map.Entry< K, V > > entrySet()
final ArrayList< V > getData()
Returns this object ordered ArrayList.
final ArrayList< V > toArrayList()
ArrayHashMap(final ArrayHashMap< K, V > o)
void putAll(final Map<? extends K, ? extends V > m)
final HashMap< K, V > getMap()
Returns this object hash map.
ArrayHashMap(final boolean supportNullValue, final int initialCapacity, final float loadFactor)
final V put(final K key, final V value)