mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-19 22:16:01 +00:00
ref[storage]: refactor convert
This commit is contained in:
1 parent
befe0badef
commit
28600e7102
6 files changed
+25
-23
No files matched your search
@@ -81,9 +81,7 @@ public class ResourceInterpreter {
|
|||||||
|
|
||||||
for (var fieldInfo : fieldInfos) {
|
for (var fieldInfo : fieldInfos) {
|
||||||
var content = columns.get(fieldInfo.index);
|
var content = columns.get(fieldInfo.index);
|
||||||
if (StringUtils.isNotEmpty(content) || fieldInfo.field.getType() == String.class) {
|
inject(instance, fieldInfo.field, content);
|
||||||
inject(instance, fieldInfo.field, content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result.add(instance);
|
result.add(instance);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,9 +56,7 @@ public class ListConverter implements ConditionalGenericConverter {
|
|||||||
clazz = (Class<?>) type;
|
clazz = (Class<?>) type;
|
||||||
}
|
}
|
||||||
if (content.startsWith("[") || content.endsWith("]")) {
|
if (content.startsWith("[") || content.endsWith("]")) {
|
||||||
return clazz.equals(List.class)
|
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
||||||
? Collections.unmodifiableList(JsonUtils.string2List(content, clazz))
|
|
||||||
: JsonUtils.string2List(content, clazz);
|
|
||||||
}
|
}
|
||||||
return ConvertUtils.convertToList(content, clazz);
|
return ConvertUtils.convertToList(content, clazz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,9 +55,7 @@ public class SetConverter implements ConditionalGenericConverter {
|
|||||||
clazz = (Class<?>) type;
|
clazz = (Class<?>) type;
|
||||||
}
|
}
|
||||||
if (content.startsWith("[") || content.endsWith("]")) {
|
if (content.startsWith("[") || content.endsWith("]")) {
|
||||||
return clazz.equals(Set.class)
|
return Collections.unmodifiableSet(JsonUtils.string2Set(content, clazz));
|
||||||
? Collections.unmodifiableSet(JsonUtils.string2Set(content, clazz))
|
|
||||||
: JsonUtils.string2Set(content, clazz);
|
|
||||||
}
|
}
|
||||||
return ConvertUtils.convertToSet(content, clazz);
|
return ConvertUtils.convertToSet(content, clazz);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public abstract class ConvertUtils {
|
|||||||
var converters = new HashSet<>();
|
var converters = new HashSet<>();
|
||||||
converters.add(new ArrayConverter());
|
converters.add(new ArrayConverter());
|
||||||
converters.add(new ListConverter());
|
converters.add(new ListConverter());
|
||||||
|
converters.add(new SetConverter());
|
||||||
converters.add(new JsonToMapConverter());
|
converters.add(new JsonToMapConverter());
|
||||||
converters.add(new JsonToObjectConverter());
|
converters.add(new JsonToObjectConverter());
|
||||||
converters.add(new StringToClassConverter());
|
converters.add(new StringToClassConverter());
|
||||||
@@ -60,12 +61,10 @@ public abstract class ConvertUtils {
|
|||||||
return Array.newInstance(componentType, 0);
|
return Array.newInstance(componentType, 0);
|
||||||
}
|
}
|
||||||
// 用普通的逗号分隔符解析
|
// 用普通的逗号分隔符解析
|
||||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
var list = convertToList(content, componentType);
|
||||||
var length = splits.length;
|
Object array = Array.newInstance(componentType, list.size());
|
||||||
Object array = Array.newInstance(componentType, length);
|
for (var i = 0; i < list.size(); i++) {
|
||||||
for (var i = 0; i < length; i++) {
|
Array.set(array, i, list.get(i));
|
||||||
Object value = ConvertUtils.convert(StringUtils.trim(splits[i]), componentType);
|
|
||||||
Array.set(array, i, value);
|
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
@@ -90,13 +89,6 @@ public abstract class ConvertUtils {
|
|||||||
if (StringUtils.isEmpty(content)) {
|
if (StringUtils.isEmpty(content)) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
return Set.copyOf(convertToList(content, type));
|
||||||
var length = splits.length;
|
|
||||||
var set = new HashSet<T>();
|
|
||||||
for (var i = 0; i < length; i++) {
|
|
||||||
var value = ConvertUtils.convert(StringUtils.trim(splits[i]), type);
|
|
||||||
set.add(value);
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableSet(set);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@ import com.zfoo.storage.anno.Index;
|
|||||||
import com.zfoo.storage.anno.Storage;
|
import com.zfoo.storage.anno.Storage;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author godotg
|
* @author godotg
|
||||||
@@ -39,6 +40,9 @@ public class StudentResource {
|
|||||||
private int age;
|
private int age;
|
||||||
private float score;
|
private float score;
|
||||||
private String[] courses;
|
private String[] courses;
|
||||||
|
private int[] intArray;
|
||||||
|
private List<Integer> intList;
|
||||||
|
private Set<Integer> intSet;
|
||||||
private User[] users;
|
private User[] users;
|
||||||
private List<User> userList;
|
private List<User> userList;
|
||||||
private User user;
|
private User user;
|
||||||
@@ -67,6 +71,18 @@ public class StudentResource {
|
|||||||
return courses;
|
return courses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int[] getIntArray() {
|
||||||
|
return intArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> getIntList() {
|
||||||
|
return intList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Integer> getIntSet() {
|
||||||
|
return intSet;
|
||||||
|
}
|
||||||
|
|
||||||
public User[] getUsers() {
|
public User[] getUsers() {
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in new issue
Block a user