mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-12-12 12:19:27 +08:00
✨ 完善工具类
This commit is contained in:
parent
cd71fcbcda
commit
a0f020729c
@ -17,18 +17,21 @@ package org.springblade.core.tool.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.TreeNode;
|
||||
import com.fasterxml.jackson.core.json.JsonReadFeature;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.type.CollectionLikeType;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.tool.utils.*;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
@ -266,7 +269,6 @@ public class JsonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将json byte 数组反序列化成对象
|
||||
*
|
||||
@ -401,12 +403,452 @@ public class JsonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clazz 获取 JavaType
|
||||
*
|
||||
* @param clazz Class
|
||||
* @return MapType
|
||||
*/
|
||||
public static JavaType getType(Class<?> clazz) {
|
||||
return getInstance().getTypeFactory().constructType(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装 map type,keyClass String
|
||||
*
|
||||
* @param valueClass value 类型
|
||||
* @return MapType
|
||||
*/
|
||||
public static MapType getMapType(Class<?> valueClass) {
|
||||
return getMapType(String.class, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装 map type
|
||||
*
|
||||
* @param keyClass key 类型
|
||||
* @param valueClass value 类型
|
||||
* @return MapType
|
||||
*/
|
||||
public static MapType getMapType(Class<?> keyClass, Class<?> valueClass) {
|
||||
return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装 map type
|
||||
*
|
||||
* @param elementClass 集合值类型
|
||||
* @return CollectionLikeType
|
||||
*/
|
||||
public static CollectionLikeType getListType(Class<?> elementClass) {
|
||||
return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装参数化类型
|
||||
*
|
||||
* <p>
|
||||
* 例如: Map.class, String.class, String.class 对应 Map[String, String]
|
||||
* </p>
|
||||
*
|
||||
* @param parametrized 泛型参数化
|
||||
* @param parameterClasses 泛型参数类型
|
||||
* @return JavaType
|
||||
*/
|
||||
public static JavaType getParametricType(Class<?> parametrized, Class<?>... parameterClasses) {
|
||||
return getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装参数化类型,用来构造复杂的泛型
|
||||
*
|
||||
* <p>
|
||||
* 例如: Map.class, String.class, String.class 对应 Map[String, String]
|
||||
* </p>
|
||||
*
|
||||
* @param parametrized 泛型参数化
|
||||
* @param parameterTypes 泛型参数类型
|
||||
* @return JavaType
|
||||
*/
|
||||
public static JavaType getParametricType(Class<?> parametrized, JavaType... parameterTypes) {
|
||||
return getInstance().getTypeFactory().constructParametricType(parametrized, parameterTypes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param elementClass elementClass
|
||||
* @param <T> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <T> List<T> readList(@Nullable byte[] content, Class<T> elementClass) {
|
||||
if (content == null || content.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getListType(elementClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content InputStream
|
||||
* @param elementClass elementClass
|
||||
* @param <T> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <T> List<T> readList(@Nullable InputStream content, Class<T> elementClass) {
|
||||
if (content == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getListType(elementClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param reader java.io.Reader
|
||||
* @param elementClass elementClass
|
||||
* @param <T> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <T> List<T> readList(@Nullable Reader reader, Class<T> elementClass) {
|
||||
if (reader == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(reader, getListType(elementClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param elementClass elementClass
|
||||
* @param <T> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <T> List<T> readList(@Nullable String content, Class<T> elementClass) {
|
||||
if (StringUtil.isBlank(content)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getListType(elementClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @return 集合
|
||||
*/
|
||||
public static Map<String, Object> readMap(@Nullable byte[] content) {
|
||||
return readMap(content, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content InputStream
|
||||
* @return 集合
|
||||
*/
|
||||
public static Map<String, Object> readMap(@Nullable InputStream content) {
|
||||
return readMap(content, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param reader java.io.Reader
|
||||
* @return 集合
|
||||
*/
|
||||
public static Map<String, Object> readMap(@Nullable Reader reader) {
|
||||
return readMap(reader, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @return 集合
|
||||
*/
|
||||
public static Map<String, Object> readMap(@Nullable String content) {
|
||||
return readMap(content, Object.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param valueClass 值类型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <V> Map<String, V> readMap(@Nullable byte[] content, Class<?> valueClass) {
|
||||
return readMap(content, String.class, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content InputStream
|
||||
* @param valueClass 值类型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <V> Map<String, V> readMap(@Nullable InputStream content, Class<?> valueClass) {
|
||||
return readMap(content, String.class, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param reader java.io.Reader
|
||||
* @param valueClass 值类型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <V> Map<String, V> readMap(@Nullable Reader reader, Class<?> valueClass) {
|
||||
return readMap(reader, String.class, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param valueClass 值类型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <V> Map<String, V> readMap(@Nullable String content, Class<?> valueClass) {
|
||||
return readMap(content, String.class, valueClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param keyClass key类型
|
||||
* @param valueClass 值类型
|
||||
* @param <K> 泛型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <K, V> Map<K, V> readMap(@Nullable byte[] content, Class<?> keyClass, Class<?> valueClass) {
|
||||
if (content == null || content.length == 0) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getMapType(keyClass, valueClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content InputStream
|
||||
* @param keyClass key类型
|
||||
* @param valueClass 值类型
|
||||
* @param <K> 泛型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <K, V> Map<K, V> readMap(@Nullable InputStream content, Class<?> keyClass, Class<?> valueClass) {
|
||||
if (content == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getMapType(keyClass, valueClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param reader java.io.Reader
|
||||
* @param keyClass key类型
|
||||
* @param valueClass 值类型
|
||||
* @param <K> 泛型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <K, V> Map<K, V> readMap(@Nullable Reader reader, Class<?> keyClass, Class<?> valueClass) {
|
||||
if (reader == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(reader, getMapType(keyClass, valueClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取集合
|
||||
*
|
||||
* @param content bytes
|
||||
* @param keyClass key类型
|
||||
* @param valueClass 值类型
|
||||
* @param <K> 泛型
|
||||
* @param <V> 泛型
|
||||
* @return 集合
|
||||
*/
|
||||
public static <K, V> Map<K, V> readMap(@Nullable String content, Class<?> keyClass, Class<?> valueClass) {
|
||||
if (StringUtil.isBlank(content)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
try {
|
||||
return getInstance().readValue(content, getMapType(keyClass, valueClass));
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* jackson 的类型转换
|
||||
*
|
||||
* @param fromValue 来源对象
|
||||
* @param toValueType 转换的类型
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
|
||||
return getInstance().convertValue(fromValue, toValueType);
|
||||
}
|
||||
|
||||
/**
|
||||
* jackson 的类型转换
|
||||
*
|
||||
* @param fromValue 来源对象
|
||||
* @param toValueType 转换的类型
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T> T convertValue(Object fromValue, JavaType toValueType) {
|
||||
return getInstance().convertValue(fromValue, toValueType);
|
||||
}
|
||||
|
||||
/**
|
||||
* jackson 的类型转换
|
||||
*
|
||||
* @param fromValue 来源对象
|
||||
* @param toValueTypeRef 泛型类型
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef) {
|
||||
return getInstance().convertValue(fromValue, toValueTypeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* tree 转对象
|
||||
*
|
||||
* @param treeNode TreeNode
|
||||
* @param valueType valueType
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T> T treeToValue(TreeNode treeNode, Class<T> valueType) {
|
||||
try {
|
||||
return getInstance().treeToValue(treeNode, valueType);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tree 转对象
|
||||
*
|
||||
* @param treeNode TreeNode
|
||||
* @param valueType valueType
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T> T treeToValue(TreeNode treeNode, JavaType valueType) {
|
||||
try {
|
||||
return getInstance().treeToValue(treeNode, valueType);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转 tree
|
||||
*
|
||||
* @param fromValue fromValue
|
||||
* @param <T> 泛型标记
|
||||
* @return 转换结果
|
||||
*/
|
||||
public static <T extends JsonNode> T valueToTree(@Nullable Object fromValue) {
|
||||
return getInstance().valueToTree(fromValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否可以序列化
|
||||
*
|
||||
* @param value 对象
|
||||
* @return 是否可以序列化
|
||||
*/
|
||||
public static boolean canSerialize(@Nullable Object value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
return getInstance().canSerialize(value.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否可以反序列化
|
||||
*
|
||||
* @param type JavaType
|
||||
* @return 是否可以反序列化
|
||||
*/
|
||||
public static boolean canDeserialize(JavaType type) {
|
||||
return getInstance().canDeserialize(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 ObjectNode
|
||||
*
|
||||
* @return ObjectNode
|
||||
*/
|
||||
public static ObjectNode createObjectNode() {
|
||||
return getInstance().createObjectNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 ArrayNode
|
||||
*
|
||||
* @return ArrayNode
|
||||
*/
|
||||
public static ArrayNode createArrayNode() {
|
||||
return getInstance().createArrayNode();
|
||||
}
|
||||
|
||||
public static ObjectMapper getInstance() {
|
||||
return JacksonHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class JacksonHolder {
|
||||
private static ObjectMapper INSTANCE = new JacksonObjectMapper();
|
||||
private static final ObjectMapper INSTANCE = new JacksonObjectMapper();
|
||||
}
|
||||
|
||||
public static class JacksonObjectMapper extends ObjectMapper {
|
||||
|
@ -15,11 +15,7 @@
|
||||
*/
|
||||
package org.springblade.core.tool.utils;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.core.io.*;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -42,7 +38,6 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils {
|
||||
* 2. file:
|
||||
* 3. ftp:
|
||||
* 4. http: and https:
|
||||
* 5. classpath*:
|
||||
* 6. C:/dir1/ and /Users/lcm
|
||||
* </p>
|
||||
*
|
||||
@ -53,7 +48,8 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils {
|
||||
public static Resource getResource(String resourceLocation) throws IOException {
|
||||
Assert.notNull(resourceLocation, "Resource location must not be null");
|
||||
if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
|
||||
return new ClassPathResource(resourceLocation);
|
||||
String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
|
||||
return new ClassPathResource(path);
|
||||
}
|
||||
if (resourceLocation.startsWith(FTP_URL_PREFIX)) {
|
||||
return new UrlResource(resourceLocation);
|
||||
@ -61,10 +57,25 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils {
|
||||
if (resourceLocation.matches(HTTP_REGEX)) {
|
||||
return new UrlResource(resourceLocation);
|
||||
}
|
||||
if (resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)) {
|
||||
return SpringUtil.getContext().getResource(resourceLocation);
|
||||
if (resourceLocation.startsWith(FILE_URL_PREFIX)) {
|
||||
return new FileUrlResource(resourceLocation);
|
||||
}
|
||||
return new FileSystemResource(resourceLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取资源文件为字符串
|
||||
*
|
||||
* @param resourceLocation 资源文件地址
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String getAsString(String resourceLocation) {
|
||||
try {
|
||||
Resource resource = getResource(resourceLocation);
|
||||
return IoUtil.toString(resource.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 org.springblade.core.tool.utils;
|
||||
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 运行时工具类
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class RuntimeUtil {
|
||||
private static volatile int pId = -1;
|
||||
private static final int CPU_NUM = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
/**
|
||||
* 获得当前进程的PID
|
||||
* <p>
|
||||
* 当失败时返回-1
|
||||
*
|
||||
* @return pid
|
||||
*/
|
||||
public static int getPId() {
|
||||
if (pId > 0) {
|
||||
return pId;
|
||||
}
|
||||
// something like '<pid>@<hostname>', at least in SUN / Oracle JVMs
|
||||
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
final int index = jvmName.indexOf(CharPool.AT);
|
||||
if (index > 0) {
|
||||
pId = NumberUtil.toInt(jvmName.substring(0, index), -1);
|
||||
return pId;
|
||||
}
|
||||
return pId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回应用启动的时间
|
||||
*
|
||||
* @return {Instant}
|
||||
*/
|
||||
public static Instant getStartTime() {
|
||||
return Instant.ofEpochMilli(ManagementFactory.getRuntimeMXBean().getStartTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回应用启动到现在的时间
|
||||
*
|
||||
* @return {Duration}
|
||||
*/
|
||||
public static Duration getUpTime() {
|
||||
return Duration.ofMillis(ManagementFactory.getRuntimeMXBean().getUptime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回输入的JVM参数列表
|
||||
*
|
||||
* @return jvm参数
|
||||
*/
|
||||
public static String getJvmArguments() {
|
||||
List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
|
||||
return StringUtil.join(vmArguments, StringPool.SPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CPU核数
|
||||
*
|
||||
* @return cpu count
|
||||
*/
|
||||
public static int getCpuNum() {
|
||||
return CPU_NUM;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user