cool.linco.common.convert.AbstractDictContainer Maven / Gradle / Ivy
The newest version!
package cool.linco.common.convert;
import javax.lang.model.type.NullType;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* @author Linco
*/
public abstract class AbstractDictContainer {
private Map>> dictFunctionMap = new HashMap<>(16);
private Map> dictItemFunctionMap = new HashMap<>(16);
final void addDict(String dict, String keyField, String valueField, Collection source) {
addDict(dict, ObjectMapUtils.makeKeyValueFromCollection(keyField, valueField, source));
}
/**
* 手动刷新并设置, 刷新有效则空值不覆盖, 刷新无效则空值也覆盖
* */
protected void setValue(String dict, String key, Object value) {
// 即使有value也要先按source规则初始化dict, 防止被覆盖
Object freshValue = freshDictItemFromSource(dict, key);
if (freshValue == null || value != null) {
safeSaveDictValue(dict, key, value);
}
}
/**
* 字典来源函数, 如果缓存中没有会调用字典来源取得
*
* @param dict 字典名
* @param getDictFunction 取值方法
*/
protected void addDictSource(String dict, String keyField, String valueField, Function> getDictFunction) {
dictFunctionMap.put(dict, (nullType) -> ObjectMapUtils.makeKeyValueFromCollection(keyField, valueField, getDictFunction.apply(null)));
}
/**
* 如果缓存中没有, 调用字典来源取得数据
*
* @param key
*/
protected synchronized Object freshDictItemFromSource(String dict, String key) {
// 整个dict未初始化
if (!contains(dict)) {
if (dictFunctionMap.containsKey(dict)) {
addDict(dict, dictFunctionMap.get(dict).apply(null));
} else if (dictItemFunctionMap.containsKey(dict)) {
Object value = dictItemFunctionMap.get(dict).apply(key);
safeSaveDictValue(dict, key, value);
}
return getValueCache(dict, key);
}
// 单个数据可能未更新
if (!dictItemFunctionMap.containsKey(dict)) {
return null;
}
Object value = dictItemFunctionMap.get(dict).apply(key);
safeSaveDictValue(dict, key, value);
return value;
}
private void safeSaveDictValue(String dict, String key, Object value){
if (contains(dict)) {
saveDictValue(dict, key, value);
} else if (value != null){
// 原信息不存在, 不需要处理null
Map values = new HashMap<>(8);
values.put(key, value);
addDict(dict, values);
}
}
/**
* 单项数据来源函数, 如果缓存中没有会调用字典来源取得
*
* @param dict 字典名
*/
protected void addDictItemSource(String dict, Function getItemFunction) {
dictItemFunctionMap.put(dict, getItemFunction);
}
Object getValue(String dict, String key) {
Object value = getValueCache(dict, key);
if (value == null) {
value = freshDictItemFromSource(dict, key);
}
return value;
}
protected abstract void addDict(String key, Map mapValues);
protected abstract Object getValueCache(String dict, String key);
/**
* 添加字典
* value为空视存储实现可能要操作删除key以节省空间
* @param dict 字典名
* @param key key
* @param value value
*/
protected abstract void saveDictValue(String dict, String key, Object value);
protected abstract boolean contains(String dict);
final protected boolean containsOrConfig(String dict) {
return dictFunctionMap.containsKey(dict) || dictItemFunctionMap.containsKey(dict) || contains(dict);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy