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

com.moon.core.util.ThreadLocalMap Maven / Gradle / Ivy

package com.moon.core.util;

import com.moon.core.enums.Maps;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 本地线程可见的 Map
 * 

* 依赖{@link ThreadLocal}实现的当前线程 Map * * @author moonsky */ public class ThreadLocalMap extends ThreadLocal> implements Map { private final static Supplier CONSTRUCTOR = Maps.HashMaps; private final Supplier> constructor; private Supplier> constructor() { return constructor == null ? CONSTRUCTOR : constructor; } private Map getMap() { Map m = get(); if (m == null) { set(m = constructor().get()); } return m; } public ThreadLocalMap() { this(CONSTRUCTOR); } public ThreadLocalMap(Supplier> constructor) { this.constructor = (constructor == null) ? CONSTRUCTOR : constructor; } public ThreadLocalMap(Map map) { this(() -> new HashMap<>(map)); } @Override public int size() { return getMap().size(); } @Override public boolean isEmpty() { return getMap().isEmpty(); } @Override public boolean containsKey(Object key) { return getMap().containsKey(key); } @Override public boolean containsValue(Object value) { return getMap().containsValue(value); } @Override public V get(Object key) { return getMap().get(key); } @Override public V put(K key, V value) { return getMap().put(key, value); } @Override public V remove(Object key) { return getMap().remove(key); } @Override public void putAll(Map m) { getMap().putAll(m); } @Override public void clear() { getMap().clear(); } @Override public Set keySet() { return getMap().keySet(); } @Override public Collection values() { return getMap().values(); } @Override public Set> entrySet() { return getMap().entrySet(); } @Override public void forEach(BiConsumer action) { this.getMap().forEach(action); } @Override public V merge(K key, V value, BiFunction remappingFunction) { return getMap().merge(key, value, remappingFunction); } @Override public V compute(K key, BiFunction remappingFunction) { return getMap().compute(key, remappingFunction); } @Override public V computeIfAbsent(K key, Function mappingFunction) { return getMap().computeIfAbsent(key, mappingFunction); } @Override public V computeIfPresent(K key, BiFunction remappingFunction) { return getMap().computeIfPresent(key, remappingFunction); } @Override public V getOrDefault(Object key, V defaultValue) { return getMap().getOrDefault(key, defaultValue); } @Override public void replaceAll(BiFunction function) { getMap().replaceAll(function); } @Override public V putIfAbsent(K key, V value) { return getMap().putIfAbsent(key, value); } @Override public boolean remove(Object key, Object value) { return getMap().remove(key, value); } @Override public boolean replace(K key, V oldValue, V newValue) { return getMap().replace(key, oldValue, newValue); } @Override public V replace(K key, V value) { return getMap().replace(key, value); } @Override public Map get() { Object cached = super.get(); if (cached instanceof Map) { return (Map) cached; } if (cached == null) { return null; } throw new IllegalArgumentException(); } @Override public void remove() { super.remove(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy