mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-11 03:13:51 +00:00
update
This commit is contained in:
1 parent
24b5b6012a
commit
7258e12a36
4 files changed
+116
-15
No files matched your search
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package com.zfoo.protocol.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
*/
|
||||
public class CsvUtils {
|
||||
|
||||
/**
|
||||
* CSV 字符串转对象列表
|
||||
*
|
||||
* @param csv CSV 格式字符串,第一行是表头
|
||||
* @param clazz 目标类
|
||||
* @param <T> 泛型类型
|
||||
* @return 对象列表
|
||||
*/
|
||||
public static <T> List<T> parse(String csv, Class<T> clazz) {
|
||||
if (StringUtils.isEmpty(csv)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
var lines = csv.split(FileUtils.LS_REGEX);
|
||||
if (lines.length < 2) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
var result = new ArrayList<T>();
|
||||
// 第一行作为表头
|
||||
var headers = lines[0].split(StringUtils.COMMA_REGEX);
|
||||
var headerIndex = new HashMap<String, Integer>();
|
||||
for (var i = 0; i < headers.length; i++) {
|
||||
headerIndex.put(headers[i].trim(), i);
|
||||
}
|
||||
|
||||
var fields = ReflectionUtils.notStaticAndTransientFields(clazz);
|
||||
fields.forEach(it -> ReflectionUtils.makeAccessible(it));
|
||||
// 从第二行开始解析
|
||||
for (var i = 1; i < lines.length; i++) {
|
||||
var values = lines[i].split(StringUtils.COMMA_REGEX);
|
||||
|
||||
T obj = ReflectionUtils.newInstance(clazz);
|
||||
for (var field : fields) {
|
||||
if (!headerIndex.containsKey(field.getName())) {
|
||||
continue;
|
||||
}
|
||||
var idx = headerIndex.get(field.getName());
|
||||
if (idx < values.length) {
|
||||
var raw = values[idx].trim();
|
||||
Object converted = ReflectionUtils.convertSimple(raw, field.getType());
|
||||
ReflectionUtils.setField(field, obj, converted);
|
||||
}
|
||||
}
|
||||
result.add(obj);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public abstract class DomUtils {
|
||||
try {
|
||||
return MAPPER.readValue(xml, clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RunException("将xml字符串[xml:{}]转换为对象[{}]异常", xml, clazz, e);
|
||||
throw new RunException("convert xml:[{}] to class:[{}] exception", xml, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public abstract class DomUtils {
|
||||
var sr = f.createXMLStreamReader(new FileInputStream(xmlFile));
|
||||
return MAPPER.readValue(sr, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将xml文件[xml:{}]转换为对象[{}]异常", xmlFile, clazz, e);
|
||||
throw new RunException("convert file xml:[{}] to class:[{}] exception", xmlFile.getName(), clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public abstract class DomUtils {
|
||||
try {
|
||||
return MAPPER.readValue(xmlInputStream, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将xmlInputStream转换为对象[{}]异常", clazz, e);
|
||||
throw new RunException("convert input stream to class:[{}] exception", clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz, e);
|
||||
throw new RunException("convert json:[{}] to class:[{}] exception", json, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.writeValueAsString(object);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
|
||||
throw new RunException("convert class:[{}] to json exception", object.getClass(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
|
||||
throw new RunException("convert class:[{}] to json exception", object.getClass(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER_TURBO.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz, e);
|
||||
throw new RunException("convert json:[{}] to class:[{}] exception", json, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER_TURBO.writeValueAsString(object);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
|
||||
throw new RunException("convert class:[{}] to json exception", object.getClass(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.readValue(json, collectionType);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为List[{}]异常", json, clazz, e);
|
||||
throw new RunException("convert json:[{}] to List:[{}] exception", json, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.readValue(json, collectionType);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为Set[{}]异常", json, clazz, e);
|
||||
throw new RunException("convert json:[{}] to Set:[{}] exception", json, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public abstract class JsonUtils {
|
||||
var ct = MAPPER.getTypeFactory().constructCollectionType(collectionType, elementType);
|
||||
return MAPPER.readValue(json, ct);
|
||||
} catch (IOException e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为Collection[{}]异常", json, collectionType, e);
|
||||
throw new RunException("convert json:[{}] to Collection:[{}] exception", json, collectionType, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.readValue(json, mapType);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为Map[[key:{}], [value:{}]]异常", json, kClazz, vClazz, e);
|
||||
throw new RunException("convert json:[{}] to Map key:[{}] value:[{}] exception", json, kClazz, vClazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ public abstract class JsonUtils {
|
||||
try {
|
||||
return MAPPER.readValue(json, arrayType);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("将json字符串[{}]转换为数组[array:{}]异常", json, clazz, e);
|
||||
throw new RunException("convert json:[{}] to Array:[{}] exception", json, clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ public abstract class JsonUtils {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为jsonTree[nodeName:{}]异常", json, nodeName, e);
|
||||
throw new RunException("convert json:[{}] to JsonTree:[{}] exception", json, nodeName, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -215,7 +215,7 @@ public abstract class JsonUtils {
|
||||
}
|
||||
return jsonMap;
|
||||
} catch (IOException e) {
|
||||
throw new RunException("将json字符串[json:{}]转换为jsonMap异常", json, e);
|
||||
throw new RunException("convert json:[{}] to JsonMap exception", json, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,5 +352,33 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* simple convert to simple primitive type, String
|
||||
*/
|
||||
public static Object convertSimple(String value, Class<?> targetType) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (targetType == String.class) {
|
||||
return value;
|
||||
} else if (targetType == byte.class || targetType == Byte.class) {
|
||||
return Byte.parseByte(value);
|
||||
} else if (targetType == short.class || targetType == Short.class) {
|
||||
return Short.parseShort(value);
|
||||
} else if (targetType == int.class || targetType == Integer.class) {
|
||||
return Integer.parseInt(value);
|
||||
} else if (targetType == long.class || targetType == Long.class) {
|
||||
return Long.parseLong(value);
|
||||
} else if (targetType == float.class || targetType == Float.class) {
|
||||
return Float.parseFloat(value);
|
||||
} else if (targetType == double.class || targetType == Double.class) {
|
||||
return Double.parseDouble(value);
|
||||
} else if (targetType == boolean.class || targetType == Boolean.class) {
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user