52package com.jogamp.common.util;
54import java.lang.ref.ReferenceQueue;
55import java.lang.ref.WeakReference;
56import java.util.Collection;
57import java.util.Collections;
58import java.util.HashMap;
59import java.util.HashSet;
76 private final ReferenceQueue<K> queue =
new ReferenceQueue<>();
77 private final Map<IdentityWeakReference<K>, V> backingStore;
83 backingStore =
new HashMap<>();
100 backingStore =
new HashMap<>(initialCapacity, loadFactor);
111 @SuppressWarnings(
"rawtypes")
113 final float[] lf = { loadFactor };
134 return Integer.MAX_VALUE;
136 float lf = loadFactor[0];
137 int c0 = (int)( requiredSize/lf + 1.0f );
143 c0 = (int)( requiredSize/lf + 1.0f );
152 backingStore.clear();
156 @SuppressWarnings(
"unchecked")
160 return backingStore.containsKey(
new IdentityWeakReference<K>((K) key, queue));
166 return backingStore.containsValue(value);
172 final Set<Map.Entry<K, V>> ret =
new HashSet<>();
173 for (
final Map.Entry<IdentityWeakReference<K>, V> ref : backingStore.entrySet()) {
174 final K key = ref.getKey().get();
176 final V value = ref.getValue();
177 final Map.Entry<K, V> entry =
new Map.Entry<K, V>() {
184 public V getValue() {
189 public V setValue(
final V value) {
190 throw new UnsupportedOperationException();
196 return Collections.unmodifiableSet(ret);
202 final Set<K> ret =
new HashSet<>();
203 for (
final IdentityWeakReference<K> ref : backingStore.keySet()) {
204 final K key = ref.get();
209 return Collections.unmodifiableSet(ret);
220 @SuppressWarnings(
"unchecked")
222 public V
get(
final Object key) {
224 return backingStore.get(
new IdentityWeakReference<K>((K) key, queue));
228 public V
put(
final K key,
final V value) {
230 return backingStore.put(
new IdentityWeakReference<K>(key, queue), value);
236 return backingStore.hashCode();
242 return backingStore.isEmpty();
246 public void putAll(
final Map<? extends K, ? extends V> t) {
247 final int n = t.size();
249 final float[] lf = { 0.75f };
251 final Map<IdentityWeakReference<K>, V> t2 =
new HashMap<>(icap, lf[0]);
252 for (
final Map.Entry<? extends K, ? extends V> e : t.entrySet()) {
253 t2.put(
new IdentityWeakReference<K>(e.getKey(), queue), e.getValue());
255 backingStore.putAll(t2);
260 @SuppressWarnings(
"unchecked")
262 public V
remove(
final Object key) {
264 return backingStore.remove(
new IdentityWeakReference<K>((K) key, queue));
270 return backingStore.size();
276 return backingStore.values();
279 private synchronized void reap() {
280 Object zombie = queue.poll();
282 while (zombie !=
null) {
283 @SuppressWarnings(
"unchecked")
284 final IdentityWeakReference<K> victim = (IdentityWeakReference<K>) zombie;
285 backingStore.
remove(victim);
286 zombie = queue.poll();
290 private static class IdentityWeakReference<K> extends WeakReference<K> {
293 IdentityWeakReference(
final K obj,
final ReferenceQueue<K> q) {
295 hash = System.identityHashCode(obj);
304 public boolean equals(
final Object o) {
308 if (!(o instanceof IdentityWeakReference)) {
311 @SuppressWarnings(
"unchecked")
312 final IdentityWeakReference<K> ref = (IdentityWeakReference<K>) o;
313 return this.
get() == ref.
get();
Bit operation utilities (static).
static final boolean isPowerOf2(final int n)
Returns true if the given integer is a power of 2.
static final int MAX_POWER_OF_2
Maximum 32bit integer value being of isPowerOf2(int).
Implements a combination of WeakHashMap and IdentityHashMap.
V put(final K key, final V value)
boolean containsValue(final Object value)
V remove(final Object key)
boolean equals(final Object o)
WeakIdentityHashMap(final int initialCapacity, final float loadFactor)
See HashMap#HashMap(int, float).
Set< Map.Entry< K, V > > entrySet()
static WeakIdentityHashMap<?, ?> createWithRequiredSize(final int requiredSize, final float loadFactor)
Static creation method using capacityForRequiredSize(int, float[]) to instantiate a new WeakIdentityH...
static int capacityForRequiredSize(final int requiredSize, final float[] loadFactor)
Returns the [initial] capacity using the given loadFactor and requiredSize.
boolean containsKey(final Object key)
void putAll(final Map<? extends K, ? extends V > t)
WeakIdentityHashMap()
See HashMap#HashMap().
Simple bitfield interface for efficient bit storage access in O(1).