package com.vci.web.util; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.vci.web.util.jackson.JsonUtil; import org.springframework.util.StringUtils; import org.springframework.beans.BeansException; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.convert.TypeDescriptor; import org.springframework.lang.Nullable; import org.springframework.util.PatternMatchUtils; import org.springframework.web.method.HandlerMethod; import java.io.Closeable; import java.io.File; import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.nio.charset.Charset; import java.text.DecimalFormat; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.util.*; import java.util.function.Supplier; /** * 工具包集合,工具类快捷方式 * * @author L.cm */ public class Func { /** * 通常用于将主键oid转换成8位唯一hash码值 * @return */ public static String oidEnHash(String oid){ UUID originalUUID = UUID.fromString(oid); // 使用Base64编码将原始UUID转换为较短的字符串 String base64String = Base64.getUrlEncoder().withoutPadding().encodeToString(new byte[] { (byte)(originalUUID.getMostSignificantBits()>>>56), (byte)(originalUUID.getMostSignificantBits()>>>48), (byte)(originalUUID.getMostSignificantBits()>>>40), (byte)(originalUUID.getMostSignificantBits()>>>32), (byte)(originalUUID.getMostSignificantBits()>>>24), (byte)(originalUUID.getMostSignificantBits()>>>16), (byte)(originalUUID.getMostSignificantBits()>>>8), (byte)(originalUUID.getMostSignificantBits()), (byte)(originalUUID.getLeastSignificantBits()>>>56), (byte)(originalUUID.getLeastSignificantBits()>>>48), (byte)(originalUUID.getLeastSignificantBits()>>>40), (byte)(originalUUID.getLeastSignificantBits()>>>32), (byte)(originalUUID.getLeastSignificantBits()>>>24), (byte)(originalUUID.getLeastSignificantBits()>>>16), (byte)(originalUUID.getLeastSignificantBits()>>>8), (byte)(originalUUID.getLeastSignificantBits())}); return base64String; } /** * 通常用于将主键oid转换成的8位唯一hash码,反转为oid * @param base64String * @return */ public static String oidDeHash(String base64String){ // 反推回原始的UUID字符串 byte[] bytes = Base64.getUrlDecoder().decode(base64String); UUID reconstructedUUID = new UUID(((long)(bytes[0] & 0xff) << 56) | ((long)(bytes[1] & 0xff) << 48) | ((long)(bytes[2] & 0xff) << 40) | ((long)(bytes[3] & 0xff) << 32) | ((long)(bytes[4] & 0xff) << 24) | ((long)(bytes[5] & 0xff) << 16) | ((long)(bytes[6] & 0xff) << 8) | (bytes[7] & 0xff), ((long)(bytes[8] & 0xff) << 56) | ((long)(bytes[9] & 0xff) << 48) | ((long)(bytes[10] & 0xff) << 40) | ((long)(bytes[11] & 0xff) << 32) | ((long)(bytes[12] & 0xff) << 24) | ((long)(bytes[13] & 0xff) << 16) | ((long)(bytes[14] & 0xff) << 8) | (bytes[15] & 0xff)); return reconstructedUUID.toString().toUpperCase(Locale.ROOT); } /** * 断言,必须不能为 null *
* * @param obj the object reference to check for nullity * @param* public Foo(Bar bar) { * this.bar = $.requireNotNull(bar); * } *
* * @param obj the object reference to check for nullity * @param message detail message to be used in the event that a {@code * NullPointerException} is thrown * @param* public Foo(Bar bar, Baz baz) { * this.bar = $.requireNotNull(bar, "bar must not be null"); * this.baz = $.requireNotNull(baz, "baz must not be null"); * } *
* * @param obj the object reference to check for nullity * @param messageSupplier supplier of the detail message to be * used in the event that a {@code NullPointerException} is thrown * @param* public Foo(Bar bar, Baz baz) { * this.bar = $.requireNotNull(bar, () -> "bar must not be null"); * } *
* This method exists to be used as a * {@link java.util.function.Predicate}, {@code filter($::isNull)} *
* * @param obj a reference to be checked against {@code null} * @return {@code true} if the provided reference is {@code null} otherwise * {@code false} * @see java.util.function.Predicate */ public static boolean isNull(@Nullable Object obj) { return Objects.isNull(obj); } /** * 判断对象是否 not null ** This method exists to be used as a * {@link java.util.function.Predicate}, {@code filter($::notNull)} *
* * @param obj a reference to be checked against {@code null} * @return {@code true} if the provided reference is non-{@code null} * otherwise {@code false} * @see java.util.function.Predicate */ public static boolean notNull(@Nullable Object obj) { return Objects.nonNull(obj); } /** * 首字母变小写 * * @param str 字符串 * @return {String} */ public static String firstCharToLower(String str) { return StringUtil.firstCharToLower(str); } /** * 首字母变大写 * * @param str 字符串 * @return {String} */ public static String firstCharToUpper(String str) { return StringUtil.firstCharToUpper(str); } /** * 判断是否为空字符串 ** $.isBlank(null) = true * $.isBlank("") = true * $.isBlank(" ") = true * $.isBlank("12345") = false * $.isBlank(" 12345 ") = false ** * @param cs the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not {@code null}, * its length is greater than 0, and it does not contain whitespace only * @see Character#isWhitespace */ public static boolean isBlank(@Nullable final CharSequence cs) { return StringUtil.isBlank(cs); } /** * 判断不为空字符串 *
* $.isNotBlank(null) = false * $.isNotBlank("") = false * $.isNotBlank(" ") = false * $.isNotBlank("bob") = true * $.isNotBlank(" bob ") = true ** * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is * not empty and not null and not whitespace * @see Character#isWhitespace */ public static boolean isNotBlank(@Nullable final CharSequence cs) { return StringUtil.isNotBlank(cs); } /** * 判断是否有任意一个 空字符串 * * @param css CharSequence * @return boolean */ public static boolean isAnyBlank(final CharSequence... css) { return StringUtil.isAnyBlank(css); } /** * 判断是否全为非空字符串 * * @param css CharSequence * @return boolean */ public static boolean isNoneBlank(final CharSequence... css) { return StringUtil.isNoneBlank(css); } /** * 判断对象是数组 * * @param obj the object to check * @return 是否数组 */ public static boolean isArray(@Nullable Object obj) { return ObjectUtil.isArray(obj); } /** * 判断空对象 object、map、list、set、字符串、数组 * * @param obj the object to check * @return 数组是否为空 */ public static boolean isEmpty(@Nullable Object obj) { return ObjectUtil.isEmpty(obj); } /** * 对象不为空 object、map、list、set、字符串、数组 * * @param obj the object to check * @return 是否不为空 */ public static boolean isNotEmpty(@Nullable Object obj) { return !ObjectUtil.isEmpty(obj); } /** * 判断数组为空 * * @param array the array to check * @return 数组是否为空 */ public static boolean isEmpty(@Nullable Object[] array) { return ObjectUtil.isEmpty(array); } /** * 判断数组不为空 * * @param array 数组 * @return 数组是否不为空 */ public static boolean isNotEmpty(@Nullable Object[] array) { return ObjectUtil.isNotEmpty(array); } /** * 对象组中是否存在 Empty Object * * @param os 对象组 * @return boolean */ public static boolean hasEmpty(Object... os) { for (Object o : os) { if (isEmpty(o)) { return true; } } return false; } /** * 对象组中是否全部为 Empty Object * * @param os 对象组 * @return boolean */ public static boolean isAllEmpty(Object... os) { for (Object o : os) { if (isNotEmpty(o)) { return false; } } return true; } /** * 将字符串中特定模式的字符转换成map中对应的值 *
* use: format("my name is ${name}, and i like ${like}!", {"name":"L.cm", "like": "Java"})
*
* @param message 需要转换的字符串
* @param params 转换所需的键值对集合
* @return 转换后的字符串
*/
public static String format(@Nullable String message, @Nullable Map
* use: format("my name is {}, and i like {}!", "L.cm", "Java")
*
* @param message 需要转换的字符串
* @param arguments 需要替换的变量
* @return 转换后的字符串
*/
public static String format(@Nullable String message, @Nullable Object... arguments) {
return StringUtil.format(message, arguments);
}
/**
* 格式化执行时间,单位为 ms 和 s,保留三位小数
*
* @param nanos 纳秒
* @return 格式化后的时间
*/
public static String format(long nanos) {
return StringUtil.format(nanos);
}
/**
* 比较两个对象是否相等。 Convert a If the string is Convert a If the string is Convert a If the string is Convert a If the string is Convert a If the string is Convert a If the string is
* pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
*
* pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy"
*
* 支持 map bean copy
*
* 支持 map bean copy
*
* 支持 map bean copy
*
* 相同的条件有两个,满足其一即可:
*
* @param obj1 对象1
* @param obj2 对象2
* @return 是否相等
*/
public static boolean equals(Object obj1, Object obj2) {
return Objects.equals(obj1, obj2);
}
/**
* 安全的 equals
*
* @param o1 first Object to compare
* @param o2 second Object to compare
* @return whether the given objects are equal
* @see Object#equals(Object)
* @see java.util.Arrays#equals
*/
public static boolean equalsSafe(@Nullable Object o1, @Nullable Object o2) {
return ObjectUtil.nullSafeEquals(o1, o2);
}
/**
* 判断数组中是否包含元素
*
* @param array the Array to check
* @param element the element to look for
* @param
* $.toInt(null) = 0
* $.toInt("") = 0
* $.toInt("1") = 1
*
*
* @param str the string to convert, may be null
* @return the int represented by the string, or zero
if
* conversion fails
*/
public static int toInt(final Object str) {
return NumberUtil.toInt(String.valueOf(str));
}
/**
* 字符串转 int,为空则返回默认值
*
*
* $.toInt(null, 1) = 1
* $.toInt("", 1) = 1
* $.toInt("1", 0) = 1
*
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
*/
public static int toInt(@Nullable final Object str, final int defaultValue) {
return NumberUtil.toInt(String.valueOf(str), defaultValue);
}
/**
* 字符串转 long,为空则返回0
*
*
* $.toLong(null) = 0L
* $.toLong("") = 0L
* $.toLong("1") = 1L
*
*
* @param str the string to convert, may be null
* @return the long represented by the string, or 0
if
* conversion fails
*/
public static long toLong(final Object str) {
return NumberUtil.toLong(String.valueOf(str));
}
/**
* 字符串转 long,为空则返回默认值
*
*
* $.toLong(null, 1L) = 1L
* $.toLong("", 1L) = 1L
* $.toLong("1", 0L) = 1L
*
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the long represented by the string, or the default if conversion fails
*/
public static long toLong(@Nullable final Object str, final long defaultValue) {
return NumberUtil.toLong(String.valueOf(str), defaultValue);
}
/**
* String
to an Double
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toDouble(null, 1) = 1.0
* $.toDouble("", 1) = 1.0
* $.toDouble("1", 0) = 1.0
*
*
* @param value the string to convert, may be null
* @return the int represented by the string, or the default if conversion fails
*/
public static Double toDouble(Object value) {
return toDouble(String.valueOf(value), -1.00);
}
/**
* String
to an Double
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toDouble(null, 1) = 1.0
* $.toDouble("", 1) = 1.0
* $.toDouble("1", 0) = 1.0
*
*
* @param value the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
*/
public static Double toDouble(Object value, Double defaultValue) {
return NumberUtil.toDouble(String.valueOf(value), defaultValue);
}
/**
* String
to an Float
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toFloat(null, 1) = 1.00f
* $.toFloat("", 1) = 1.00f
* $.toFloat("1", 0) = 1.00f
*
*
* @param value the string to convert, may be null
* @return the int represented by the string, or the default if conversion fails
*/
public static Float toFloat(Object value) {
return toFloat(String.valueOf(value), -1.0f);
}
/**
* String
to an Float
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toFloat(null, 1) = 1.00f
* $.toFloat("", 1) = 1.00f
* $.toFloat("1", 0) = 1.00f
*
*
* @param value the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
*/
public static Float toFloat(Object value, Float defaultValue) {
return NumberUtil.toFloat(String.valueOf(value), defaultValue);
}
/**
* String
to an Boolean
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toBoolean("true", true) = true
* $.toBoolean("false") = false
* $.toBoolean("", false) = false
*
*
* @param value the string to convert, may be null
* @return the int represented by the string, or the default if conversion fails
*/
public static Boolean toBoolean(Object value) {
return toBoolean(value, null);
}
/**
* String
to an Boolean
, returning a
* default value if the conversion fails.null
, the default value is returned.
* $.toBoolean("true", true) = true
* $.toBoolean("false") = false
* $.toBoolean("", false) = false
*
*
* @param value the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
*/
public static Boolean toBoolean(Object value, Boolean defaultValue) {
if (value != null) {
String val = String.valueOf(value);
val = val.toLowerCase().trim();
return Boolean.parseBoolean(val);
}
return defaultValue;
}
/**
* 转换为Integer数组
*
* @param str 被转换的值
* @return 结果
*/
public static Integer[] toIntArray(String str) {
return toIntArray(",", str);
}
/**
* 转换为Integer数组
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static Integer[] toIntArray(String split, String str) {
if (StringUtil.isEmpty(str)) {
return new Integer[]{};
}
String[] arr = str.split(split);
final Integer[] ints = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) {
final Integer v = toInt(arr[i], 0);
ints[i] = v;
}
return ints;
}
/**
* 转换为Integer集合
*
* @param str 结果被转换的值
* @return 结果
*/
public static List
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static List
*
* @param str 被转换的值
* @return 结果
*/
public static Long[] toLongArray(String str) {
return toLongArray(",", str);
}
/**
* 转换为Long数组
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static Long[] toLongArray(String split, String str) {
if (StringUtil.isEmpty(str)) {
return new Long[]{};
}
String[] arr = str.split(split);
final Long[] longs = new Long[arr.length];
for (int i = 0; i < arr.length; i++) {
final Long v = toLong(arr[i], 0);
longs[i] = v;
}
return longs;
}
/**
* 转换为Long集合
*
* @param str 结果被转换的值
* @return 结果
*/
public static List
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static List
*
* @param str 被转换的值
* @return 结果
*/
public static String[] toStrArray(String str) {
return toStrArray(",", str);
}
/**
* 转换为String数组
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static String[] toStrArray(String split, String str) {
if (isBlank(str)) {
return new String[]{};
}
return str.split(split);
}
/**
* 转换为String集合
*
* @param str 结果被转换的值
* @return 结果
*/
public static List
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static ListInputStream
to read from
* @return the requested String
* @throws NullPointerException if the input is null
*/
public static String readToString(InputStream input) {
return IoUtil.readToString(input);
}
/**
* InputStream to String
*
* @param input the InputStream
to read from
* @param charset the Charset
* @return the requested String
* @throws NullPointerException if the input is null
*/
public static String readToString(@Nullable InputStream input, Charset charset) {
return IoUtil.readToString(input, charset);
}
/**
* InputStream to bytes 数组
*
* @param input InputStream
* @return the requested byte array
*/
public static byte[] readToByteArray(@Nullable InputStream input) {
return IoUtil.readToByteArray(input);
}
/**
* 读取文件为字符串
*
* @param file the file to read, must not be {@code null}
* @return the file contents, never {@code null}
*/
public static String readToString(final File file) {
return FileUtil.readToString(file);
}
/**
* 读取文件为字符串
*
* @param file the file to read, must not be {@code null}
* @param encoding the encoding to use, {@code null} means platform default
* @return the file contents, never {@code null}
*/
public static String readToString(File file, Charset encoding) {
return FileUtil.readToString(file, encoding);
}
/**
* 读取文件为 byte 数组
*
* @param file the file to read, must not be {@code null}
* @return the file contents, never {@code null}
*/
public static byte[] readToByteArray(File file) {
return FileUtil.readToByteArray(file);
}
/**
* 将对象序列化成json字符串
*
* @param object javaBean
* @return jsonString json字符串
*/
public static String toJson(Object object) {
return JsonUtil.toJson(object);
}
/**
* 将对象序列化成 json byte 数组
*
* @param object javaBean
* @return jsonString json字符串
*/
public static byte[] toJsonAsBytes(Object object) {
return JsonUtil.toJsonAsBytes(object);
}
/**
* 将json字符串转成 JsonNode
*
* @param jsonString jsonString
* @return jsonString json字符串
*/
public static JsonNode readTree(String jsonString) {
return JsonUtil.readTree(jsonString);
}
/**
* 将json字符串转成 JsonNode
*
* @param in InputStream
* @return jsonString json字符串
*/
public static JsonNode readTree(InputStream in) {
return JsonUtil.readTree(in);
}
/**
* 将json字符串转成 JsonNode
*
* @param content content
* @return jsonString json字符串
*/
public static JsonNode readTree(byte[] content) {
return JsonUtil.readTree(content);
}
/**
* 将json字符串转成 JsonNode
*
* @param jsonParser JsonParser
* @return jsonString json字符串
*/
public static JsonNode readTree(JsonParser jsonParser) {
return JsonUtil.readTree(jsonParser);
}
/**
* 将json byte 数组反序列化成对象
*
* @param bytes json bytes
* @param valueType class
* @param