All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.map.weak.identity.WeakIdentityHashMap Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map.weak.identity;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Supplier;

/**
 * This map combines the features of {@link IdentityHashMap} and
 * {@link WeakHashMap}.
 * 

* The keys of this map are hashed by identity and referenced by weak * references. *

* This map supports null for values but not for keys. * * @see IdentityHashMap * @see WeakHashMap * @author Oliver Richers */ public class WeakIdentityHashMap { private final Map, V> hashMap; private final ReferenceQueue referenceQueue; public WeakIdentityHashMap() { this(HashMap::new); } public WeakIdentityHashMap(Supplier, V>> factory) { this.hashMap = factory.get(); this.referenceQueue = new ReferenceQueue<>(); } public V get(K key) { collect(); WeakIdentityReference reference = new WeakIdentityReference<>(key); return hashMap.get(reference); } public V put(K key, V value) { collect(); if (key == null) { throw new IllegalArgumentException("No null keys allowed."); } WeakIdentityReference reference = new WeakIdentityReference<>(key, referenceQueue); return hashMap.put(reference, value); } public V remove(K key) { collect(); WeakIdentityReference reference = new WeakIdentityReference<>(key); return hashMap.remove(reference); } public int size() { collect(); return hashMap.size(); } public boolean isEmpty() { return size() == 0; } public void collect() { while (true) { Reference deadReference = referenceQueue.poll(); if (deadReference == null) { // no more dead references break; } hashMap.remove(deadReference); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy