mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-18 06:27:01 +00:00
ref[storage]: use the JDK new API
This commit is contained in:
@@ -46,29 +46,29 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
|
||||
|
||||
public static StorageObject<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
|
||||
var storage = new StorageObject<>();
|
||||
storage.clazz = resourceClazz;
|
||||
var idDef = IdDef.valueOf(resourceClazz);
|
||||
storage.idDef = idDef;
|
||||
storage.indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz));
|
||||
|
||||
try {
|
||||
var storage = new StorageObject<>();
|
||||
storage.clazz = resourceClazz;
|
||||
var idDef = IdDef.valueOf(resourceClazz);
|
||||
storage.idDef = idDef;
|
||||
storage.indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz));
|
||||
var list = ResourceInterpreter.read(inputStream, resourceClazz, suffix);
|
||||
for (var object : list) {
|
||||
storage.put(object);
|
||||
}
|
||||
var idType = idDef.getField().getType();
|
||||
if (idType == int.class || idType == Integer.class) {
|
||||
return new StorageInt<>(storage);
|
||||
} else if (idType == long.class || idType == Long.class) {
|
||||
return new StorageLong<>(storage);
|
||||
} else {
|
||||
return storage;
|
||||
}
|
||||
storage.putAll(list);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
} finally {
|
||||
IOUtils.closeIO(inputStream);
|
||||
}
|
||||
|
||||
var idType = idDef.getField().getType();
|
||||
if (idType == int.class || idType == Integer.class) {
|
||||
return new StorageInt<>(storage);
|
||||
} else if (idType == long.class || idType == Long.class) {
|
||||
return new StorageLong<>(storage);
|
||||
} else {
|
||||
return storage;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -179,36 +179,38 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
return dataMap.size();
|
||||
}
|
||||
|
||||
public V put(Object value) {
|
||||
@SuppressWarnings("unchecked")
|
||||
var id = (K) ReflectionUtils.getField(idDef.getField(), value);
|
||||
public void putAll(List<?> values) {
|
||||
for (var value: values) {
|
||||
@SuppressWarnings("unchecked")
|
||||
var id = (K) ReflectionUtils.getField(idDef.getField(), value);
|
||||
|
||||
if (id == null) {
|
||||
throw new RuntimeException("There is an item with an unconfigured id in the static resource");
|
||||
}
|
||||
if (dataMap.containsKey(id)) {
|
||||
throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName()));
|
||||
}
|
||||
// 添加资源
|
||||
@SuppressWarnings("unchecked")
|
||||
var v = (V) value;
|
||||
var result = dataMap.put(id, v);
|
||||
// 添加索引
|
||||
for (var def : indexDefMap.values()) {
|
||||
// 使用field的名称作为索引的名称
|
||||
var indexKey = def.getField().getName();
|
||||
var indexValue = ReflectionUtils.getField(def.getField(), v);
|
||||
if (def.isUnique()) {
|
||||
var uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> new HashMap<>(32));
|
||||
if (uniqueIndex.put(indexValue, v) != null) {
|
||||
throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName()));
|
||||
if (id == null) {
|
||||
throw new RuntimeException("There is an item with an unconfigured id in the static resource");
|
||||
}
|
||||
if (dataMap.containsKey(id)) {
|
||||
throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName()));
|
||||
}
|
||||
// 添加资源
|
||||
@SuppressWarnings("unchecked")
|
||||
var v = (V) value;
|
||||
dataMap.put(id, v);
|
||||
// 添加索引
|
||||
for (var def : indexDefMap.values()) {
|
||||
// 使用field的名称作为索引的名称
|
||||
var indexKey = def.getField().getName();
|
||||
var indexValue = ReflectionUtils.getField(def.getField(), v);
|
||||
if (def.isUnique()) {
|
||||
var uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> HashMap.newHashMap(values.size()));
|
||||
if (uniqueIndex.put(indexValue, v) != null) {
|
||||
throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName()));
|
||||
}
|
||||
} else {
|
||||
var index = indexMap.computeIfAbsent(indexKey, it -> HashMap.newHashMap(values.size()));
|
||||
var list = index.computeIfAbsent(indexValue, it -> new ArrayList<V>());
|
||||
list.add(v);
|
||||
}
|
||||
} else {
|
||||
var index = indexMap.computeIfAbsent(indexKey, it -> new HashMap<>(32));
|
||||
var list = index.computeIfAbsent(indexValue, it -> new ArrayList<V>());
|
||||
list.add(v);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user