78 |
|
|
79 |
public EnumMap(Map<K, ? extends V> map) |
public EnumMap(Map<K, ? extends V> map) |
80 |
{ |
{ |
81 |
if (map instanceof EnumMap<K, ? extends V>) |
if (map instanceof EnumMap) |
82 |
{ |
{ |
83 |
EnumMap<K, ? extends V> other = (EnumMap<K, ? extends V>) map; |
EnumMap<K, ? extends V> other = (EnumMap<K, ? extends V>) map; |
84 |
store = (V[]) other.store.clone(); |
store = (V[]) other.store.clone(); |
93 |
if (store == null) |
if (store == null) |
94 |
{ |
{ |
95 |
enumClass = key.getDeclaringClass(); |
enumClass = key.getDeclaringClass(); |
96 |
store = new V[enumClass.getEnumConstants().length]; |
store = (V[]) new Object[enumClass.getEnumConstants().length]; |
97 |
} |
} |
98 |
int o = key.ordinal(); |
int o = key.ordinal(); |
99 |
if (store[o] == emptySlot) |
if (store[o] == emptySlot) |
123 |
|
|
124 |
public boolean containsKey(Object key) |
public boolean containsKey(Object key) |
125 |
{ |
{ |
126 |
if (! (key instanceof Enum<K>)) |
if (! (key instanceof Enum)) |
127 |
return false; |
return false; |
128 |
Enum<K> e = (Enum<K>) key; |
Enum<K> e = (Enum<K>) key; |
129 |
if (e.enumClass != enumClass) |
if (e.getDeclaringClass() != enumClass) |
130 |
return false; |
return false; |
131 |
return store[e.ordinal()] != emptySlot; |
return store[e.ordinal()] != emptySlot; |
132 |
} |
} |
133 |
|
|
134 |
public V get(Object key) |
public V get(Object key) |
135 |
{ |
{ |
136 |
if (! (key instanceof Enum<K>)) |
if (! (key instanceof Enum)) |
137 |
return null; |
return null; |
138 |
Enum<K> e = (Enum<K>) key; |
Enum<K> e = (Enum<K>) key; |
139 |
if (e.enumClass != enumClass) |
if (e.getDeclaringClass() != enumClass) |
140 |
return null; |
return null; |
141 |
return store[e.ordinal()]; |
return store[e.ordinal()]; |
142 |
} |
} |
158 |
|
|
159 |
public V remove(Object key) |
public V remove(Object key) |
160 |
{ |
{ |
161 |
if (! (key instanceof Enum<K>)) |
if (! (key instanceof Enum)) |
162 |
return null; |
return null; |
163 |
Enum<K> e = (Enum<K>) key; |
Enum<K> e = (Enum<K>) key; |
164 |
if (e.enumClass != enumClass) |
if (e.getDeclaringClass() != enumClass) |
165 |
return null; |
return null; |
166 |
V result = store[e.ordinal()]; |
V result = store[e.ordinal()]; |
167 |
if (result == emptySlot) |
if (result == emptySlot) |
168 |
result = null; |
result = null; |
169 |
else |
else |
170 |
--cardinality; |
--cardinality; |
171 |
store[e.ordinal()] = emptySlot; |
store[e.ordinal()] = (V) emptySlot; |
172 |
return result; |
return result; |
173 |
} |
} |
174 |
|
|
225 |
public void remove() |
public void remove() |
226 |
{ |
{ |
227 |
--cardinality; |
--cardinality; |
228 |
store[index] = emptySlot; |
store[index] = (V) emptySlot; |
229 |
} |
} |
230 |
}; |
}; |
231 |
} |
} |
242 |
|
|
243 |
public boolean remove(Object o) |
public boolean remove(Object o) |
244 |
{ |
{ |
245 |
return EnumMap.this.remove(o); |
return EnumMap.this.remove(o) != null; |
246 |
} |
} |
247 |
}; |
}; |
248 |
} |
} |
272 |
return count < cardinality; |
return count < cardinality; |
273 |
} |
} |
274 |
|
|
275 |
public K next() |
public V next() |
276 |
{ |
{ |
277 |
++count; |
++count; |
278 |
for (++index; store[index] == emptySlot; ++index) |
for (++index; store[index] == emptySlot; ++index) |
283 |
public void remove() |
public void remove() |
284 |
{ |
{ |
285 |
--cardinality; |
--cardinality; |
286 |
store[index] = emptySlot; |
store[index] = (V) emptySlot; |
287 |
} |
} |
288 |
}; |
}; |
289 |
} |
} |
320 |
return count < cardinality; |
return count < cardinality; |
321 |
} |
} |
322 |
|
|
323 |
public K next() |
public Map.Entry<K,V> next() |
324 |
{ |
{ |
325 |
++count; |
++count; |
326 |
for (++index; store[index] == emptySlot; ++index) |
for (++index; store[index] == emptySlot; ++index) |
330 |
return new AbstractMap.BasicMapEntry<K, V>(enumClass.getEnumConstants()[index], |
return new AbstractMap.BasicMapEntry<K, V>(enumClass.getEnumConstants()[index], |
331 |
store[index]) |
store[index]) |
332 |
{ |
{ |
333 |
public V setValue(K newVal) |
public V setValue(V newVal) |
334 |
{ |
{ |
335 |
value = newVal; |
value = newVal; |
336 |
return put(key, newVal); |
return put(key, newVal); |
341 |
public void remove() |
public void remove() |
342 |
{ |
{ |
343 |
--cardinality; |
--cardinality; |
344 |
store[index] = emptySlot; |
store[index] = (V) emptySlot; |
345 |
} |
} |
346 |
}; |
}; |
347 |
} |
} |
353 |
|
|
354 |
public boolean contains(Object o) |
public boolean contains(Object o) |
355 |
{ |
{ |
356 |
if (! (o instanceof Map.Entry<K, V>)) |
if (! (o instanceof Map.Entry)) |
357 |
return false; |
return false; |
358 |
Map.Entry<K, V> other = (Map.Entry<K, V>) o; |
Map.Entry<K, V> other = (Map.Entry<K, V>) o; |
359 |
return (containsKey(other.getKey()) |
return (containsKey(other.getKey()) |
363 |
|
|
364 |
public boolean remove(Object o) |
public boolean remove(Object o) |
365 |
{ |
{ |
366 |
if (! (o instanceof Map.Entry<K, V>)) |
if (! (o instanceof Map.Entry)) |
367 |
return false; |
return false; |
368 |
Map.Entry<K, V> other = (Map.Entry<K, V>) o; |
Map.Entry<K, V> other = (Map.Entry<K, V>) o; |
369 |
return EnumMap.this.remove(other.getKey()); |
return EnumMap.this.remove(other.getKey()) != null; |
370 |
} |
} |
371 |
}; |
}; |
372 |
} |
} |
375 |
|
|
376 |
public boolean equals(Object o) |
public boolean equals(Object o) |
377 |
{ |
{ |
378 |
if (! (o instanceof EnumMap<K, V>)) |
if (! (o instanceof EnumMap)) |
379 |
return false; |
return false; |
380 |
EnumMap<K, V> other = (EnumMap<K, V>) o; |
EnumMap<K, V> other = (EnumMap<K, V>) o; |
381 |
if (other.enumClass != enumClass || other.cardinality != cardinality) |
if (other.enumClass != enumClass || other.cardinality != cardinality) |
385 |
|
|
386 |
public EnumMap<K, V> clone() |
public EnumMap<K, V> clone() |
387 |
{ |
{ |
388 |
EnumMap<K, V> r = (EnumMap<K, V>) super.clone(); |
/* We don't use super.clone() as enums don't support it */ |
389 |
r.store = (V[]) store.clone(); |
return new EnumMap(this); |
|
return r; |
|
390 |
} |
} |
391 |
|
|
392 |
} |
} |