lucee.commons.lang.WeakConcurrentHashMapAsHashtable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lucee Show documentation
Show all versions of lucee Show documentation
Building the Lucee Loader JAR
package lucee.commons.lang;
import java.util.Hashtable;
public class WeakConcurrentHashMapAsHashtable extends Hashtable {
/*
* private static final Reference DEFAULT_VALUE = new WeakReference("");
*
* private ConcurrentHashMap> map;
*
* public WeakConcurrentHashMapAsHashtable() { this.map = new ConcurrentHashMap<>(); }
*
* @Override public int size() { int size = 0; for (Reference ref: map.values()) { if (ref !=
* null && ref.get() != null) size++; }
*
* return size; }
*
* @Override public boolean isEmpty() { for (Reference ref: map.values()) { if (ref != null &&
* ref.get() != null) return false; } return true; }
*
* @Override public Enumeration keys() { return entries().keys(); }
*
* @Override public Enumeration elements() { return entries().elements(); }
*
* @Override public boolean contains(Object value) { return map.containsValue(value); }
*
* @Override public boolean containsValue(Object value) { if (value == null) throw new
* NullPointerException();
*
* for (Reference ref: map.values()) { if (ref != null && ref.get() != null && (ref.get() ==
* value || ref.get().equals(value))) return true; } return false; }
*
* @Override public boolean containsKey(Object key) { if (key == null) throw new
* NullPointerException(); Reference ref = map.get(key); return ref != null && ref.get() != null;
* }
*
* @Override public V get(Object key) { Reference ref = map.get(key); return ref != null ?
* ref.get() : null; }
*
* @Override protected void rehash() { // do nothing }
*
* @Override public V put(K key, V value) { Reference ref = map.put(key, new
* WeakReference(value)); return ref != null ? ref.get() : null; }
*
* private void putIgnoreNull(K key, V value) { if (value != null) map.put(key, new
* WeakReference(value)); }
*
* @Override public V remove(Object key) { Reference ref = map.remove(key); return ref != null ?
* ref.get() : null; }
*
* @Override public void putAll(Map extends K, ? extends V> m) { for (Map.Entry extends K, ?
* extends V> e: m.entrySet()) { put(e.getKey(), e.getValue()); } }
*
* @Override public void clear() { map.clear(); }
*
* @Override public Object clone() { ConcurrentHashMapAsHashtable newMap = new
* ConcurrentHashMapAsHashtable<>(); Reference ref; for (java.util.Map.Entry> e:
* map.entrySet()) { ref = e.getValue(); if (ref != null) newMap.putIgnoreNull(e.getKey(),
* ref.get()); } return newMap; }
*
* @Override public String toString() {
*
* return map.toString(); }
*
* @Override public Set keySet() {
*
* return map.keySet(); }
*
* @Override public Set> entrySet() { return entries().entrySet(); }
*
* @Override public Collection values() { return entries().values(); }
*
* @Override public boolean equals(Object o) {
*
* return map.equals(o); }
*
* @Override public int hashCode() {
*
* return map.hashCode(); }
*
* @Override public V getOrDefault(Object key, V defaultValue) { Reference ref =
* map.getOrDefault(key, DEFAULT_VALUE); return ref == DEFAULT_VALUE ? defaultValue : (V) ref.get();
* }
*
* @Override public void forEach(BiConsumer super K, ? super V> action) {
* entries().forEach(action); }
*
* @Override public void replaceAll(BiFunction super K, ? super V, ? extends V> function) {
*
* map.replaceAll(function); }
*
* @Override public V putIfAbsent(K key, V value) {
*
* return map.putIfAbsent(key, value); }
*
* @Override public boolean remove(Object key, Object value) {
*
* return map.remove(key, value); }
*
* @Override public boolean replace(K key, V oldValue, V newValue) {
*
* return map.replace(key, oldValue, newValue); }
*
* @Override public V replace(K key, V value) {
*
* return map.replace(key, value); }
*
* @Override public V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction) {
*
* return map.computeIfAbsent(key, mappingFunction); }
*
* @Override public V computeIfPresent(K key, BiFunction super K, ? super V, ? extends V>
* remappingFunction) {
*
* return map.computeIfPresent(key, remappingFunction); }
*
* @Override public V compute(K key, BiFunction super K, ? super V, ? extends V>
* remappingFunction) {
*
* return map.compute(key, remappingFunction); }
*
* @Override public V merge(K key, V value, BiFunction super V, ? super V, ? extends V>
* remappingFunction) {
*
* return map.merge(key, value, remappingFunction); }
*
* private ConcurrentHashMap entries() { ConcurrentHashMap entries = new
* ConcurrentHashMap<>(); V v; for (Entry> e: map.entrySet()) { if (e.getValue() !=
* null) { v = e.getValue().get(); if (v != null) entries.put(e.getKey(), e.getValue().get()); } }
* return entries; }
*/
}