docs: 补全 src 全部 Kotlin KDoc 与 Java Javadoc,版本升级至 5.5.1
Gitea Actions Build / Build (push) Canceled after 0s
Gitea Actions Build / Build (push) Canceled after 0s
为所有缺失文档的公开声明补充中文 KDoc/Javadoc(含 exception、json、 springboot、tuple 生成类、MediaType 枚举项及测试),仅插入注释不改代码; gradle.properties VERSIONS 5.4.4 -> 5.5.1。
This commit is contained in:
@@ -34,10 +34,15 @@ import javax.crypto.Cipher
|
||||
import javax.crypto.spec.GCMParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
/** 加密算法名称:AES。 */
|
||||
const val ALGORITHM = "AES"
|
||||
/** AES 加密变换模式:GCM 模式,无填充。 */
|
||||
const val TRANSFORMATION = "AES/GCM/NoPadding"
|
||||
/** GCM 模式初始化向量(IV)长度,单位字节。 */
|
||||
const val GCM_IV_LENGTH = 12
|
||||
/** GCM 模式认证标签(Tag)长度,单位字节。 */
|
||||
const val GCM_TAG_LENGTH = 16
|
||||
/** 全局安全随机数生成器,用于生成加密安全的随机 IV。 */
|
||||
val SECURE_RANDOM = SecureRandom()
|
||||
|
||||
|
||||
|
||||
@@ -33,12 +33,26 @@ import javax.lang.model.type.TypeMirror
|
||||
import javax.tools.StandardLocation
|
||||
|
||||
|
||||
/**
|
||||
* [AutoService] 注解的编译期处理器。
|
||||
*
|
||||
* 扫描标注了 [AutoService] 的类,收集服务接口与实现类的映射关系,
|
||||
* 并在编译输出目录的 META-INF/services 下生成对应的服务注册文件。
|
||||
*/
|
||||
@SupportedAnnotationTypes("com.mingliqiye.utils.annotation.AutoService")
|
||||
class AutoServiceProcessor : AbstractProcessor() {
|
||||
override fun init(processingEnv: ProcessingEnvironment) {
|
||||
super.init(processingEnv)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理标注了 [AutoService] 的元素,收集服务接口与实现类的映射并生成服务注册文件。
|
||||
* 若注解未显式指定服务接口,则默认使用该类直接实现的接口。
|
||||
*
|
||||
* @param annotations 本轮处理的注解类型集合
|
||||
* @param roundEnv 当前轮次的处理环境
|
||||
* @return 是否已处理该注解,存在标注元素时返回 true,否则返回 false
|
||||
*/
|
||||
override fun process(
|
||||
annotations: Set<TypeElement>,
|
||||
roundEnv: RoundEnvironment,
|
||||
@@ -73,6 +87,12 @@ class AutoServiceProcessor : AbstractProcessor() {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据服务接口与实现类的映射,在 META-INF/services 目录下生成服务注册文件。
|
||||
* 实现列表为空或文件创建失败时打印提示信息并跳过。
|
||||
*
|
||||
* @param map 服务接口全限定名到实现类全限定名列表的映射
|
||||
*/
|
||||
fun processClassAnnotation(map: Map<String, MutableList<String>>) {
|
||||
map.forEach { (interfaceName, implementations) ->
|
||||
if (implementations.isEmpty()) {
|
||||
|
||||
@@ -24,6 +24,11 @@ package com.mingliqiye.utils.base
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* Base10 编解码器实现类。
|
||||
*
|
||||
* 提供字节数组与十进制字符串之间的相互转换功能,基于 [BigInteger] 实现。
|
||||
*/
|
||||
internal class Base10Codec : BaseCodec {
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,7 +32,13 @@ package com.mingliqiye.utils.base
|
||||
*/
|
||||
internal class Base256Codec : BaseCodec {
|
||||
|
||||
/**
|
||||
* 伴生对象,持有 Base256 的编码字符集及其反向索引表。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* Base256 编码字符集,共 256 个字符,数组索引即对应的字节值(0~255)。
|
||||
*/
|
||||
val code = arrayOf(
|
||||
'!',
|
||||
'#',
|
||||
@@ -291,6 +297,9 @@ internal class Base256Codec : BaseCodec {
|
||||
'▘',
|
||||
'▙'
|
||||
)
|
||||
/**
|
||||
* 字符到字节值的反向映射表,用于解码时快速查找字符对应的索引。
|
||||
*/
|
||||
val codeMap = code.mapIndexed { index, c -> c to index }.toMap()
|
||||
}
|
||||
|
||||
@@ -303,6 +312,13 @@ internal class Base256Codec : BaseCodec {
|
||||
return String(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Base256 字符串解码为字节数组。
|
||||
*
|
||||
* @param string 待解码的字符串,所有字符必须属于编码字符集
|
||||
* @return 解码后的字节数组
|
||||
* @throws NullPointerException 如果字符串中包含字符集之外的字符
|
||||
*/
|
||||
override fun decode(string: String): ByteArray {
|
||||
val result = ByteArray(string.length)
|
||||
for (i in string.indices) {
|
||||
|
||||
@@ -22,6 +22,11 @@
|
||||
|
||||
package com.mingliqiye.utils.base
|
||||
|
||||
/**
|
||||
* Base2 编解码器实现类。
|
||||
*
|
||||
* 提供字节数组与二进制字符串(每字节 8 位,高位在前)之间的相互转换功能。
|
||||
*/
|
||||
internal class Base2Codec : BaseCodec {
|
||||
override fun encode(bytes: ByteArray): String {
|
||||
if (bytes.isEmpty()) return ""
|
||||
@@ -34,6 +39,13 @@ internal class Base2Codec : BaseCodec {
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* 将二进制字符串解码为字节数组。
|
||||
*
|
||||
* @param string 待解码的二进制字符串,长度必须为 8 的倍数且仅包含字符 '0' 和 '1'
|
||||
* @return 解码后的字节数组
|
||||
* @throws IllegalArgumentException 如果字符串长度不是 8 的倍数或包含非法字符
|
||||
*/
|
||||
override fun decode(string: String): ByteArray {
|
||||
if (string.length % 8 != 0) {
|
||||
throw IllegalArgumentException(
|
||||
|
||||
@@ -24,25 +24,25 @@ package com.mingliqiye.utils.base
|
||||
|
||||
import java.util.*
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64编解码工具类
|
||||
* 提供Base64编码和解码功能的实现
|
||||
*/
|
||||
internal class Base64Codec : BaseCodec {
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64编码器实例
|
||||
* 用于执行字节数组到Base64字符串的编码操作
|
||||
*/
|
||||
val encoder: Base64.Encoder = Base64.getEncoder()
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64解码器实例
|
||||
* 用于执行Base64字符串到字节数组的解码操作
|
||||
*/
|
||||
val decoder: Base64.Decoder = Base64.getDecoder()
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将字节数组编码为Base64字符串
|
||||
*
|
||||
* @param bytes 待编码的字节数组
|
||||
@@ -52,7 +52,7 @@ internal class Base64Codec : BaseCodec {
|
||||
return encoder.encodeToString(bytes)
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将Base64字符串解码为字节数组
|
||||
*
|
||||
* @param string 待解码的Base64字符串
|
||||
|
||||
@@ -24,21 +24,26 @@ package com.mingliqiye.utils.base
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* 无填充 Base64 URL 编解码器实现类。
|
||||
*
|
||||
* 使用 URL 安全的 Base64 字母表,编码结果不带末尾的 '=' 填充字符。
|
||||
*/
|
||||
internal class Base64NoPaddingCodec : BaseCodec {
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64编码器实例
|
||||
* 用于执行字节数组到Base64字符串的编码操作
|
||||
*/
|
||||
val encoder: Base64.Encoder = Base64.getUrlEncoder().withoutPadding()
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64解码器实例
|
||||
* 用于执行Base64字符串到字节数组的解码操作
|
||||
*/
|
||||
val decoder: Base64.Decoder = Base64.getUrlDecoder()
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将字节数组编码为Base64字符串
|
||||
*
|
||||
* @param bytes 待编码的字节数组
|
||||
@@ -48,7 +53,7 @@ internal class Base64NoPaddingCodec : BaseCodec {
|
||||
return encoder.encodeToString(bytes)
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将Base64字符串解码为字节数组
|
||||
*
|
||||
* @param string 待解码的Base64字符串
|
||||
|
||||
@@ -24,21 +24,26 @@ package com.mingliqiye.utils.base
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* URL 安全 Base64 编解码器实现类。
|
||||
*
|
||||
* 使用 '-' 和 '_' 替代标准 Base64 中的 '+' 和 '/',适用于 URL 与文件名场景。
|
||||
*/
|
||||
internal class Base64UrlCodec : BaseCodec {
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64编码器实例
|
||||
* 用于执行字节数组到Base64字符串的编码操作
|
||||
*/
|
||||
val encoder: Base64.Encoder = Base64.getUrlEncoder()
|
||||
|
||||
/*
|
||||
/**
|
||||
* Base64解码器实例
|
||||
* 用于执行Base64字符串到字节数组的解码操作
|
||||
*/
|
||||
val decoder: Base64.Decoder = Base64.getUrlDecoder()
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将字节数组编码为Base64字符串
|
||||
*
|
||||
* @param bytes 待编码的字节数组
|
||||
@@ -48,7 +53,7 @@ internal class Base64UrlCodec : BaseCodec {
|
||||
return encoder.encodeToString(bytes)
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* 将Base64字符串解码为字节数组
|
||||
*
|
||||
* @param string 待解码的Base64字符串
|
||||
|
||||
@@ -29,6 +29,9 @@ package com.mingliqiye.utils.base
|
||||
*/
|
||||
internal class Base91Codec : BaseCodec {
|
||||
|
||||
/**
|
||||
* 伴生对象,持有 Base91 的编码表与解码表。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* Base91 编码表,共 91 个可打印 ASCII 字符。
|
||||
|
||||
@@ -31,17 +31,36 @@ import com.mingliqiye.utils.base.code.BASE64_NO_PADDING as CODE_BASE64_NO_PADDIN
|
||||
import com.mingliqiye.utils.base.code.BASE64_URL as CODE_BASE64_URL
|
||||
import com.mingliqiye.utils.base.code.BASE91 as CODE_BASE91
|
||||
|
||||
/**
|
||||
* Base 编码类型枚举。
|
||||
*
|
||||
* 每个枚举项持有一个编解码器的工厂函数,枚举本身实现了 [BaseCodec],
|
||||
* 可直接作为编解码器使用,编码与解码操作委托给对应的编解码器实例。
|
||||
*
|
||||
* @param baseCodec 编解码器的工厂函数,每次访问 [codec] 时调用以创建实例
|
||||
*/
|
||||
enum class BaseType(private val baseCodec: () -> BaseCodec) : BaseCodec {
|
||||
|
||||
/** 二进制(Base2)编解码。 */
|
||||
BASE2(::CODE_BASE2),
|
||||
/** 十进制(Base10)编解码。 */
|
||||
BASE10(::CODE_BASE10),
|
||||
/** 十六进制(Base16)编解码。 */
|
||||
BASE16(::CODE_BASE16),
|
||||
/** 标准 Base64 编解码。 */
|
||||
BASE64(::CODE_BASE64),
|
||||
/** URL 安全的 Base64 编解码。 */
|
||||
BASE64_URL(::CODE_BASE64_URL),
|
||||
/** 无填充(不带末尾 '=')的 URL 安全 Base64 编解码。 */
|
||||
BASE64_NO_PADDING(::CODE_BASE64_NO_PADDING),
|
||||
/** Base91 编解码。 */
|
||||
BASE91(::CODE_BASE91),
|
||||
/** Base256 编解码。 */
|
||||
BASE256(::CODE_BASE256);
|
||||
|
||||
/**
|
||||
* 获取当前类型对应的编解码器实例(每次访问都会通过工厂函数新建)。
|
||||
*/
|
||||
val codec
|
||||
get() = baseCodec()
|
||||
|
||||
|
||||
@@ -62,11 +62,19 @@ val BASE64: BaseCodec by lazy {
|
||||
Base64Codec()
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供Base64 URL编码器的懒加载实例。
|
||||
* 该编码器用于将数据编码为URL安全的Base64格式。
|
||||
*/
|
||||
@get:JvmName("base64Url")
|
||||
val BASE64_URL: BaseCodec by lazy {
|
||||
Base64UrlCodec()
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供无填充Base64 URL编码器的懒加载实例。
|
||||
* 该编码器用于将数据编码为URL安全的Base64格式,且结果不带末尾的'='填充字符。
|
||||
*/
|
||||
@get:JvmName("base64NoPadding")
|
||||
val BASE64_NO_PADDING: BaseCodec by lazy {
|
||||
Base64NoPaddingCodec()
|
||||
|
||||
@@ -29,31 +29,69 @@ import java.security.SecureRandom
|
||||
import org.mindrot.jbcrypt.BCrypt as JBCrypt
|
||||
|
||||
|
||||
/**
|
||||
* 使用随机生成的盐对字符串进行 BCrypt 哈希。
|
||||
*
|
||||
* @param string 需要哈希的明文字符串
|
||||
* @return BCrypt 哈希字符串
|
||||
*/
|
||||
fun hashpw(string: String): String {
|
||||
return hashpw(string, gensalt())
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用指定的盐对字符串进行 BCrypt 哈希。
|
||||
*
|
||||
* @param string 需要哈希的明文字符串
|
||||
* @param salt BCrypt 盐字符串,默认随机生成
|
||||
* @return BCrypt 哈希字符串
|
||||
*/
|
||||
fun hashpw(string: String, salt: String = gensalt()): String {
|
||||
return JBCrypt.hashpw(string, salt)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证明文字符串与 BCrypt 哈希是否匹配。
|
||||
*
|
||||
* @param string 明文字符串
|
||||
* @param bcrypted 已生成的 BCrypt 哈希字符串
|
||||
* @return 匹配返回 true,否则返回 false
|
||||
*/
|
||||
fun checkpw(string: String, bcrypted: String): Boolean {
|
||||
return JBCrypt.checkpw(string, bcrypted)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成默认工作因子(10)的 BCrypt 盐。
|
||||
*
|
||||
* @return BCrypt 盐字符串
|
||||
*/
|
||||
fun gensalt(): String {
|
||||
return JBCrypt.gensalt()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成指定工作因子的 BCrypt 盐。
|
||||
*
|
||||
* @param long 工作因子(log2 迭代次数),值越大计算越慢
|
||||
* @return BCrypt 盐字符串
|
||||
*/
|
||||
fun gensalt(long: Int): String {
|
||||
return JBCrypt.gensalt(long)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用指定的安全随机数生成器生成 BCrypt 盐。
|
||||
*
|
||||
* @param long 工作因子(log2 迭代次数)
|
||||
* @param secureRandom 用于生成盐的安全随机数生成器
|
||||
* @return BCrypt 盐字符串
|
||||
*/
|
||||
fun gensalt(long: Int, secureRandom: SecureRandom): String {
|
||||
return JBCrypt.gensalt(long, secureRandom)
|
||||
}
|
||||
|
||||
@@ -198,6 +198,9 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
return "ByteBufferWrapper(capacity=$capacity, position=$position, limit=$limit, remaining=$remaining)#${hashCode()}"
|
||||
}
|
||||
|
||||
/**
|
||||
* 伴生对象,提供工厂方法以及类型转换器的注册与查询功能。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 包装现有的 ByteBuffer
|
||||
@@ -279,6 +282,11 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
byteBufferWrapperConvertor
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加转换器到映射表,通过具体化类型参数自动创建转换器实例
|
||||
*
|
||||
* @param T 转换器类型,必须提供无参构造函数
|
||||
*/
|
||||
@JvmName("inline_addConvertor")
|
||||
inline fun <reified T : ByteBufferWrapperConvertor<*>> addConvertor() {
|
||||
addConvertor(ByteBufferWrapperConvertor.getInstantiation<T>())
|
||||
@@ -537,24 +545,64 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
return convertor.read(this, parameters)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取指定类型的对象(不带额外参数)
|
||||
*
|
||||
* @param clazz 要读取的对象类型对应的 Class 对象
|
||||
* @param T 要读取的对象类型
|
||||
* @return 读取并转换后的 T 类型对象
|
||||
* @throws IllegalArgumentException 如果该类型没有注册对应的转换器
|
||||
*/
|
||||
fun <T : Any> getObject(clazz: Class<T>): T {
|
||||
return getObject(clazz, emptyArray())
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取指定类型的对象,并传入长度参数
|
||||
*
|
||||
* @param clazz 要读取的对象类型对应的 Class 对象
|
||||
* @param size 读取长度(如数组元素个数),作为转换器的第一个参数
|
||||
* @param T 要读取的对象类型
|
||||
* @return 读取并转换后的 T 类型对象
|
||||
* @throws IllegalArgumentException 如果该类型没有注册对应的转换器
|
||||
*/
|
||||
fun <T : Any> getObject(clazz: Class<T>, size: Int): T {
|
||||
return getObject(clazz, arrayOf(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取具体化类型 T 的对象,并传入额外参数
|
||||
*
|
||||
* @param parameters 传递给转换器的参数数组
|
||||
* @param T 要读取的对象类型
|
||||
* @return 读取并转换后的 T 类型对象
|
||||
* @throws IllegalArgumentException 如果该类型没有注册对应的转换器
|
||||
*/
|
||||
@JvmName("inline_getObject")
|
||||
inline fun <reified T : Any> getObject(parameters: Array<Any>): T {
|
||||
return getObject(T::class.java, parameters)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取具体化类型 T 的对象,并传入长度参数
|
||||
*
|
||||
* @param size 读取长度(如数组元素个数),作为转换器的第一个参数
|
||||
* @param T 要读取的对象类型
|
||||
* @return 读取并转换后的 T 类型对象
|
||||
* @throws IllegalArgumentException 如果该类型没有注册对应的转换器
|
||||
*/
|
||||
@JvmName("inline_getObject")
|
||||
inline fun <reified T : Any> getObject(size: Int): T {
|
||||
return getObject(T::class.java, size)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取具体化类型 T 的对象(不带额外参数)
|
||||
*
|
||||
* @param T 要读取的对象类型
|
||||
* @return 读取并转换后的 T 类型对象
|
||||
* @throws IllegalArgumentException 如果该类型没有注册对应的转换器
|
||||
*/
|
||||
@JvmName("inline_getObject")
|
||||
inline fun <reified T : Any> getObject(): T {
|
||||
return getObject(T::class.java)
|
||||
@@ -574,11 +622,24 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 按初始化函数生成指定长度的字节数组并写入缓冲区
|
||||
*
|
||||
* @param size 要生成并写入的字节数
|
||||
* @param init 初始化函数,根据索引生成每个字节的值
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putByteArray(size: Int, init: (Int) -> Byte): ByteBufferWrapper {
|
||||
buffer.put(ByteArray(size, init))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符数组完整写入缓冲区(每个字符取低 8 位,1字节)
|
||||
*
|
||||
* @param charArray 要写入的字符数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putCharArray(charArray: CharArray): ByteBufferWrapper {
|
||||
putByteArray(charArray.size) {
|
||||
charArray[it].code.toByte()
|
||||
@@ -586,6 +647,12 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的字符数组(每个字节转换为一个字符)
|
||||
*
|
||||
* @param size 要读取的字符数
|
||||
* @return 读取的字符数组
|
||||
*/
|
||||
fun getCharArray(size: Int): CharArray {
|
||||
return getByteArray(size).let {
|
||||
CharArray(it.size) { s ->
|
||||
@@ -594,52 +661,118 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将整数数组完整写入缓冲区(每个整数4字节)
|
||||
*
|
||||
* @param intArray 要写入的整数数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putIntArray(intArray: IntArray): ByteBufferWrapper {
|
||||
intArray.forEach(::putInt)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的整数数组
|
||||
*
|
||||
* @param size 要读取的整数个数
|
||||
* @return 读取的整数数组
|
||||
*/
|
||||
fun getIntArray(size: Int): IntArray {
|
||||
return IntArray(size) { getInt() }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将短整数数组完整写入缓冲区(每个短整数2字节)
|
||||
*
|
||||
* @param shortArray 要写入的短整数数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putShortArray(shortArray: ShortArray): ByteBufferWrapper {
|
||||
shortArray.forEach(::putShort)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的短整数数组
|
||||
*
|
||||
* @param size 要读取的短整数个数
|
||||
* @return 读取的短整数数组
|
||||
*/
|
||||
fun getShortArray(size: Int): ShortArray {
|
||||
return ShortArray(size) { getShort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将长整数数组完整写入缓冲区(每个长整数8字节)
|
||||
*
|
||||
* @param longArray 要写入的长整数数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putLongArray(longArray: LongArray): ByteBufferWrapper {
|
||||
longArray.forEach(::putLong)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的长整数数组
|
||||
*
|
||||
* @param size 要读取的长整数个数
|
||||
* @return 读取的长整数数组
|
||||
*/
|
||||
fun getLongArray(size: Int): LongArray {
|
||||
return LongArray(size) { getLong() }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将双精度浮点数数组完整写入缓冲区(每个浮点数8字节)
|
||||
*
|
||||
* @param doubleArray 要写入的双精度浮点数数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putDoubleArray(doubleArray: DoubleArray): ByteBufferWrapper {
|
||||
doubleArray.forEach(::putDouble)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的双精度浮点数数组
|
||||
*
|
||||
* @param size 要读取的双精度浮点数个数
|
||||
* @return 读取的双精度浮点数数组
|
||||
*/
|
||||
fun getDoubleArray(size: Int): DoubleArray {
|
||||
return DoubleArray(size) { getDouble() }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将单精度浮点数数组完整写入缓冲区(每个浮点数4字节)
|
||||
*
|
||||
* @param floatArray 要写入的单精度浮点数数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putFloatArray(floatArray: FloatArray): ByteBufferWrapper {
|
||||
floatArray.forEach(::putFloat)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的单精度浮点数数组
|
||||
*
|
||||
* @param size 要读取的单精度浮点数个数
|
||||
* @return 读取的单精度浮点数数组
|
||||
*/
|
||||
fun getFloatArray(size: Int): FloatArray {
|
||||
return FloatArray(size) { getFloat() }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将布尔数组完整写入缓冲区(每个布尔值1字节,true = 0x01,false = 0x00)
|
||||
*
|
||||
* @param booleanArray 要写入的布尔数组
|
||||
* @return 当前对象(支持链式调用)
|
||||
*/
|
||||
fun putBooleanArray(booleanArray: BooleanArray): ByteBufferWrapper {
|
||||
putByteArray(booleanArray.size) {
|
||||
if (booleanArray[it]) 0x01 else 0x00
|
||||
@@ -647,6 +780,13 @@ class ByteBufferWrapper private constructor(private val buffer: ByteBuffer) {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取指定长度的布尔数组
|
||||
*
|
||||
* @param size 要读取的布尔值个数
|
||||
* @return 读取的布尔数组
|
||||
* @throws IllegalArgumentException 如果读取到的字节值不是 0x00 或 0x01
|
||||
*/
|
||||
fun getBooleanArray(size: Int): BooleanArray {
|
||||
return getByteArray(size).let {
|
||||
BooleanArray(it.size) { index ->
|
||||
|
||||
@@ -33,21 +33,67 @@ import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.UUID as JUUID
|
||||
|
||||
/**
|
||||
* ByteBufferWrapper 类型转换器接口。
|
||||
*
|
||||
* 定义某种类型与 [ByteBufferWrapper] 之间的读写规则。
|
||||
*
|
||||
* @param T 该转换器处理的数据类型
|
||||
*/
|
||||
interface ByteBufferWrapperConvertorInterface<T : Any> {
|
||||
/**
|
||||
* 将对象写入缓冲区。
|
||||
*
|
||||
* @param obj 要写入的对象
|
||||
* @param byteBufferWrapper 目标缓冲区包装器
|
||||
*/
|
||||
fun write(obj: T, byteBufferWrapper: ByteBufferWrapper)
|
||||
|
||||
/**
|
||||
* 从缓冲区中读取对象。
|
||||
*
|
||||
* @param byteBufferWrapper 源缓冲区包装器
|
||||
* @param parameters 读取时需要的额外参数(如数组长度)
|
||||
* @return 读取并转换后的对象
|
||||
*/
|
||||
fun read(byteBufferWrapper: ByteBufferWrapper, parameters: Array<Any>): T
|
||||
|
||||
/**
|
||||
* 获取该转换器处理的数据类型。
|
||||
*
|
||||
* @return 类型对应的 Class 对象
|
||||
*/
|
||||
fun getType(): Class<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* ByteBufferWrapper 类型转换器抽象基类。
|
||||
*
|
||||
* 提供转换器实例的缓存创建以及默认转换器的注册功能。
|
||||
*
|
||||
* @param T 该转换器处理的数据类型
|
||||
*/
|
||||
abstract class ByteBufferWrapperConvertor<T : Any> : ByteBufferWrapperConvertorInterface<T> {
|
||||
/**
|
||||
* 返回包含所处理类型信息的字符串表示。
|
||||
*/
|
||||
override fun toString(): String {
|
||||
return "ByteBufferWrapperConvertor(type=${getType()})"
|
||||
}
|
||||
|
||||
/**
|
||||
* 伴生对象,管理转换器实例缓存并提供默认转换器安装方法。
|
||||
*/
|
||||
companion object {
|
||||
private val _instantiations =
|
||||
ConcurrentHashMap<Class<ByteBufferWrapperConvertor<*>>, ByteBufferWrapperConvertor<*>>()
|
||||
|
||||
/**
|
||||
* 获取指定转换器类的实例,不存在时通过无参构造函数创建并缓存。
|
||||
*
|
||||
* @param clazz 转换器类对应的 Class 对象
|
||||
* @return 缓存的或新创建的转换器实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getInstantiation(clazz: Class<ByteBufferWrapperConvertor<*>>): ByteBufferWrapperConvertor<*> {
|
||||
return _instantiations.getOrPut(clazz) {
|
||||
@@ -55,12 +101,23 @@ abstract class ByteBufferWrapperConvertor<T : Any> : ByteBufferWrapperConvertorI
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取具体化类型 T 对应的转换器实例,不存在时创建并缓存。
|
||||
*
|
||||
* @param T 转换器类型,必须提供无参构造函数
|
||||
* @return 缓存的或新创建的转换器实例
|
||||
*/
|
||||
@JvmStatic
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T : ByteBufferWrapperConvertor<*>> getInstantiation(): ByteBufferWrapperConvertor<*> {
|
||||
return getInstantiation(T::class.java as Class<ByteBufferWrapperConvertor<*>>)
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装所有默认转换器。
|
||||
*
|
||||
* 将基础类型、大数、字符串、时间、UUID 及各类数组的转换器注册到 [ByteBufferWrapper]。
|
||||
*/
|
||||
fun defaultConvertorInstall() {
|
||||
ByteBufferWrapper.addConvertor<BigDecimalBufferWrapperConvertor>()
|
||||
ByteBufferWrapper.addConvertor<BigIntegerBufferWrapperConvertor>()
|
||||
@@ -91,6 +148,16 @@ abstract class ByteBufferWrapperConvertor<T : Any> : ByteBufferWrapperConvertorI
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于读写函数构造的通用对象转换器。
|
||||
*
|
||||
* 通过传入的写入函数、读取函数和类型信息实现转换逻辑,读取时忽略额外参数。
|
||||
*
|
||||
* @param T 该转换器处理的数据类型
|
||||
* @param write 写入函数,将对象写入缓冲区
|
||||
* @param read 读取函数,从缓冲区读取对象
|
||||
* @param typer 该转换器处理的数据类型对应的 Class 对象
|
||||
*/
|
||||
abstract class ObjectBufferWrapperConvertor<T : Any>(
|
||||
val write: ByteBufferWrapper.(obj: T) -> Unit,
|
||||
val read: ByteBufferWrapper.() -> T,
|
||||
@@ -104,72 +171,84 @@ abstract class ObjectBufferWrapperConvertor<T : Any>(
|
||||
}
|
||||
|
||||
|
||||
/** [Byte] 类型转换器,使用 putByte/getByte 读写。 */
|
||||
class BytedBufferWrapperConvertor : ObjectBufferWrapperConvertor<Byte>(
|
||||
ByteBufferWrapper::putByte,
|
||||
ByteBufferWrapper::getByte,
|
||||
Byte::class.java,
|
||||
)
|
||||
|
||||
/** [Char] 类型转换器,使用 putChar/getChar 读写。 */
|
||||
class CharBufferWrapperConvertor : ObjectBufferWrapperConvertor<Char>(
|
||||
ByteBufferWrapper::putChar,
|
||||
ByteBufferWrapper::getChar,
|
||||
Char::class.java,
|
||||
)
|
||||
|
||||
/** [Short] 类型转换器,使用 putShort/getShort 读写。 */
|
||||
class ShortBufferWrapperConvertor : ObjectBufferWrapperConvertor<Short>(
|
||||
ByteBufferWrapper::putShort,
|
||||
ByteBufferWrapper::getShort,
|
||||
Short::class.java,
|
||||
)
|
||||
|
||||
/** [Int] 类型转换器,使用 putInt/getInt 读写。 */
|
||||
class IntBufferWrapperConvertor : ObjectBufferWrapperConvertor<Int>(
|
||||
ByteBufferWrapper::putInt,
|
||||
ByteBufferWrapper::getInt,
|
||||
Int::class.java,
|
||||
)
|
||||
|
||||
/** [Long] 类型转换器,使用 putLong/getLong 读写。 */
|
||||
class LongBufferWrapperConvertor : ObjectBufferWrapperConvertor<Long>(
|
||||
ByteBufferWrapper::putLong,
|
||||
ByteBufferWrapper::getLong,
|
||||
Long::class.java,
|
||||
)
|
||||
|
||||
/** [Double] 类型转换器,使用 putDouble/getDouble 读写。 */
|
||||
class DoubleBufferWrapperConvertor : ObjectBufferWrapperConvertor<Double>(
|
||||
ByteBufferWrapper::putDouble,
|
||||
ByteBufferWrapper::getDouble,
|
||||
Double::class.java,
|
||||
)
|
||||
|
||||
/** [Float] 类型转换器,使用 putFloat/getFloat 读写。 */
|
||||
class FloatBufferWrapperConvertor : ObjectBufferWrapperConvertor<Float>(
|
||||
ByteBufferWrapper::putFloat,
|
||||
ByteBufferWrapper::getFloat,
|
||||
Float::class.java,
|
||||
)
|
||||
|
||||
/** [BigInteger] 类型转换器,使用 putBigInteger/getBigInteger 读写。 */
|
||||
class BigIntegerBufferWrapperConvertor : ObjectBufferWrapperConvertor<BigInteger>(
|
||||
ByteBufferWrapper::putBigInteger,
|
||||
ByteBufferWrapper::getBigInteger,
|
||||
BigInteger::class.java,
|
||||
)
|
||||
|
||||
/** [BigDecimal] 类型转换器,使用 putBigDecimal/getBigDecimal 读写。 */
|
||||
class BigDecimalBufferWrapperConvertor : ObjectBufferWrapperConvertor<BigDecimal>(
|
||||
ByteBufferWrapper::putBigDecimal,
|
||||
ByteBufferWrapper::getBigDecimal,
|
||||
BigDecimal::class.java,
|
||||
)
|
||||
|
||||
/** [Boolean] 类型转换器,使用 putBoolean/getBoolean 读写。 */
|
||||
class BooleanBufferWrapperConvertor : ObjectBufferWrapperConvertor<Boolean>(
|
||||
ByteBufferWrapper::putBoolean,
|
||||
ByteBufferWrapper::getBoolean,
|
||||
Boolean::class.java,
|
||||
)
|
||||
|
||||
/** [String] 类型转换器,使用 putString/getString 读写。 */
|
||||
class StringBufferWrapperConvertor : ObjectBufferWrapperConvertor<String>(
|
||||
ByteBufferWrapper::putString,
|
||||
ByteBufferWrapper::getString,
|
||||
String::class.java,
|
||||
)
|
||||
|
||||
/** [ByteFlags] 类型转换器,使用 putFlag/getFlag 读写。 */
|
||||
class ByteFlagsBufferWrapperConvertor : ObjectBufferWrapperConvertor<ByteFlags>(
|
||||
ByteBufferWrapper::putFlag,
|
||||
ByteBufferWrapper::getFlag,
|
||||
@@ -177,42 +256,62 @@ class ByteFlagsBufferWrapperConvertor : ObjectBufferWrapperConvertor<ByteFlags>(
|
||||
)
|
||||
|
||||
|
||||
/** Java [JUUID] 类型转换器,使用 putUUID/getJUUID 读写。 */
|
||||
class JUUIDBufferWrapperConvertor : ObjectBufferWrapperConvertor<JUUID>(
|
||||
ByteBufferWrapper::putUUID,
|
||||
ByteBufferWrapper::getJUUID,
|
||||
JUUID::class.java,
|
||||
)
|
||||
|
||||
/** 自定义 [UUID] 类型转换器,使用 putUUID/getUUID 读写。 */
|
||||
class UUIDBufferWrapperConvertor : ObjectBufferWrapperConvertor<UUID>(
|
||||
ByteBufferWrapper::putUUID,
|
||||
ByteBufferWrapper::getUUID,
|
||||
UUID::class.java,
|
||||
)
|
||||
|
||||
/** [DateTime] 类型转换器,使用 putDateTime/getDateTime 读写。 */
|
||||
class DateTimeFlagsBufferWrapperConvertor : ObjectBufferWrapperConvertor<DateTime>(
|
||||
ByteBufferWrapper::putDateTime,
|
||||
ByteBufferWrapper::getDateTime,
|
||||
DateTime::class.java,
|
||||
)
|
||||
|
||||
/** [LocalDateTime] 类型转换器,使用 putLocalDateTime/getLocalDateTime 读写。 */
|
||||
class LocalDateTimeBufferWrapperConvertor : ObjectBufferWrapperConvertor<LocalDateTime>(
|
||||
ByteBufferWrapper::putLocalDateTime,
|
||||
ByteBufferWrapper::getLocalDateTime,
|
||||
LocalDateTime::class.java,
|
||||
)
|
||||
|
||||
/** [Date] 类型转换器,使用 putDate/getDate 读写。 */
|
||||
class DateBufferWrapperConvertor : ObjectBufferWrapperConvertor<Date>(
|
||||
ByteBufferWrapper::putDate,
|
||||
ByteBufferWrapper::getDate,
|
||||
Date::class.java,
|
||||
)
|
||||
|
||||
/**
|
||||
* 基于读写函数构造的通用数组转换器。
|
||||
*
|
||||
* 读取时要求额外参数的第一个元素为 Int 类型的数组长度。
|
||||
*
|
||||
* @param T 该转换器处理的数组类型
|
||||
* @param write 写入函数,将数组写入缓冲区
|
||||
* @param read 读取函数,按指定长度从缓冲区读取数组
|
||||
* @param typer 该转换器处理的数组类型对应的 Class 对象
|
||||
*/
|
||||
abstract class ArrayBufferWrapperConvertor<T : Any>(
|
||||
val write: ByteBufferWrapper.(obj: T) -> Unit,
|
||||
val read: ByteBufferWrapper.(size: Int) -> T,
|
||||
val typer: Class<T>,
|
||||
) : ByteBufferWrapperConvertor<T>() {
|
||||
override fun write(obj: T, byteBufferWrapper: ByteBufferWrapper) = write.invoke(byteBufferWrapper, obj)
|
||||
/**
|
||||
* 从缓冲区读取数组,要求 parameters[0] 为 Int 类型的长度。
|
||||
*
|
||||
* @throws IllegalArgumentException 如果参数为空或第一个参数不是 Int
|
||||
*/
|
||||
override fun read(byteBufferWrapper: ByteBufferWrapper, parameters: Array<Any>): T {
|
||||
if (parameters.isEmpty()) {
|
||||
throw IllegalArgumentException("parameters cannot be empty, must has a Int")
|
||||
@@ -226,42 +325,49 @@ abstract class ArrayBufferWrapperConvertor<T : Any>(
|
||||
override fun getType(): Class<T> = typer
|
||||
}
|
||||
|
||||
/** [ByteArray] 类型转换器,使用 putByteArray/getByteArray 读写。 */
|
||||
class ByteArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<ByteArray>(
|
||||
ByteBufferWrapper::putByteArray,
|
||||
ByteBufferWrapper::getByteArray,
|
||||
ByteArray::class.java,
|
||||
)
|
||||
|
||||
/** [CharArray] 类型转换器,使用 putCharArray/getCharArray 读写。 */
|
||||
class CharArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<CharArray>(
|
||||
ByteBufferWrapper::putCharArray,
|
||||
ByteBufferWrapper::getCharArray,
|
||||
CharArray::class.java,
|
||||
)
|
||||
|
||||
/** [ShortArray] 类型转换器,使用 putShortArray/getShortArray 读写。 */
|
||||
class ShortArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<ShortArray>(
|
||||
ByteBufferWrapper::putShortArray,
|
||||
ByteBufferWrapper::getShortArray,
|
||||
ShortArray::class.java,
|
||||
)
|
||||
|
||||
/** [IntArray] 类型转换器,使用 putIntArray/getIntArray 读写。 */
|
||||
class IntArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<IntArray>(
|
||||
ByteBufferWrapper::putIntArray,
|
||||
ByteBufferWrapper::getIntArray,
|
||||
IntArray::class.java,
|
||||
)
|
||||
|
||||
/** [LongArray] 类型转换器,使用 putLongArray/getLongArray 读写。 */
|
||||
class LongArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<LongArray>(
|
||||
ByteBufferWrapper::putLongArray,
|
||||
ByteBufferWrapper::getLongArray,
|
||||
LongArray::class.java,
|
||||
)
|
||||
|
||||
/** [FloatArray] 类型转换器,使用 putFloatArray/getFloatArray 读写。 */
|
||||
class FloatArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<FloatArray>(
|
||||
ByteBufferWrapper::putFloatArray,
|
||||
ByteBufferWrapper::getFloatArray,
|
||||
FloatArray::class.java,
|
||||
)
|
||||
|
||||
/** [DoubleArray] 类型转换器,使用 putDoubleArray/getDoubleArray 读写。 */
|
||||
class DoubleArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<DoubleArray>(
|
||||
ByteBufferWrapper::putDoubleArray,
|
||||
ByteBufferWrapper::getDoubleArray,
|
||||
@@ -269,6 +375,7 @@ class DoubleArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<DoubleArra
|
||||
)
|
||||
|
||||
|
||||
/** [BooleanArray] 类型转换器,使用 putBooleanArray/getBooleanArray 读写。 */
|
||||
class BooleanArrayBufferWrapperConvertor : ArrayBufferWrapperConvertor<BooleanArray>(
|
||||
ByteBufferWrapper::putBooleanArray,
|
||||
ByteBufferWrapper::getBooleanArray,
|
||||
|
||||
@@ -295,4 +295,12 @@ fun ByteBuffer.putVarInt(value: Int): ByteBuffer = this.putVarLong(value.toLong(
|
||||
fun ByteBuffer.putVarShort(value: Short): ByteBuffer = this.putVarLong(value.toLong())
|
||||
|
||||
|
||||
/**
|
||||
* 将 [ByteBuffer] 中剩余的全部字节写入指定的输出流。
|
||||
*
|
||||
* 此操作会读取从当前位置到 limit 之间的所有字节,并推进 position。
|
||||
*
|
||||
* @param outputStream 目标输出流
|
||||
* @throws IOException 如果写入过程中发生 I/O 错误
|
||||
*/
|
||||
fun ByteBuffer.writeStream(outputStream: OutputStream) = outputStream.write(this.toByteArray())
|
||||
|
||||
@@ -26,23 +26,56 @@ import java.io.OutputStream
|
||||
import java.io.Serializable
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* 可转换为字节数组的对象接口。
|
||||
*
|
||||
* 实现该接口的对象可以将自身序列化为字节数组,并支持写入输出流或 [ByteBuffer]。
|
||||
*/
|
||||
interface ByteConvertible : Serializable {
|
||||
|
||||
/**
|
||||
* 将当前对象转换为字节数组。
|
||||
*
|
||||
* @return 表示当前对象的字节数组
|
||||
*/
|
||||
fun toBytes(): ByteArray
|
||||
|
||||
/**
|
||||
* 将当前对象转换后的字节数组写入指定的输出流。
|
||||
*
|
||||
* @param outputStream 目标输出流
|
||||
*/
|
||||
fun writeToStream(outputStream: OutputStream) {
|
||||
outputStream.write(toBytes())
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前对象转换后的字节数组写入指定的 [ByteBuffer]。
|
||||
*
|
||||
* @param byteBuffer 目标缓冲区
|
||||
*/
|
||||
fun putToByteBuffer(byteBuffer: ByteBuffer) {
|
||||
byteBuffer.put(toBytes())
|
||||
}
|
||||
|
||||
/**
|
||||
* 伴生对象,提供面向输出流和缓冲区的便捷扩展函数。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 将 [ByteConvertible] 对象写入当前输出流。
|
||||
*
|
||||
* @param byteConvertible 要写入的可转换对象
|
||||
*/
|
||||
fun OutputStream.write(byteConvertible: ByteConvertible) {
|
||||
byteConvertible.writeToStream(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 [ByteConvertible] 对象写入当前 [ByteBuffer]。
|
||||
*
|
||||
* @param byteConvertible 要写入的可转换对象
|
||||
*/
|
||||
fun ByteBuffer.put(byteConvertible: ByteConvertible) {
|
||||
byteConvertible.putToByteBuffer(this)
|
||||
}
|
||||
|
||||
@@ -413,6 +413,9 @@ data class ByteFlags(
|
||||
return "ByteFlags(${toBinaryString()} = ${toHexString()})"
|
||||
}
|
||||
|
||||
/**
|
||||
* 伴生对象,提供 [ByteFlags] 的多种静态工厂方法。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 创建一个所有位均为 `true` 的 [ByteFlags] 实例。
|
||||
@@ -456,6 +459,15 @@ data class ByteFlags(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从布尔数组创建 [ByteFlags] 实例。
|
||||
*
|
||||
* 使用数组的前 8 个元素(索引 0~7),分别对应 b0~b7。
|
||||
*
|
||||
* @param booleanArray 布尔数组(长度至少为 8)
|
||||
* @return 对应的 ByteFlags 实例
|
||||
* @throws IllegalArgumentException 如果数组长度不足 8
|
||||
*/
|
||||
fun fromBooleanArray(booleanArray: BooleanArray): ByteFlags {
|
||||
return ByteFlags().fromBooleanArray(booleanArray)
|
||||
}
|
||||
|
||||
@@ -28,11 +28,24 @@ import com.mingliqiye.utils.json.api.JSONA
|
||||
import java.io.*
|
||||
|
||||
|
||||
/**
|
||||
* 通过 Java 序列化机制对当前对象进行深克隆。
|
||||
*
|
||||
* @param T 对象类型,必须实现 [Serializable]
|
||||
* @return 深克隆后的新对象
|
||||
*/
|
||||
@JvmName("_inline_deepClone")
|
||||
inline fun <reified T : Serializable> T.deepClone(): T {
|
||||
return deepClone(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 JSON 序列化与反序列化对当前对象进行深克隆。
|
||||
*
|
||||
* @param T 对象类型
|
||||
* @return 深克隆后的新对象
|
||||
* @throws JsonException 如果 JSON 转换过程中发生异常
|
||||
*/
|
||||
inline fun <reified T> T.deepJsonClone(): T {
|
||||
try {
|
||||
return JSONA.convert(this as Any, this!!.javaClass) as T
|
||||
@@ -43,6 +56,16 @@ inline fun <reified T> T.deepJsonClone(): T {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 Java 序列化机制对对象进行深克隆。
|
||||
*
|
||||
* 将对象序列化到内存后再反序列化,得到一个全新的对象副本。
|
||||
*
|
||||
* @param T 对象类型,必须实现 [Serializable]
|
||||
* @param obj 要克隆的对象
|
||||
* @return 深克隆后的新对象
|
||||
* @throws RuntimeException 如果序列化或反序列化过程中发生 I/O 异常或类找不到
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Serializable?> deepClone(obj: T): T {
|
||||
try {
|
||||
|
||||
@@ -987,6 +987,14 @@ inline fun <reified T> toArray(list: List<T>): Array<T> {
|
||||
.toArray(T::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任意类型的 List 转换为指定类型的数组。
|
||||
*
|
||||
* @param T 元素类型
|
||||
* @param list 输入的 List
|
||||
* @param clazz 数组元素类型对应的 Class 对象
|
||||
* @return 转换后的数组
|
||||
*/
|
||||
fun <T> toArray(list: List<T>, clazz: Class<T>): Array<T> {
|
||||
return SuperStream.of<T>(list)
|
||||
.toArray(clazz)
|
||||
|
||||
@@ -39,6 +39,9 @@ class IsChanged<T> {
|
||||
*/
|
||||
private val atomicReferenceData: AtomicReference<T> = AtomicReference()
|
||||
|
||||
/**
|
||||
* 无参构造函数,初始数据为 null
|
||||
*/
|
||||
constructor()
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,6 +63,9 @@ private fun getGetterName(fieldName: String): String {
|
||||
*/
|
||||
open class ConfigurationProps {
|
||||
|
||||
/**
|
||||
* 伴生对象,提供根据命令行参数初始化配置类实例的静态方法。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 初始化配置类实例,并根据命令行参数填充其字段。
|
||||
@@ -241,6 +244,9 @@ open class ConfigurationProps {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印配置项帮助信息,列出所有标注了 ConfigurationProp 且允许显示帮助的字段名称、类型和描述。
|
||||
*/
|
||||
fun printHelp() {
|
||||
val fields = this::class.java.declaredFields
|
||||
val help = StringBuilder()
|
||||
@@ -258,6 +264,9 @@ open class ConfigurationProps {
|
||||
println(help)
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前配置对象所有声明字段的名称与值的映射。
|
||||
*/
|
||||
val fields: Map<String, Any?>
|
||||
get() {
|
||||
val fields = this::class.java.declaredFields
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示编码或解码过程中发生的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class CodecException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 JSON 内容为空时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class EmptyJsonException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 HTTP 相关异常的基类,继承自 [MingLiUtilsBaseException]。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
open class HttpException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 HTTP URL 无效或处理失败时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
open class HttpUrlException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 JSON 内容无效或不合法时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class InvalidJsonException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 JSON 对象类型转换失败时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class JsonObjectCastException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 JSON 解析过程中发生的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class JsonParserException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
@@ -30,6 +38,14 @@ class JsonParserException @JvmOverloads constructor(
|
||||
) : JsonException(message, throwable, enableSuppression, writableStackTrace)
|
||||
|
||||
|
||||
/**
|
||||
* 表示 JSON 格式不正确时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class JsonFormatException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示 JSON 数据类型不匹配时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class JsonTypeException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* mingli-utils 工具库所有异常的基类,继承自 [RuntimeException]。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param cause 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
open class MingLiUtilsBaseException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
cause: Throwable? = null,
|
||||
|
||||
@@ -22,6 +22,14 @@
|
||||
|
||||
package com.mingliqiye.utils.exception
|
||||
|
||||
/**
|
||||
* 表示请求的资源未找到时抛出的异常。
|
||||
*
|
||||
* @param message 异常信息,默认为 null
|
||||
* @param throwable 异常原因,默认为 null
|
||||
* @param enableSuppression 是否启用异常抑制,默认为 false
|
||||
* @param writableStackTrace 是否可写入堆栈跟踪信息,默认为 true
|
||||
*/
|
||||
class NotFoundResourceException @JvmOverloads constructor(
|
||||
message: String? = null,
|
||||
throwable: Throwable? = null,
|
||||
|
||||
@@ -24,151 +24,381 @@
|
||||
|
||||
package com.mingliqiye.utils.functions
|
||||
|
||||
/**
|
||||
* 对任意对象执行无参函数调用,执行完成后返回接收者自身。
|
||||
*
|
||||
* @param function 要执行的无参函数
|
||||
* @return 接收者自身
|
||||
*/
|
||||
fun <T> T.with(function: Function): T = function.call().let {
|
||||
this
|
||||
}
|
||||
|
||||
/**
|
||||
* 以接收者自身作为参数执行函数调用,执行完成后返回接收者自身。
|
||||
*
|
||||
* @param function 接收一个参数的函数,调用时传入接收者自身
|
||||
* @return 接收者自身
|
||||
*/
|
||||
fun <T> T.with(function: P1Function<T>): T = function.call(this).let {
|
||||
this
|
||||
}
|
||||
|
||||
/**
|
||||
* 无参数且无返回值的函数式接口。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface Function {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*/
|
||||
fun call()
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 1 个参数且无返回值的函数式接口。
|
||||
*
|
||||
* @param P 参数类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P1Function<P> {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*
|
||||
* @param p 参数
|
||||
*/
|
||||
fun call(p: P)
|
||||
}
|
||||
|
||||
/**
|
||||
* 无参数且带返回值的函数式接口。
|
||||
*
|
||||
* @param R 返回值类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface RFunction<R> {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 1 个参数且带返回值的函数式接口。
|
||||
*
|
||||
* @param P 参数类型
|
||||
* @param R 返回值类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P1RFunction<P, R> {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*
|
||||
* @param p 参数
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 2 个参数且无返回值的函数式接口。
|
||||
*
|
||||
* @param P 第 1 个参数类型
|
||||
* @param P1 第 2 个参数类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P2Function<P, P1> {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*
|
||||
* @param p 第 1 个参数
|
||||
* @param p1 第 2 个参数
|
||||
*/
|
||||
fun call(p: P, p1: P1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 2 个参数且带返回值的函数式接口。
|
||||
*
|
||||
* @param P 第 1 个参数类型
|
||||
* @param P1 第 2 个参数类型
|
||||
* @param R 返回值类型
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P2RFunction<P, P1, R> {
|
||||
/**
|
||||
* 执行函数调用。
|
||||
*
|
||||
* @param p 第 1 个参数
|
||||
* @param p1 第 2 个参数
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 3 个参数且无返回值的函数式接口,参数类型依次为 P、P1、P2。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P3Function<P, P1, P2> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1、p2。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 3 个参数且带返回值的函数式接口,参数类型依次为 P、P1、P2,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P3RFunction<P, P1, P2, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1、p2。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 4 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P3。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P4Function<P, P1, P2, P3> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p3。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 4 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P3,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P4RFunction<P, P1, P2, P3, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p3。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 5 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P4。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P5Function<P, P1, P2, P3, P4> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p4。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 5 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P4,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P5RFunction<P, P1, P2, P3, P4, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p4。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 6 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P5。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P6Function<P, P1, P2, P3, P4, P5> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p5。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 6 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P5,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P6RFunction<P, P1, P2, P3, P4, P5, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p5。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 7 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P6。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P7Function<P, P1, P2, P3, P4, P5, P6> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p6。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 7 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P6,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P7RFunction<P, P1, P2, P3, P4, P5, P6, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p6。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 8 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P7。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P8Function<P, P1, P2, P3, P4, P5, P6, P7> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p7。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 8 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P7,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P8RFunction<P, P1, P2, P3, P4, P5, P6, P7, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p7。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 9 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P8。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P9Function<P, P1, P2, P3, P4, P5, P6, P7, P8> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p8。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 9 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P8,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P9RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p8。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 10 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P9。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P10Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p9。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 10 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P9,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P10RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p9。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 11 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P10。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P11Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p10。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 11 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P10,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P11RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p10。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 12 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P11。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P12Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p11。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 12 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P11,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P12RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p11。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 13 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P12。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P13Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p12。
|
||||
*/
|
||||
fun call(p: P, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 13 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P12,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P13RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p12。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -186,8 +416,14 @@ fun interface P13RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 14 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P13。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P14Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p13。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -206,8 +442,16 @@ fun interface P14Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 14 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P13,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P14RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p13。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -226,8 +470,14 @@ fun interface P14RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 15 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P14。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P15Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p14。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -247,8 +497,16 @@ fun interface P15Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 15 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P14,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P15RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p14。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -268,8 +526,14 @@ fun interface P15RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 16 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P15。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P16Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p15。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -290,8 +554,16 @@ fun interface P16Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 16 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P15,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P16RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p15。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -312,8 +584,14 @@ fun interface P16RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 17 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P16。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P17Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p16。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -335,8 +613,16 @@ fun interface P17Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 17 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P16,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P17RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p16。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -358,8 +644,14 @@ fun interface P17RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 18 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P17。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P18Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p17。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -382,8 +674,16 @@ fun interface P18Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 18 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P17,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P18RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p17。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -406,8 +706,14 @@ fun interface P18RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 19 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P18。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P19Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p18。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -431,8 +737,16 @@ fun interface P19Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 19 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P18,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P19RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p18。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -456,8 +770,14 @@ fun interface P19RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 20 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P19。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P20Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p19。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -482,8 +802,16 @@ fun interface P20Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 20 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P19,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P20RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p19。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -508,8 +836,14 @@ fun interface P20RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 21 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P20。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P21Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p20。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -535,8 +869,16 @@ fun interface P21Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 21 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P20,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P21RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p20。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -562,8 +904,14 @@ fun interface P21RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 22 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P21。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P22Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p21。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -590,8 +938,16 @@ fun interface P22Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 22 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P21,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P22RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p21。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -618,8 +974,14 @@ fun interface P22RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 23 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P22。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P23Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p22。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -647,8 +1009,16 @@ fun interface P23Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 23 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P22,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P23RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p22。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -676,8 +1046,14 @@ fun interface P23RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
): R
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 24 个参数且无返回值的函数式接口,参数类型依次为 P、P1 至 P23。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P24Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p23。
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
@@ -706,8 +1082,16 @@ fun interface P24Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收 24 个参数且带返回值的函数式接口,参数类型依次为 P、P1 至 P23,返回值类型为 R。
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface P24RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22, P23, R> {
|
||||
/**
|
||||
* 执行函数调用,参数依次为 p、p1 至 p23。
|
||||
*
|
||||
* @return 函数执行结果
|
||||
*/
|
||||
fun call(
|
||||
p: P,
|
||||
p1: P1,
|
||||
|
||||
@@ -22,10 +22,22 @@
|
||||
|
||||
package com.mingliqiye.utils.http
|
||||
|
||||
/**
|
||||
* HTTP 响应基类,封装时间、消息、数据和状态码等通用响应字段。
|
||||
*
|
||||
* 除 [time] 外字段均可变,本身非线程安全;
|
||||
* 如需不可变更新语义,请使用子类的 with* 系列方法。
|
||||
*
|
||||
* @param T 响应数据的类型
|
||||
*/
|
||||
open class BaseResponse<T>(
|
||||
/** 响应时间(格式化的字符串),创建后不可修改 */
|
||||
open val time: String,
|
||||
/** 响应消息,可通过 setter 修改 */
|
||||
open var message: String,
|
||||
/** 响应数据,可通过 setter 修改 */
|
||||
open var data: T,
|
||||
/** HTTP 状态码,可通过 setter 修改 */
|
||||
open var statusCode: Int,
|
||||
|
||||
) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -26,4 +26,9 @@ package com.mingliqiye.utils.http
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
|
||||
|
||||
/**
|
||||
* 将本枚举转换为 OkHttp 的 [okhttp3.MediaType]。
|
||||
*
|
||||
* @return 与 [MediaType.value] 对应的 OkHttp 媒体类型
|
||||
*/
|
||||
fun MediaType.okhttp(): okhttp3.MediaType = this.value.toMediaType()
|
||||
|
||||
@@ -53,7 +53,15 @@ data class PageData<T>(
|
||||
*/
|
||||
val pageIndex: Long = 0,
|
||||
) {
|
||||
/** 伴生对象,提供工厂方法 */
|
||||
companion object {
|
||||
/**
|
||||
* 从 MyBatis-Plus 的分页对象构建分页数据
|
||||
*
|
||||
* @param T 记录类型
|
||||
* @param page MyBatis-Plus 分页对象
|
||||
* @return 转换后的分页数据
|
||||
*/
|
||||
fun <T> of(page: Page<T>): PageData<T> = PageData(
|
||||
records = page.records,
|
||||
totalCount = page.total,
|
||||
|
||||
@@ -25,6 +25,18 @@ package com.mingliqiye.utils.http
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||
import com.mingliqiye.utils.time.DateTime
|
||||
|
||||
/**
|
||||
* 分页 HTTP 响应数据类,数据部分为 [PageData] 分页结构。
|
||||
*
|
||||
* 继承 [BaseResponse],支持链式调用与 with* 不可变更新,
|
||||
* 并提供多种工厂方法创建成功/错误响应。
|
||||
*
|
||||
* @param T 分页记录的类型
|
||||
* @property time 响应时间(格式化的字符串)
|
||||
* @property message 响应消息
|
||||
* @property data 分页响应数据
|
||||
* @property statusCode HTTP 状态码
|
||||
*/
|
||||
data class PagedResponse<T>(
|
||||
override val time: String, // 响应时间,一旦创建不可修改
|
||||
override var message: String, // 响应消息,可通过setter修改
|
||||
@@ -36,6 +48,7 @@ data class PagedResponse<T>(
|
||||
data,
|
||||
statusCode
|
||||
) {
|
||||
/** 伴生对象,提供各类分页响应的工厂方法 */
|
||||
companion object {
|
||||
/**
|
||||
* 创建一个默认的成功响应对象
|
||||
|
||||
@@ -49,6 +49,7 @@ data class Response<T>(
|
||||
statusCode,
|
||||
) {
|
||||
|
||||
/** 伴生对象,提供各类响应的工厂方法 */
|
||||
companion object {
|
||||
/**
|
||||
* 创建一个默认的成功响应对象
|
||||
|
||||
@@ -300,6 +300,23 @@ enum class Method {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在 [Request.Builder] 上设置 POST 方法,使用空请求体。
|
||||
*
|
||||
* @return 当前 [Request.Builder],便于链式调用
|
||||
*/
|
||||
fun Request.Builder.post() = post(RequestBody.EMPTY)
|
||||
|
||||
/**
|
||||
* 在 [Request.Builder] 上设置 PUT 方法,使用空请求体。
|
||||
*
|
||||
* @return 当前 [Request.Builder],便于链式调用
|
||||
*/
|
||||
fun Request.Builder.put() = put(RequestBody.EMPTY)
|
||||
|
||||
/**
|
||||
* 在 [Request.Builder] 上设置 PATCH 方法,使用空请求体。
|
||||
*
|
||||
* @return 当前 [Request.Builder],便于链式调用
|
||||
*/
|
||||
fun Request.Builder.patch() = patch(RequestBody.EMPTY)
|
||||
|
||||
@@ -28,16 +28,37 @@ import java.io.OutputStream
|
||||
import java.nio.charset.Charset
|
||||
|
||||
|
||||
/**
|
||||
* 读取输入流中的全部字节并按指定字符集解码为字符串。
|
||||
*
|
||||
* @param charset 字符编码,默认为 [Charsets.UTF_8]
|
||||
* @return 解码后的字符串
|
||||
*/
|
||||
fun InputStream.readAllText(charset: Charset = Charsets.UTF_8) = this.readAllBytes().toString(charset)
|
||||
|
||||
/**
|
||||
* 将输入流中的全部字节导出到指定输出流,并刷新输出流。
|
||||
*
|
||||
* @param out 目标输出流
|
||||
*/
|
||||
fun InputStream.exportBytes(out: OutputStream) {
|
||||
out.write(this.readAllBytes())
|
||||
out.flush()
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取输入流中的全部字节并转换为字节列表。
|
||||
*
|
||||
* @return 包含全部字节的列表
|
||||
*/
|
||||
fun InputStream.readToList(): List<Byte> = this.readBytes().toList()
|
||||
|
||||
|
||||
/**
|
||||
* 读取输入流中的全部字节。
|
||||
*
|
||||
* @return 包含全部字节的字节数组
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
@JvmName("readInputStreamAllBytes")
|
||||
fun InputStream.readAllBytes(): ByteArray = this.readBytes()
|
||||
|
||||
@@ -44,10 +44,24 @@ object SIO {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展函数:条件不满足时对当前对象执行给定变换函数
|
||||
*
|
||||
* @param boolean 条件值,为 true 时直接返回原对象
|
||||
* @param pd 条件为 false 时执行的变换函数
|
||||
* @return 原对象或变换后的对象
|
||||
*/
|
||||
fun <T> T.ifOr(boolean: Boolean, pd: T.() -> T): T = if (boolean) {
|
||||
this
|
||||
} else pd(this)
|
||||
|
||||
/**
|
||||
* 扩展函数:条件满足时对当前对象执行给定变换函数
|
||||
*
|
||||
* @param boolean 条件值,为 false 时直接返回原对象
|
||||
* @param pd 条件为 true 时执行的变换函数
|
||||
* @return 原对象或变换后的对象
|
||||
*/
|
||||
fun <T> T.ifOrElse(boolean: Boolean, pd: T.() -> T): T = if (!boolean) {
|
||||
this
|
||||
} else pd(this)
|
||||
|
||||
@@ -34,22 +34,48 @@ import tools.jackson.databind.node.JsonNodeType
|
||||
import tools.jackson.databind.node.ObjectNode
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* 基于Jackson3的JSON处理实现类,提供JSON字符串解析、格式化、合并、节点操作等功能。
|
||||
*
|
||||
* 节点路径操作([getNodeValue]、[updateNodeValue])使用JSON Pointer风格路径(如"/user/name")。
|
||||
* ObjectMapper支持直接持有,也支持通过get/set回调间接访问外部实例。
|
||||
*/
|
||||
class Jackson3JsonApi : JsonApi {
|
||||
|
||||
/**
|
||||
* ObjectMapper的获取回调,默认返回内部持有的实例
|
||||
*/
|
||||
var get: () -> ObjectMapper = { _prObjectMapper!! }
|
||||
/**
|
||||
* ObjectMapper的设置回调,默认写入内部持有的实例
|
||||
*/
|
||||
var set: (ObjectMapper) -> Unit = { _prObjectMapper = it }
|
||||
|
||||
private var _prObjectMapper: ObjectMapper? = null
|
||||
|
||||
|
||||
/**
|
||||
* 当前使用的ObjectMapper实例,读写均通过[get]/[set]回调转发
|
||||
*/
|
||||
var objectMapper: ObjectMapper
|
||||
get() = get()
|
||||
private set(value) = set(value)
|
||||
|
||||
/**
|
||||
* 使用指定的ObjectMapper构造实例
|
||||
*
|
||||
* @param objectMapper 自定义的ObjectMapper实例
|
||||
*/
|
||||
constructor(objectMapper: ObjectMapper) {
|
||||
_prObjectMapper = objectMapper
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用获取/设置回调构造实例,ObjectMapper由外部管理
|
||||
*
|
||||
* @param get ObjectMapper的获取回调
|
||||
* @param set ObjectMapper的设置回调
|
||||
*/
|
||||
constructor(
|
||||
get: () -> ObjectMapper,
|
||||
set: (ObjectMapper) -> Unit,
|
||||
@@ -58,6 +84,9 @@ class Jackson3JsonApi : JsonApi {
|
||||
this.set = set
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用默认的ObjectMapper构造实例
|
||||
*/
|
||||
constructor() {
|
||||
_prObjectMapper = ObjectMapper()
|
||||
}
|
||||
@@ -105,6 +134,9 @@ class Jackson3JsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并多个JSON对象字符串;无参数时返回"{}",遇到非对象JSON时抛出JsonException
|
||||
*/
|
||||
override fun merge(vararg jsons: String): String {
|
||||
try {
|
||||
if (jsons.isEmpty()) return "{}"
|
||||
@@ -126,6 +158,9 @@ class Jackson3JsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定路径的节点值,路径为JSON Pointer风格(如"/user/name"),节点缺失或为null时返回null
|
||||
*/
|
||||
override fun getNodeValue(json: String, path: String): String? {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
@@ -141,6 +176,9 @@ class Jackson3JsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新指定路径的节点值,路径为JSON Pointer风格(如"/user/name"),空路径时替换整个根节点
|
||||
*/
|
||||
override fun updateNodeValue(json: String, path: String, newValue: Any): String {
|
||||
try {
|
||||
val rootNode = objectMapper.readTree(json)
|
||||
@@ -212,6 +250,9 @@ class Jackson3JsonApi : JsonApi {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为JsonItem对象,解析失败时包装为JsonException抛出
|
||||
*/
|
||||
override fun parseJsonItem(json: String): JsonItem {
|
||||
try {
|
||||
return getJsonItem(objectMapper.readTree(json))
|
||||
@@ -222,8 +263,21 @@ class Jackson3JsonApi : JsonApi {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基于Jackson3的JsonItem标记实现类,通过伴生对象工厂方法创建具体的JsonItem
|
||||
*/
|
||||
class JackSon3JsonItem : JsonItem {
|
||||
/**
|
||||
* 伴生对象,提供JsonNode到JsonItem的工厂方法
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 根据JsonNode的节点类型创建对应的JsonItem实例
|
||||
*
|
||||
* @param node Jackson3的JsonNode节点
|
||||
* @return 与节点类型对应的JsonItem实例
|
||||
* @throws IllegalStateException 当节点类型未知时抛出
|
||||
*/
|
||||
fun getJsonItem(node: JsonNode): JsonItem {
|
||||
return when (node.nodeType) {
|
||||
JsonNodeType.OBJECT -> JackSon3JsonObject(node)
|
||||
@@ -238,11 +292,31 @@ class JackSon3JsonItem : JsonItem {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于Jackson3节点的JSON数字类型
|
||||
*
|
||||
* @param node 数字类型的JsonNode节点
|
||||
*/
|
||||
class JackSon3JsonNumber(node: JsonNode) : JsonNumber(node.asString())
|
||||
/**
|
||||
* 基于Jackson3节点的JSON布尔类型
|
||||
*
|
||||
* @param node 布尔类型的JsonNode节点
|
||||
*/
|
||||
class JackSon3JsonBoolean(node: JsonNode) : JsonBoolean(node.asBoolean())
|
||||
/**
|
||||
* 基于Jackson3节点的JSON字符串类型
|
||||
*
|
||||
* @param node 字符串类型的JsonNode节点
|
||||
*/
|
||||
class JackSon3JsonString(node: JsonNode) : JsonString(node.asString())
|
||||
|
||||
|
||||
/**
|
||||
* 基于Jackson3节点的JSON数组类型
|
||||
*
|
||||
* @property node 数组类型的JsonNode节点
|
||||
*/
|
||||
class JackSon3JsonArray(val node: JsonNode) : JsonArray() {
|
||||
override fun getIndex(index: Int): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(index))
|
||||
@@ -264,6 +338,11 @@ class JackSon3JsonArray(val node: JsonNode) : JsonArray() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于Jackson3节点的JSON对象类型
|
||||
*
|
||||
* @property node 对象类型的JsonNode节点
|
||||
*/
|
||||
class JackSon3JsonObject(val node: JsonNode) : JsonObject() {
|
||||
override fun getField(name: String): JsonItem {
|
||||
return JackSon3JsonItem.getJsonItem(node.get(name))
|
||||
|
||||
@@ -49,6 +49,11 @@ class JacksonJsonApi : JsonApi {
|
||||
this.objectMapper = ObjectMapper()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内部使用的ObjectMapper实例
|
||||
*
|
||||
* @return 当前的ObjectMapper实例
|
||||
*/
|
||||
fun getObjectMapper() = objectMapper
|
||||
|
||||
/**
|
||||
@@ -113,6 +118,13 @@ class JacksonJsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON字符串,非ASCII字符以Unicode转义形式输出
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @return 格式化后的JSON字符串
|
||||
* @throws JsonException 当格式化失败时抛出异常
|
||||
*/
|
||||
fun formatUnicode(obj: Any): String {
|
||||
return try {
|
||||
objectMapper.writer().with(JsonGenerator.Feature.ESCAPE_NON_ASCII).writeValueAsString(obj)
|
||||
@@ -137,6 +149,13 @@ class JacksonJsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化(带缩进)的JSON字符串,非ASCII字符以Unicode转义形式输出
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @return 美化后的JSON字符串
|
||||
* @throws JsonException 当格式化失败时抛出异常
|
||||
*/
|
||||
fun formatPrettyUnicode(obj: Any): String {
|
||||
return try {
|
||||
objectMapper.writerWithDefaultPrettyPrinter().with(JsonGenerator.Feature.ESCAPE_NON_ASCII)
|
||||
@@ -287,6 +306,9 @@ class JacksonJsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JSON字符串解析为JsonItem对象,解析失败时包装为JsonException抛出
|
||||
*/
|
||||
override fun parseJsonItem(json: String): JsonItem {
|
||||
try {
|
||||
return getJsonItem(objectMapper.readTree(json))
|
||||
@@ -296,8 +318,21 @@ class JacksonJsonApi : JsonApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于Jackson2的JsonItem标记实现类,通过伴生对象工厂方法创建具体的JsonItem
|
||||
*/
|
||||
class JackSonJsonItem : JsonItem {
|
||||
/**
|
||||
* 伴生对象,提供JsonNode到JsonItem的工厂方法
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 根据JsonNode的节点类型创建对应的JsonItem实例
|
||||
*
|
||||
* @param node Jackson2的JsonNode节点
|
||||
* @return 与节点类型对应的JsonItem实例
|
||||
* @throws IllegalStateException 当节点类型未知时抛出
|
||||
*/
|
||||
fun getJsonItem(node: JsonNode): JsonItem {
|
||||
return when (node.nodeType) {
|
||||
JsonNodeType.OBJECT -> JackSonJsonObject(node)
|
||||
@@ -312,11 +347,31 @@ class JackSonJsonItem : JsonItem {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于Jackson2节点的JSON数字类型
|
||||
*
|
||||
* @param node 数字类型的JsonNode节点
|
||||
*/
|
||||
class JackSonJsonNumber(node: JsonNode) : JsonNumber(node.asText())
|
||||
/**
|
||||
* 基于Jackson2节点的JSON布尔类型
|
||||
*
|
||||
* @param node 布尔类型的JsonNode节点
|
||||
*/
|
||||
class JackSonJsonBoolean(node: JsonNode) : JsonBoolean(node.asBoolean())
|
||||
/**
|
||||
* 基于Jackson2节点的JSON字符串类型
|
||||
*
|
||||
* @param node 字符串类型的JsonNode节点
|
||||
*/
|
||||
class JackSonJsonString(node: JsonNode) : JsonString(node.asText())
|
||||
|
||||
|
||||
/**
|
||||
* 基于Jackson2节点的JSON数组类型
|
||||
*
|
||||
* @property node 数组类型的JsonNode节点
|
||||
*/
|
||||
class JackSonJsonArray(val node: JsonNode) : JsonArray() {
|
||||
override fun getIndex(index: Int): JsonItem {
|
||||
return getJsonItem(node.get(index))
|
||||
@@ -338,6 +393,11 @@ class JackSonJsonArray(val node: JsonNode) : JsonArray() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于Jackson2节点的JSON对象类型
|
||||
*
|
||||
* @property node 对象类型的JsonNode节点
|
||||
*/
|
||||
class JackSonJsonObject(val node: JsonNode) : JsonObject() {
|
||||
override fun getField(name: String): JsonItem {
|
||||
return getJsonItem(node.get(name))
|
||||
|
||||
@@ -38,6 +38,9 @@ import kotlin.io.path.readBytes
|
||||
*/
|
||||
interface JsonApi {
|
||||
|
||||
/**
|
||||
* 伴生对象,提供基于 reified 泛型参数的便捷解析与转换扩展函数
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 将JSON字符串解析为指定泛型类型对象
|
||||
@@ -342,46 +345,106 @@ interface JsonApi {
|
||||
*/
|
||||
fun formatPretty(obj: Any): String
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化格式的JSON字节数组
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @return 格式化后的美化JSON字节数组
|
||||
*/
|
||||
fun formatPrettyBytes(obj: Any): ByteArray {
|
||||
return formatPretty(obj)!!.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化JSON并写入指定路径的文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件路径
|
||||
*/
|
||||
fun formatPretty(obj: Any, file: String) {
|
||||
formatPretty(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化JSON并写入指定路径的文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件路径对象
|
||||
*/
|
||||
fun formatPretty(obj: Any, file: Path) {
|
||||
formatPretty(obj, file.toFile())
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化JSON并写入指定文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件对象
|
||||
*/
|
||||
fun formatPretty(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
formatPretty(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为美化JSON并写入输出流
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param stream 目标输出流
|
||||
*/
|
||||
fun formatPretty(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyBytes(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON字节数组
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @return 格式化后的JSON字节数组
|
||||
*/
|
||||
fun formatBytes(obj: Any): ByteArray {
|
||||
return format(obj).toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON并写入指定路径的文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件路径
|
||||
*/
|
||||
fun format(obj: Any, file: String) {
|
||||
format(obj, Paths.get(file))
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON并写入指定路径的文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件路径对象
|
||||
*/
|
||||
fun format(obj: Any, file: Path) {
|
||||
format(obj, file.toFile())
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON并写入指定文件
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param file 目标文件对象
|
||||
*/
|
||||
fun format(obj: Any, file: File) {
|
||||
FileOutputStream(file).use { fos ->
|
||||
format(obj, fos)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象格式化为JSON并写入输出流
|
||||
*
|
||||
* @param obj 待格式化的对象
|
||||
* @param stream 目标输出流
|
||||
*/
|
||||
fun format(obj: Any, stream: OutputStream) {
|
||||
stream.write(formatPrettyBytes(obj))
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ import java.util.*
|
||||
*/
|
||||
abstract class JsonTypeReference<T> : Comparable<JsonTypeReference<T>> {
|
||||
|
||||
/**
|
||||
* 当前引用的类型信息,子类可覆写以自定义类型
|
||||
*/
|
||||
open var type: Type = Any::class.java
|
||||
|
||||
/**
|
||||
@@ -68,6 +71,12 @@ abstract class JsonTypeReference<T> : Comparable<JsonTypeReference<T>> {
|
||||
* @return 类型引用实例
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 根据 reified 泛型参数创建类型引用
|
||||
*
|
||||
* @param <T> 目标类型
|
||||
* @return 类型引用实例
|
||||
*/
|
||||
inline fun <reified T> of(): JsonTypeReference<T> {
|
||||
return object : JsonTypeReference<T>() {}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,12 @@ import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
import java.lang.reflect.WildcardType
|
||||
|
||||
/**
|
||||
* 参数化类型的简单实现,用于在运行时构造并描述带泛型参数的类型
|
||||
*
|
||||
* @property rawClass 原始类型(如 List::class.java)
|
||||
* @param paramType 泛型参数类型列表
|
||||
*/
|
||||
class TypeReference(
|
||||
val rawClass: Type,
|
||||
vararg paramType: Type,
|
||||
@@ -40,12 +46,24 @@ class TypeReference(
|
||||
override fun getOwnerType(): Type? = null
|
||||
|
||||
|
||||
/**
|
||||
* 返回"原始类型<参数1,参数2>"形式的类型描述字符串
|
||||
*/
|
||||
override fun toString(): String = "${getRawString(rawClass)}<${
|
||||
",".join(typeArguments) { getRawString(it) }
|
||||
}>"
|
||||
|
||||
/**
|
||||
* 伴生对象,提供类型描述与TypeReference的工厂方法
|
||||
*/
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* 获取类型的完整描述字符串,支持通配符与参数化类型
|
||||
*
|
||||
* @param type 类型对象
|
||||
* @return 类型的描述字符串
|
||||
*/
|
||||
fun getRawString(type: Type): String {
|
||||
return when (type) {
|
||||
is Class<*> -> type.name
|
||||
@@ -73,10 +91,23 @@ class TypeReference(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 reified 泛型参数创建TypeReference
|
||||
*
|
||||
* @param <T> 目标类型
|
||||
* @return TypeReference实例
|
||||
*/
|
||||
inline fun <reified T> of(): TypeReference {
|
||||
return of((object : JsonTypeReference<T>() {}).type)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任意Type转换为TypeReference
|
||||
*
|
||||
* @param type 类型对象,支持Class、ParameterizedType和WildcardType
|
||||
* @return TypeReference实例
|
||||
* @throws IllegalStateException 当类型无法识别时抛出
|
||||
*/
|
||||
fun of(type: Type): TypeReference {
|
||||
return when (type) {
|
||||
is Class<*> -> TypeReference(type)
|
||||
|
||||
@@ -33,7 +33,16 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
|
||||
import com.mingliqiye.utils.time.DateTime
|
||||
import com.mingliqiye.utils.time.Formatter
|
||||
|
||||
/**
|
||||
* [DateTime] 的 JSON 字符串转换器。
|
||||
*
|
||||
* 根据字段上的 [DateTimeJsonFormat] 注解选择格式化器,
|
||||
* 在日期时间对象与 JSON 字符串之间进行双向转换。
|
||||
*/
|
||||
class DateTimeJsonConverter : BaseJsonConverter<DateTime> {
|
||||
/**
|
||||
* 按 [DateTimeJsonFormat] 注解指定的格式将日期时间写为 JSON 字符串,null 时写入 null
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
override fun convert(
|
||||
obj: DateTime?,
|
||||
@@ -56,6 +65,11 @@ class DateTimeJsonConverter : BaseJsonConverter<DateTime> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 按 [DateTimeJsonFormat] 注解指定的格式从 JSON 字符串解析日期时间
|
||||
*
|
||||
* @throws JsonParseException 当节点不是字符串类型时抛出
|
||||
*/
|
||||
override fun deConvert(jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter): DateTime? {
|
||||
val dateTimeJsonFormat: DateTimeJsonFormat? = annotationGetter.getAnnotation<DateTimeJsonFormat>()
|
||||
if (jsonReader.isNull) return null
|
||||
|
||||
@@ -44,6 +44,9 @@ import com.mingliqiye.utils.network.NetworkEndpoint
|
||||
* @see NetworkEndpoint
|
||||
*/
|
||||
class NetworkEndpointJsonConverter : BaseJsonConverter<NetworkEndpoint> {
|
||||
/**
|
||||
* 将网络端点写为 `host:port` 形式的 JSON 字符串,null 时写入 null
|
||||
*/
|
||||
override fun convert(
|
||||
obj: NetworkEndpoint?,
|
||||
jsonWriter: BaseJsonWriter,
|
||||
@@ -52,6 +55,11 @@ class NetworkEndpointJsonConverter : BaseJsonConverter<NetworkEndpoint> {
|
||||
if (obj == null) jsonWriter.setNull() else jsonWriter.setString(obj.toHostPortString())
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 `host:port` 形式的 JSON 字符串还原网络端点
|
||||
*
|
||||
* @throws JsonParseException 当节点不是字符串类型时抛出
|
||||
*/
|
||||
override fun deConvert(
|
||||
jsonReader: BaseJsonReader,
|
||||
annotationGetter: AnnotationGetter,
|
||||
|
||||
@@ -34,8 +34,17 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
|
||||
import com.mingliqiye.utils.uuid.UUID
|
||||
import com.mingliqiye.utils.uuid.UUIDFormatType
|
||||
|
||||
/**
|
||||
* [UUID] 的 JSON 字符串转换器。
|
||||
*
|
||||
* 根据字段上的 [UUIDJsonFormat] 注解选择进制与格式,
|
||||
* 在 UUID 对象与 JSON 字符串之间进行双向转换。
|
||||
*/
|
||||
class UUIDJsonConverter : BaseJsonConverter<UUID> {
|
||||
|
||||
/**
|
||||
* 按 [UUIDJsonFormat] 注解指定的进制与格式将 UUID 写为 JSON 字符串,null 时写入 null
|
||||
*/
|
||||
override fun convert(
|
||||
obj: UUID?,
|
||||
jsonWriter: BaseJsonWriter,
|
||||
@@ -60,6 +69,11 @@ class UUIDJsonConverter : BaseJsonConverter<UUID> {
|
||||
jsonWriter.setString(uuid)
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 [UUIDJsonFormat] 注解指定的进制从 JSON 字符串解析 UUID
|
||||
*
|
||||
* @throws JsonParseException 当节点不是字符串类型时抛出
|
||||
*/
|
||||
override fun deConvert(
|
||||
jsonReader: BaseJsonReader,
|
||||
annotationGetter: AnnotationGetter,
|
||||
|
||||
@@ -22,6 +22,15 @@
|
||||
|
||||
package com.mingliqiye.utils.json.converters.base
|
||||
|
||||
/**
|
||||
* 注解获取接口,用于在自定义序列化/反序列化时读取字段上的注解
|
||||
*/
|
||||
interface AnnotationGetter {
|
||||
/**
|
||||
* 获取指定类型的注解
|
||||
*
|
||||
* @param clazz 注解的Class类型
|
||||
* @return 注解实例,不存在时返回null(默认实现始终返回null)
|
||||
*/
|
||||
fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = null
|
||||
}
|
||||
|
||||
@@ -34,15 +34,42 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
|
||||
*/
|
||||
interface BaseJsonConverter<T> {
|
||||
|
||||
/**
|
||||
* 序列化:将对象转换并写入 JSON
|
||||
*
|
||||
* @param obj 待写入的对象,可能为null
|
||||
* @param jsonWriter JSON写入器
|
||||
* @param annotationGetter 注解获取器,用于读取字段上的格式注解
|
||||
* @throws Exception 当写入失败时抛出
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
fun convert(obj: T?, jsonWriter: BaseJsonWriter, annotationGetter: AnnotationGetter)
|
||||
|
||||
/**
|
||||
* 反序列化:从 JSON 读取并还原对象
|
||||
*
|
||||
* @param jsonReader JSON读取器
|
||||
* @param annotationGetter 注解获取器,用于读取字段上的格式注解
|
||||
* @return 还原后的对象,JSON为null时返回null
|
||||
* @throws Exception 当读取失败时抛出
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
fun deConvert(jsonReader: BaseJsonReader, annotationGetter: AnnotationGetter): T?
|
||||
|
||||
/**
|
||||
* 获取本转换器处理的目标类型引用
|
||||
*
|
||||
* @return 目标类型的JsonTypeReference
|
||||
* @throws Exception 当获取类型失败时抛出
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
fun getType(): JsonTypeReference<T>
|
||||
|
||||
/**
|
||||
* 获取本转换器处理的目标类型的原始Class
|
||||
*
|
||||
* @return 目标类型的Class对象
|
||||
*/
|
||||
@Throws(Exception::class)
|
||||
fun getClazz() = getType().getRawType()
|
||||
}
|
||||
|
||||
@@ -29,16 +29,32 @@ import tools.jackson.databind.DeserializationContext
|
||||
import tools.jackson.databind.ValueDeserializer
|
||||
|
||||
|
||||
/**
|
||||
* Jackson3 反序列化适配器,将 [BaseJsonConverter] 包装为 Jackson3 的 [ValueDeserializer]。
|
||||
*
|
||||
* @property property 当前属性信息,用于读取字段注解,可为null
|
||||
* @property jsonConverter 实际执行反序列化逻辑的转换器
|
||||
*/
|
||||
class JackJson3Deserializer<T>(
|
||||
val property: BeanProperty? = null,
|
||||
val jsonConverter: BaseJsonConverter<T>,
|
||||
) : ValueDeserializer<T>() {
|
||||
|
||||
/**
|
||||
* 注解获取器,从当前属性信息中读取字段注解
|
||||
*/
|
||||
val annotationGetter =
|
||||
object : AnnotationGetter {
|
||||
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化方法:将 JSON 树交给转换器还原为目标对象。
|
||||
*
|
||||
* @param p JSON 解析器。
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @return 还原后的对象,可能为null。
|
||||
*/
|
||||
override fun deserialize(
|
||||
p: JsonParser,
|
||||
ctxt: DeserializationContext?,
|
||||
|
||||
@@ -29,20 +29,43 @@ import tools.jackson.databind.SerializationContext
|
||||
import tools.jackson.databind.ValueSerializer
|
||||
|
||||
|
||||
/**
|
||||
* Jackson3 序列化适配器,将 [BaseJsonConverter] 包装为 Jackson3 的 [ValueSerializer]。
|
||||
*
|
||||
* @property property 当前属性信息,用于读取字段注解,可为null
|
||||
* @property jsonConverter 实际执行序列化逻辑的转换器
|
||||
*/
|
||||
class JackJson3Serializer<T>(
|
||||
val property: BeanProperty? = null,
|
||||
val jsonConverter: BaseJsonConverter<T>,
|
||||
) : ValueSerializer<T>() {
|
||||
|
||||
/**
|
||||
* 注解获取器,从当前属性信息中读取字段注解
|
||||
*/
|
||||
val annotationGetter =
|
||||
object : AnnotationGetter {
|
||||
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化方法:将源对象交给转换器写入 JSON。
|
||||
*
|
||||
* @param value 源对象。
|
||||
* @param gen JSON 生成器,用于写入 JSON 数据。
|
||||
* @param ctxt 序列化上下文。
|
||||
*/
|
||||
override fun serialize(value: T, gen: JsonGenerator, ctxt: SerializationContext) {
|
||||
jsonConverter.convert(value, JackSon3JsonWriter(gen, ctxt), annotationGetter)
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param ctxt 序列化上下文,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 携带属性信息的新序列化器实例。
|
||||
*/
|
||||
override fun createContextual(ctxt: SerializationContext?, property: BeanProperty?): ValueSerializer<*> {
|
||||
return JackJson3Serializer(property, jsonConverter)
|
||||
}
|
||||
|
||||
@@ -29,17 +29,33 @@ import com.fasterxml.jackson.databind.JsonDeserializer
|
||||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
|
||||
import com.mingliqiye.utils.json.reader.JackSonJsonReader
|
||||
|
||||
/**
|
||||
* Jackson2 反序列化适配器,将 [BaseJsonConverter] 包装为 Jackson2 的 [JsonDeserializer]。
|
||||
*
|
||||
* @property property 当前属性信息,用于读取字段注解,可为null
|
||||
* @property jsonConverter 实际执行反序列化逻辑的转换器
|
||||
*/
|
||||
class JackJsonDeserializer<T>(
|
||||
val property: BeanProperty? = null,
|
||||
val jsonConverter: BaseJsonConverter<T>,
|
||||
) : JsonDeserializer<T>(), ContextualDeserializer {
|
||||
|
||||
|
||||
/**
|
||||
* 注解获取器,从当前属性信息中读取字段注解
|
||||
*/
|
||||
val annotationGetter =
|
||||
object : AnnotationGetter {
|
||||
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化方法:将 JSON 树交给转换器还原为目标对象。
|
||||
*
|
||||
* @param p JSON 解析器。
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @return 还原后的对象,可能为null。
|
||||
*/
|
||||
override fun deserialize(
|
||||
p: JsonParser,
|
||||
ctxt: DeserializationContext?,
|
||||
@@ -47,6 +63,13 @@ class JackJsonDeserializer<T>(
|
||||
return jsonConverter.deConvert(JackSonJsonReader(p.readValueAsTree()), annotationGetter)
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文感知方法:设置当前属性信息。
|
||||
*
|
||||
* @param ctxt 反序列化上下文,可选参数。
|
||||
* @param property 当前属性信息。
|
||||
* @return 携带属性信息的新反序列化器实例。
|
||||
*/
|
||||
override fun createContextual(
|
||||
ctxt: DeserializationContext?,
|
||||
property: BeanProperty?,
|
||||
|
||||
@@ -29,11 +29,20 @@ import com.fasterxml.jackson.databind.SerializerProvider
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer
|
||||
import com.mingliqiye.utils.json.witer.JackSonJsonWriter
|
||||
|
||||
/**
|
||||
* Jackson2 序列化适配器,将 [BaseJsonConverter] 包装为 Jackson2 的 [JsonSerializer]。
|
||||
*
|
||||
* @property property 当前属性信息,用于读取字段注解,可为null
|
||||
* @property jsonConverter 实际执行序列化逻辑的转换器
|
||||
*/
|
||||
class JackJsonSerializer<T>(
|
||||
val property: BeanProperty?,
|
||||
val jsonConverter: BaseJsonConverter<T>,
|
||||
) : JsonSerializer<T>(), ContextualSerializer {
|
||||
|
||||
/**
|
||||
* 注解获取器,从当前属性信息中读取字段注解
|
||||
*/
|
||||
val annotationGetter = object : AnnotationGetter {
|
||||
override fun <T : Annotation> getAnnotation(clazz: Class<T>): T? = property?.getAnnotation(clazz)
|
||||
}
|
||||
|
||||
@@ -32,21 +32,43 @@ import tools.jackson.core.JsonGenerator as JackSon3JsonGenerator
|
||||
import tools.jackson.databind.module.SimpleModule as JackSon3SimpleModule
|
||||
|
||||
|
||||
/**
|
||||
* 将转换器包装为 Jackson2 的 SimpleModule,注册对应的序列化器与反序列化器。
|
||||
*
|
||||
* @return 注册了当前转换器的 Jackson2 模块
|
||||
*/
|
||||
fun <T> BaseJsonConverter<T>.getJacksonModule(): JackSon2SimpleModule =
|
||||
JackSon2SimpleModule("${getClazz().simpleName}Module")
|
||||
.addSerializer(getClazz(), JackJsonSerializer(null, this))
|
||||
.addDeserializer(getClazz(), JackJsonDeserializer(null, this))
|
||||
|
||||
/**
|
||||
* 将转换器包装为 Jackson3 的 SimpleModule,注册对应的序列化器与反序列化器。
|
||||
*
|
||||
* @return 注册了当前转换器的 Jackson3 模块
|
||||
*/
|
||||
fun <T> BaseJsonConverter<T>.getJackson3Module(): JackSon3SimpleModule =
|
||||
JackSon3SimpleModule("${getClazz().simpleName}Module")
|
||||
.addSerializer(getClazz(), JackJson3Serializer(null, this))
|
||||
.addDeserializer(getClazz(), JackJson3Deserializer(null, this))
|
||||
|
||||
|
||||
/**
|
||||
* 根据 reified 泛型参数获取指定类型的注解。
|
||||
*
|
||||
* @param T 注解类型
|
||||
* @return 注解实例,不存在时返回null
|
||||
*/
|
||||
inline fun <reified T : Annotation> AnnotationGetter.getAnnotation(): T? =
|
||||
getAnnotation(T::class.java)
|
||||
|
||||
|
||||
/**
|
||||
* 按数值的实际类型写入 JSON 数字(Jackson3),支持常见数值类型。
|
||||
*
|
||||
* @param value 待写入的数值
|
||||
* @return 当前 JsonGenerator 实例,便于链式调用
|
||||
*/
|
||||
fun JackSon3JsonGenerator.writeNumber(value: Number): JackSon3JsonGenerator {
|
||||
when (value) {
|
||||
is Long -> this.writeNumber(value)
|
||||
@@ -61,6 +83,12 @@ fun JackSon3JsonGenerator.writeNumber(value: Number): JackSon3JsonGenerator {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 按数值的实际类型写入 JSON 数字(Jackson2),支持常见数值类型。
|
||||
*
|
||||
* @param value 待写入的数值
|
||||
* @return 当前 JsonGenerator 实例,便于链式调用
|
||||
*/
|
||||
fun JackSon2JsonGenerator.writeNumber(value: Number): JackSon2JsonGenerator {
|
||||
when (value) {
|
||||
is Long -> this.writeNumber(value)
|
||||
|
||||
@@ -25,6 +25,11 @@ package com.mingliqiye.utils.json.judgment
|
||||
import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment
|
||||
import tools.jackson.databind.JsonNode
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonNode 的类型判断实现,各判定属性直接委托给节点自身
|
||||
*
|
||||
* @param treeNode 待判断的 Jackson3 JsonNode 节点
|
||||
*/
|
||||
open class JackSon3ReaderJudgment(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonJudgment {
|
||||
|
||||
@@ -25,6 +25,11 @@ package com.mingliqiye.utils.json.judgment
|
||||
import com.fasterxml.jackson.databind.JsonNode
|
||||
import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonNode 的类型判断实现,各判定属性直接委托给节点自身
|
||||
*
|
||||
* @param treeNode 待判断的 Jackson2 JsonNode 节点
|
||||
*/
|
||||
open class JackSonReaderJudgment(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonJudgment {
|
||||
|
||||
@@ -22,20 +22,30 @@
|
||||
|
||||
package com.mingliqiye.utils.json.judgment.base
|
||||
|
||||
/**
|
||||
* JSON 节点类型判断接口,提供当前节点的类型判定属性,默认实现均返回 false
|
||||
*/
|
||||
interface BaseJsonJudgment {
|
||||
/** 当前节点是否为 JSON 对象 */
|
||||
val isObject: Boolean
|
||||
get() = false
|
||||
/** 当前节点是否为 JSON 数组 */
|
||||
val isArray: Boolean
|
||||
get() = false
|
||||
/** 当前节点是否为字符串 */
|
||||
val isString: Boolean
|
||||
get() = false
|
||||
/** 当前节点是否为数字 */
|
||||
val isNumber: Boolean
|
||||
get() = false
|
||||
/** 当前节点是否为布尔值 */
|
||||
val isBoolean: Boolean
|
||||
get() = false
|
||||
|
||||
/** 当前节点是否为 null */
|
||||
val isNull: Boolean
|
||||
get() = false
|
||||
/** 当前节点是否不为 null */
|
||||
val isNotNull: Boolean
|
||||
get() = !isNull
|
||||
}
|
||||
|
||||
@@ -29,6 +29,11 @@ import tools.jackson.databind.JsonNode
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonNode 的 JSON 数组读取器实现,按索引读取各类型值
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson3 JsonNode 数组节点
|
||||
*/
|
||||
class JackSon3JsonArrayReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonArrayReader, JackSon3ReaderJudgment(treeNode) {
|
||||
|
||||
@@ -29,6 +29,11 @@ import tools.jackson.databind.JsonNode
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonNode 的 JSON 对象读取器实现,按字段名读取各类型值
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson3 JsonNode 对象节点
|
||||
*/
|
||||
class JackSon3JsonObjectReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonObjectReader, JackSon3ReaderJudgment(treeNode) {
|
||||
|
||||
@@ -30,6 +30,11 @@ import tools.jackson.databind.JsonNode
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonNode 的 JSON 读取器实现,将节点值读取为各种类型
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson3 JsonNode 节点
|
||||
*/
|
||||
class JackSon3JsonReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonReader, JackSon3ReaderJudgment(treeNode) {
|
||||
|
||||
@@ -29,6 +29,11 @@ import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonNode 的 JSON 数组读取器实现,按索引读取各类型值
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson2 JsonNode 数组节点
|
||||
*/
|
||||
class JackSonJsonArrayReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonArrayReader, JackSonReaderJudgment(treeNode) {
|
||||
|
||||
@@ -29,6 +29,11 @@ import com.mingliqiye.utils.json.reader.base.BaseJsonObjectReader
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonNode 的 JSON 对象读取器实现,按字段名读取各类型值
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson2 JsonNode 对象节点
|
||||
*/
|
||||
class JackSonJsonObjectReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonObjectReader, JackSonReaderJudgment(treeNode) {
|
||||
|
||||
@@ -30,6 +30,11 @@ import com.mingliqiye.utils.json.reader.base.BaseJsonReader
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonNode 的 JSON 读取器实现,将节点值读取为各种类型
|
||||
*
|
||||
* @param treeNode 待读取的 Jackson2 JsonNode 节点
|
||||
*/
|
||||
class JackSonJsonReader(
|
||||
private val treeNode: JsonNode,
|
||||
) : BaseJsonReader, JackSonReaderJudgment(treeNode) {
|
||||
|
||||
@@ -26,53 +26,142 @@ import com.mingliqiye.utils.exception.JsonParserException
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* JSON 数组读取器接口,按索引读取数组中的各类型值。
|
||||
*
|
||||
* 直接读取当前节点的 getXxx 方法默认抛出 [JsonParserException],应使用 getIndexXxx 系列方法。
|
||||
*/
|
||||
interface BaseJsonArrayReader : BaseJsonReader {
|
||||
/** 数组读取器不支持直接读取为对象,调用时抛出 [JsonParserException] */
|
||||
override fun getObject(): BaseJsonObjectReader =
|
||||
throw JsonParserException("not supported getObject on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为数组,调用时抛出 [JsonParserException] */
|
||||
override fun getArray(): BaseJsonArrayReader =
|
||||
throw JsonParserException("not supported getArray on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为字符串,调用时抛出 [JsonParserException] */
|
||||
override fun getString(): String =
|
||||
throw JsonParserException("not supported getString on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为布尔值,调用时抛出 [JsonParserException] */
|
||||
override fun getBoolean(): Boolean =
|
||||
throw JsonParserException("not supported getBoolean on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Byte,调用时抛出 [JsonParserException] */
|
||||
override fun getByte(): Byte =
|
||||
throw JsonParserException("not supported getByte on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Short,调用时抛出 [JsonParserException] */
|
||||
override fun getShort(): Short =
|
||||
throw JsonParserException("not supported getShort on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Int,调用时抛出 [JsonParserException] */
|
||||
override fun getInt(): Int =
|
||||
throw JsonParserException("not supported getInt on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Long,调用时抛出 [JsonParserException] */
|
||||
override fun getLong(): Long =
|
||||
throw JsonParserException("not supported getLong on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Float,调用时抛出 [JsonParserException] */
|
||||
override fun getFloat(): Float =
|
||||
throw JsonParserException("not supported getFloat on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 Double,调用时抛出 [JsonParserException] */
|
||||
override fun getDouble(): Double =
|
||||
throw JsonParserException("not supported getDouble on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 BigInteger,调用时抛出 [JsonParserException] */
|
||||
override fun getBigInteger(): BigInteger =
|
||||
throw JsonParserException("not supported getBigInteger on BaseJsonArrayReader")
|
||||
|
||||
/** 数组读取器不支持直接读取为 BigDecimal,调用时抛出 [JsonParserException] */
|
||||
override fun getBigDecimal(): BigDecimal =
|
||||
throw JsonParserException("not supported getBigDecimal on BaseJsonArrayReader")
|
||||
|
||||
/**
|
||||
* 读取指定索引的对象元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素对应的 JSON 对象读取器
|
||||
*/
|
||||
fun getIndexObject(key: Int): BaseJsonObjectReader
|
||||
/**
|
||||
* 读取指定索引的数组元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素对应的 JSON 数组读取器
|
||||
*/
|
||||
fun getIndexArray(key: Int): BaseJsonArrayReader
|
||||
/**
|
||||
* 读取指定索引的字符串元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的字符串值
|
||||
*/
|
||||
fun getIndexString(key: Int): String
|
||||
/**
|
||||
* 读取指定索引的布尔元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的布尔值
|
||||
*/
|
||||
fun getIndexBoolean(key: Int): Boolean
|
||||
/**
|
||||
* 读取指定索引的 Byte 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Byte 数值
|
||||
*/
|
||||
fun getIndexByte(key: Int): Byte
|
||||
/**
|
||||
* 读取指定索引的 Short 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Short 数值
|
||||
*/
|
||||
fun getIndexShort(key: Int): Short
|
||||
/**
|
||||
* 读取指定索引的 Int 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Int 数值
|
||||
*/
|
||||
fun getIndexInt(key: Int): Int
|
||||
/**
|
||||
* 读取指定索引的 Long 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Long 数值
|
||||
*/
|
||||
fun getIndexLong(key: Int): Long
|
||||
/**
|
||||
* 读取指定索引的 Float 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Float 数值
|
||||
*/
|
||||
fun getIndexFloat(key: Int): Float
|
||||
/**
|
||||
* 读取指定索引的 Double 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 Double 数值
|
||||
*/
|
||||
fun getIndexDouble(key: Int): Double
|
||||
/**
|
||||
* 读取指定索引的 BigInteger 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 BigInteger 数值
|
||||
*/
|
||||
fun getIndexBigInteger(key: Int): BigInteger
|
||||
/**
|
||||
* 读取指定索引的 BigDecimal 数值元素
|
||||
*
|
||||
* @param key 数组索引
|
||||
* @return 元素的 BigDecimal 数值
|
||||
*/
|
||||
fun getIndexBigDecimal(key: Int): BigDecimal
|
||||
}
|
||||
|
||||
@@ -26,57 +26,146 @@ import com.mingliqiye.utils.exception.JsonParserException
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* JSON 对象读取器接口,按字段名读取对象中的各类型值。
|
||||
*
|
||||
* 直接读取当前节点的 getXxx 方法默认抛出 [JsonParserException],应使用 getFieldXxx 系列方法。
|
||||
*/
|
||||
interface BaseJsonObjectReader : BaseJsonReader {
|
||||
override val isObject: Boolean
|
||||
get() = true
|
||||
|
||||
|
||||
/** 对象读取器不支持直接读取为对象,调用时抛出 [JsonParserException] */
|
||||
override fun getObject(): BaseJsonObjectReader =
|
||||
throw JsonParserException("not supported getObject on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为数组,调用时抛出 [JsonParserException] */
|
||||
override fun getArray(): BaseJsonArrayReader =
|
||||
throw JsonParserException("not supported getArray on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为字符串,调用时抛出 [JsonParserException] */
|
||||
override fun getString(): String =
|
||||
throw JsonParserException("not supported getString on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为布尔值,调用时抛出 [JsonParserException] */
|
||||
override fun getBoolean(): Boolean =
|
||||
throw JsonParserException("not supported getBoolean on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Byte,调用时抛出 [JsonParserException] */
|
||||
override fun getByte(): Byte =
|
||||
throw JsonParserException("not supported getByte on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Short,调用时抛出 [JsonParserException] */
|
||||
override fun getShort(): Short =
|
||||
throw JsonParserException("not supported getShort on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Int,调用时抛出 [JsonParserException] */
|
||||
override fun getInt(): Int =
|
||||
throw JsonParserException("not supported getInt on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Long,调用时抛出 [JsonParserException] */
|
||||
override fun getLong(): Long =
|
||||
throw JsonParserException("not supported getLong on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Float,调用时抛出 [JsonParserException] */
|
||||
override fun getFloat(): Float =
|
||||
throw JsonParserException("not supported getFloat on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 Double,调用时抛出 [JsonParserException] */
|
||||
override fun getDouble(): Double =
|
||||
throw JsonParserException("not supported getDouble on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 BigInteger,调用时抛出 [JsonParserException] */
|
||||
override fun getBigInteger(): BigInteger =
|
||||
throw JsonParserException("not supported getBigInteger on BaseJsonObjectReader")
|
||||
|
||||
/** 对象读取器不支持直接读取为 BigDecimal,调用时抛出 [JsonParserException] */
|
||||
override fun getBigDecimal(): BigDecimal =
|
||||
throw JsonParserException("not supported getBigDecimal on BaseJsonObjectReader")
|
||||
|
||||
/**
|
||||
* 读取指定字段的对象值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段对应的 JSON 对象读取器
|
||||
*/
|
||||
fun getFieldObject(key: String): BaseJsonObjectReader
|
||||
/**
|
||||
* 读取指定字段的数组值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段对应的 JSON 数组读取器
|
||||
*/
|
||||
fun getFieldArray(key: String): BaseJsonArrayReader
|
||||
/**
|
||||
* 读取指定字段的字符串值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的字符串值
|
||||
*/
|
||||
fun getFieldString(key: String): String
|
||||
/**
|
||||
* 读取指定字段的布尔值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的布尔值
|
||||
*/
|
||||
fun getFieldBoolean(key: String): Boolean
|
||||
/**
|
||||
* 读取指定字段的 Byte 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Byte 数值
|
||||
*/
|
||||
fun getFieldByte(key: String): Byte
|
||||
/**
|
||||
* 读取指定字段的 Short 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Short 数值
|
||||
*/
|
||||
fun getFieldShort(key: String): Short
|
||||
/**
|
||||
* 读取指定字段的 Int 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Int 数值
|
||||
*/
|
||||
fun getFieldInt(key: String): Int
|
||||
/**
|
||||
* 读取指定字段的 Long 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Long 数值
|
||||
*/
|
||||
fun getFieldLong(key: String): Long
|
||||
/**
|
||||
* 读取指定字段的 Float 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Float 数值
|
||||
*/
|
||||
fun getFieldFloat(key: String): Float
|
||||
/**
|
||||
* 读取指定字段的 Double 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 Double 数值
|
||||
*/
|
||||
fun getFieldDouble(key: String): Double
|
||||
/**
|
||||
* 读取指定字段的 BigInteger 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 BigInteger 数值
|
||||
*/
|
||||
fun getFieldBigInteger(key: String): BigInteger
|
||||
/**
|
||||
* 读取指定字段的 BigDecimal 数值
|
||||
*
|
||||
* @param key 字段名
|
||||
* @return 字段的 BigDecimal 数值
|
||||
*/
|
||||
fun getFieldBigDecimal(key: String): BigDecimal
|
||||
}
|
||||
|
||||
@@ -26,18 +26,81 @@ import com.mingliqiye.utils.json.judgment.base.BaseJsonJudgment
|
||||
import java.math.BigDecimal
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* JSON 读取器基础接口,继承类型判断能力,定义将当前节点读取为各种类型值的方法
|
||||
*/
|
||||
interface BaseJsonReader : BaseJsonJudgment {
|
||||
/**
|
||||
* 将当前节点作为对象读取器返回
|
||||
*
|
||||
* @return JSON 对象读取器
|
||||
*/
|
||||
fun getObject(): BaseJsonObjectReader
|
||||
/**
|
||||
* 将当前节点作为数组读取器返回
|
||||
*
|
||||
* @return JSON 数组读取器
|
||||
*/
|
||||
fun getArray(): BaseJsonArrayReader
|
||||
/**
|
||||
* 读取当前节点的字符串值
|
||||
*
|
||||
* @return 字符串值
|
||||
*/
|
||||
fun getString(): String
|
||||
/**
|
||||
* 读取当前节点的布尔值
|
||||
*
|
||||
* @return 布尔值
|
||||
*/
|
||||
fun getBoolean(): Boolean
|
||||
/**
|
||||
* 读取当前节点的 Byte 数值
|
||||
*
|
||||
* @return Byte 数值
|
||||
*/
|
||||
fun getByte(): Byte
|
||||
/**
|
||||
* 读取当前节点的 Short 数值
|
||||
*
|
||||
* @return Short 数值
|
||||
*/
|
||||
fun getShort(): Short
|
||||
/**
|
||||
* 读取当前节点的 Int 数值
|
||||
*
|
||||
* @return Int 数值
|
||||
*/
|
||||
fun getInt(): Int
|
||||
/**
|
||||
* 读取当前节点的 Long 数值
|
||||
*
|
||||
* @return Long 数值
|
||||
*/
|
||||
fun getLong(): Long
|
||||
/**
|
||||
* 读取当前节点的 Float 数值
|
||||
*
|
||||
* @return Float 数值
|
||||
*/
|
||||
fun getFloat(): Float
|
||||
/**
|
||||
* 读取当前节点的 Double 数值
|
||||
*
|
||||
* @return Double 数值
|
||||
*/
|
||||
fun getDouble(): Double
|
||||
/**
|
||||
* 读取当前节点的 BigInteger 数值
|
||||
*
|
||||
* @return BigInteger 数值
|
||||
*/
|
||||
fun getBigInteger(): BigInteger
|
||||
/**
|
||||
* 读取当前节点的 BigDecimal 数值
|
||||
*
|
||||
* @return BigDecimal 数值
|
||||
*/
|
||||
fun getBigDecimal(): BigDecimal
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter
|
||||
import tools.jackson.core.JsonGenerator
|
||||
import tools.jackson.databind.SerializationContext
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonGenerator 的 JSON 数组写入器实现,向数组追加各类型元素
|
||||
*
|
||||
* @param jsonGenerator Jackson3 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文
|
||||
*/
|
||||
class JackSon3ArrayWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializationContext,
|
||||
|
||||
@@ -29,6 +29,12 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
|
||||
import tools.jackson.core.JsonGenerator
|
||||
import tools.jackson.databind.SerializationContext
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonGenerator 的 JSON 写入器实现,对象/数组写入时自动补全起止标记
|
||||
*
|
||||
* @param jsonGenerator Jackson3 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文
|
||||
*/
|
||||
class JackSon3JsonWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializationContext,
|
||||
|
||||
@@ -28,11 +28,23 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter
|
||||
import tools.jackson.core.JsonGenerator
|
||||
import tools.jackson.databind.SerializationContext
|
||||
|
||||
/**
|
||||
* 基于 Jackson3 JsonGenerator 的 JSON 对象写入器实现,按字段名追加各类型值
|
||||
*
|
||||
* @param jsonGenerator Jackson3 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文
|
||||
*/
|
||||
class JackSon3ObjectWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializationContext,
|
||||
) : BaseJsonObjectWriter {
|
||||
|
||||
/**
|
||||
* 写入字段名
|
||||
*
|
||||
* @param name 字段名
|
||||
* @return 当前 JsonGenerator 实例,便于链式调用
|
||||
*/
|
||||
fun writeName(name: String): JsonGenerator = jsonGenerator.writeName(name)
|
||||
|
||||
override fun addNull(name: String): BaseJsonObjectWriter {
|
||||
|
||||
@@ -28,6 +28,12 @@ import com.mingliqiye.utils.json.converters.base.writeNumber
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonGenerator 的 JSON 数组写入器实现,向数组追加各类型元素
|
||||
*
|
||||
* @param jsonGenerator Jackson2 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文,可为null
|
||||
*/
|
||||
class JackSonArrayWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializerProvider?,
|
||||
|
||||
@@ -29,6 +29,12 @@ import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonWriter
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonGenerator 的 JSON 写入器实现,对象/数组写入时自动补全起止标记
|
||||
*
|
||||
* @param jsonGenerator Jackson2 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文,可为null
|
||||
*/
|
||||
class JackSonJsonWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializerProvider?,
|
||||
|
||||
@@ -28,6 +28,12 @@ import com.mingliqiye.utils.json.converters.base.writeNumber
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonArrayWriter
|
||||
import com.mingliqiye.utils.json.witer.base.BaseJsonObjectWriter
|
||||
|
||||
/**
|
||||
* 基于 Jackson2 JsonGenerator 的 JSON 对象写入器实现,按字段名追加各类型值
|
||||
*
|
||||
* @param jsonGenerator Jackson2 的 JSON 生成器
|
||||
* @param serializationContext 序列化上下文,可为null
|
||||
*/
|
||||
class JackSonObjectWriter(
|
||||
private val jsonGenerator: JsonGenerator,
|
||||
private val serializationContext: SerializerProvider?,
|
||||
|
||||
@@ -24,35 +24,99 @@ package com.mingliqiye.utils.json.witer.base
|
||||
|
||||
import com.mingliqiye.utils.exception.JsonFormatException
|
||||
|
||||
/**
|
||||
* JSON 数组写入器接口,向数组中追加各类型元素,支持链式调用。
|
||||
*
|
||||
* 直接设置当前节点的 setXxx 方法默认抛出 [JsonFormatException],应使用 addXxx 系列方法。
|
||||
*/
|
||||
interface BaseJsonArrayWriter : BaseJsonWriter {
|
||||
|
||||
|
||||
/** 数组写入器不支持直接设置为对象,调用时抛出 [JsonFormatException] */
|
||||
override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) =
|
||||
throw JsonFormatException("setObject is not supported on BaseJsonArrayWriter")
|
||||
|
||||
/** 数组写入器不支持直接设置为数组,调用时抛出 [JsonFormatException] */
|
||||
override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) =
|
||||
throw JsonFormatException("setArray is not supported on BaseJsonArrayWriter")
|
||||
|
||||
/** 数组写入器不支持直接设置为字符串,调用时抛出 [JsonFormatException] */
|
||||
override fun setString(string: String) =
|
||||
throw JsonFormatException("setString is not supported on BaseJsonArrayWriter")
|
||||
|
||||
/** 数组写入器不支持直接设置为数值,调用时抛出 [JsonFormatException] */
|
||||
override fun setNumber(number: Number) =
|
||||
throw JsonFormatException("setNumber is not supported on BaseJsonArrayWriter")
|
||||
|
||||
/** 数组写入器不支持直接设置为布尔值,调用时抛出 [JsonFormatException] */
|
||||
override fun setBoolean(boolean: Boolean) =
|
||||
throw JsonFormatException("setBoolean is not supported on BaseJsonArrayWriter")
|
||||
|
||||
/** 数组写入器不支持直接设置为 null,调用时抛出 [JsonFormatException] */
|
||||
override fun setNull() = throw JsonFormatException("setNull is not supported on BaseJsonArrayWriter")
|
||||
override val isArray
|
||||
get() = true
|
||||
|
||||
/**
|
||||
* 追加 null 元素
|
||||
*
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addNull(): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加字符串元素
|
||||
*
|
||||
* @param value 字符串值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addString(value: String): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加数值元素
|
||||
*
|
||||
* @param value 数值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addNumber(value: Number): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加布尔元素
|
||||
*
|
||||
* @param value 布尔值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addBoolean(value: Boolean): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加对象元素,通过回调填充子对象
|
||||
*
|
||||
* @param value 子对象写入回调
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addObject(value: (BaseJsonObjectWriter) -> Unit): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加数组元素,通过回调填充子数组
|
||||
*
|
||||
* @param value 子数组写入回调
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun addArray(value: (BaseJsonArrayWriter) -> Unit): BaseJsonArrayWriter
|
||||
/**
|
||||
* 追加字符串元素,[addString] 的简写
|
||||
*
|
||||
* @param value 字符串值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun add(value: String): BaseJsonArrayWriter = addString(value)
|
||||
/**
|
||||
* 追加数值元素,[addNumber] 的简写
|
||||
*
|
||||
* @param value 数值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun add(value: Number): BaseJsonArrayWriter = addNumber(value)
|
||||
/**
|
||||
* 追加布尔元素,[addBoolean] 的简写
|
||||
*
|
||||
* @param value 布尔值
|
||||
* @return 当前数组写入器,便于链式调用
|
||||
*/
|
||||
fun add(value: Boolean): BaseJsonArrayWriter = addBoolean(value)
|
||||
}
|
||||
|
||||
@@ -24,36 +24,109 @@ package com.mingliqiye.utils.json.witer.base
|
||||
|
||||
import com.mingliqiye.utils.exception.JsonFormatException
|
||||
|
||||
/**
|
||||
* JSON 对象写入器接口,按字段名向对象中追加各类型值,支持链式调用。
|
||||
*
|
||||
* 直接设置当前节点的 setXxx 方法默认抛出 [JsonFormatException],应使用 addXxx 系列方法。
|
||||
*/
|
||||
interface BaseJsonObjectWriter : BaseJsonWriter {
|
||||
override val isObject: Boolean
|
||||
get() = true
|
||||
|
||||
/** 对象写入器不支持直接设置为对象,调用时抛出 [JsonFormatException] */
|
||||
override fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter) =
|
||||
throw JsonFormatException("setObject is not supported on BaseJsonObjectWriter")
|
||||
|
||||
/** 对象写入器不支持直接设置为数组,调用时抛出 [JsonFormatException] */
|
||||
override fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter) =
|
||||
throw JsonFormatException("setArray is not supported on BaseJsonObjectWriter")
|
||||
|
||||
/** 对象写入器不支持直接设置为字符串,调用时抛出 [JsonFormatException] */
|
||||
override fun setString(string: String) =
|
||||
throw JsonFormatException("setString is not supported on BaseJsonObjectWriter")
|
||||
|
||||
/** 对象写入器不支持直接设置为数值,调用时抛出 [JsonFormatException] */
|
||||
override fun setNumber(number: Number) =
|
||||
throw JsonFormatException("setNumber is not supported on BaseJsonObjectWriter")
|
||||
|
||||
/** 对象写入器不支持直接设置为布尔值,调用时抛出 [JsonFormatException] */
|
||||
override fun setBoolean(boolean: Boolean) =
|
||||
throw JsonFormatException("setBoolean is not supported on BaseJsonObjectWriter")
|
||||
|
||||
/** 对象写入器不支持直接设置为 null,调用时抛出 [JsonFormatException] */
|
||||
override fun setNull() = throw JsonFormatException("setNull is not supported on BaseJsonObjectWriter")
|
||||
/**
|
||||
* 追加值为 null 的字段
|
||||
*
|
||||
* @param name 字段名
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addNull(name: String): BaseJsonObjectWriter
|
||||
|
||||
/**
|
||||
* 追加字符串字段
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 字符串值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addString(name: String, value: String): BaseJsonObjectWriter
|
||||
/**
|
||||
* 追加数值字段
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 数值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addNumber(name: String, value: Number): BaseJsonObjectWriter
|
||||
/**
|
||||
* 追加布尔字段
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 布尔值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addBoolean(name: String, value: Boolean): BaseJsonObjectWriter
|
||||
/**
|
||||
* 追加对象字段,通过回调填充子对象
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param valueCaller 子对象写入回调
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addObject(name: String, valueCaller: (BaseJsonObjectWriter) -> Unit): BaseJsonObjectWriter
|
||||
|
||||
/**
|
||||
* 追加数组字段,通过回调填充数组元素
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param valueCaller 数组写入回调
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun addArray(name: String, valueCaller: (BaseJsonArrayWriter) -> Unit): BaseJsonObjectWriter
|
||||
/**
|
||||
* 追加字符串字段,[addString] 的简写
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 字符串值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun add(name: String, value: String): BaseJsonObjectWriter = addString(name, value)
|
||||
/**
|
||||
* 追加数值字段,[addNumber] 的简写
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 数值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun add(name: String, value: Number): BaseJsonObjectWriter = addNumber(name, value)
|
||||
/**
|
||||
* 追加布尔字段,[addBoolean] 的简写
|
||||
*
|
||||
* @param name 字段名
|
||||
* @param value 布尔值
|
||||
* @return 当前对象写入器,便于链式调用
|
||||
*/
|
||||
fun add(name: String, value: Boolean): BaseJsonObjectWriter = addBoolean(name, value)
|
||||
|
||||
}
|
||||
|
||||
@@ -25,11 +25,42 @@ package com.mingliqiye.utils.json.witer.base
|
||||
import com.mingliqiye.utils.json.judgment.BaseJsonJudgment
|
||||
|
||||
|
||||
/**
|
||||
* JSON 写入器基础接口,继承类型判断能力,定义将当前节点写为各种类型值的方法
|
||||
*/
|
||||
interface BaseJsonWriter : BaseJsonJudgment {
|
||||
/**
|
||||
* 写入一个 JSON 对象,通过回调填充对象字段
|
||||
*
|
||||
* @param objCall 对象写入回调,接收对象写入器并返回它
|
||||
*/
|
||||
fun setObject(objCall: (BaseJsonObjectWriter) -> BaseJsonObjectWriter)
|
||||
/**
|
||||
* 写入一个 JSON 数组,通过回调填充数组元素
|
||||
*
|
||||
* @param arrayCall 数组写入回调,接收数组写入器并返回它
|
||||
*/
|
||||
fun setArray(arrayCall: (BaseJsonArrayWriter) -> BaseJsonArrayWriter)
|
||||
/**
|
||||
* 写入字符串值
|
||||
*
|
||||
* @param string 待写入的字符串
|
||||
*/
|
||||
fun setString(string: String)
|
||||
/**
|
||||
* 写入数值
|
||||
*
|
||||
* @param number 待写入的数值
|
||||
*/
|
||||
fun setNumber(number: Number)
|
||||
/**
|
||||
* 写入布尔值
|
||||
*
|
||||
* @param boolean 待写入的布尔值
|
||||
*/
|
||||
fun setBoolean(boolean: Boolean)
|
||||
/**
|
||||
* 写入 null 值
|
||||
*/
|
||||
fun setNull()
|
||||
}
|
||||
|
||||
@@ -26,11 +26,20 @@ import org.slf4j.Logger
|
||||
import org.slf4j.Marker
|
||||
import org.slf4j.helpers.MessageFormatter
|
||||
|
||||
/**
|
||||
* 简易 SLF4J Logger 实现,将各级别日志直接输出到标准输出流。
|
||||
* 所有日志级别始终启用,消息格式遵循 SLF4J 的 {} 占位符风格。
|
||||
*
|
||||
* @param name 日志器名称,输出时作为标识显示
|
||||
*/
|
||||
class MingLiLogger(private val name: String) : Logger {
|
||||
override fun getName(): String {
|
||||
return name
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,TRACE 级别日志始终启用。
|
||||
*/
|
||||
override fun isTraceEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -67,6 +76,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,忽略 marker 参数。
|
||||
*/
|
||||
override fun isTraceEnabled(marker: Marker?): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -91,6 +103,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
trace(msg, t)
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,DEBUG 级别日志始终启用。
|
||||
*/
|
||||
override fun isDebugEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -127,6 +142,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,忽略 marker 参数。
|
||||
*/
|
||||
override fun isDebugEnabled(marker: Marker?): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -151,6 +169,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
debug(msg, t)
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,INFO 级别日志始终启用。
|
||||
*/
|
||||
override fun isInfoEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -187,6 +208,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,忽略 marker 参数。
|
||||
*/
|
||||
override fun isInfoEnabled(marker: Marker?): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -211,6 +235,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
info(msg, t)
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,WARN 级别日志始终启用。
|
||||
*/
|
||||
override fun isWarnEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -247,6 +274,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,忽略 marker 参数。
|
||||
*/
|
||||
override fun isWarnEnabled(marker: Marker?): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -271,6 +301,9 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
warn(msg, t)
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,ERROR 级别日志始终启用。
|
||||
*/
|
||||
override fun isErrorEnabled(): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -300,12 +333,18 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出 ERROR 级别日志,仅输出消息文本,忽略异常参数。
|
||||
*/
|
||||
override fun error(msg: String?, t: Throwable?) {
|
||||
msg?.let {
|
||||
toPrintln(it, MingLiLoggerLevel.ERROR)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 始终返回 true,忽略 marker 参数。
|
||||
*/
|
||||
override fun isErrorEnabled(marker: Marker?): Boolean {
|
||||
return true
|
||||
}
|
||||
@@ -330,6 +369,12 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
error(msg, t)
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定日志级别格式化并输出日志消息。
|
||||
*
|
||||
* @param message 日志消息内容
|
||||
* @param level 日志级别
|
||||
*/
|
||||
fun toPrintln(message: String, level: MingLiLoggerLevel) {
|
||||
when (level) {
|
||||
MingLiLoggerLevel.TRACE -> wirteToSteam("[TRACE] [$name] $message\n")
|
||||
@@ -340,6 +385,11 @@ class MingLiLogger(private val name: String) : Logger {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串以字节形式写入标准输出流。
|
||||
*
|
||||
* @param string 要输出的字符串
|
||||
*/
|
||||
fun wirteToSteam(string: String) {
|
||||
System.out.write(string.toByteArray())
|
||||
}
|
||||
|
||||
@@ -27,8 +27,15 @@ import org.slf4j.LoggerFactory
|
||||
import java.lang.reflect.Method
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* 日志器工厂,优先使用 SLF4J 实现(或 mingli-logger 的 Loggers)获取日志器,
|
||||
* 在没有可用 SLF4J 实现时回退到内置的 MingLiLogger。
|
||||
*/
|
||||
class MingLiLoggerFactory {
|
||||
|
||||
/**
|
||||
* 伴生对象,提供获取日志器的静态方法。
|
||||
*/
|
||||
companion object {
|
||||
|
||||
private var nameMethod: Method? = null
|
||||
@@ -48,6 +55,12 @@ class MingLiLoggerFactory {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据名称获取日志器。
|
||||
*
|
||||
* @param name 日志器名称
|
||||
* @return 对应的 Logger 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getLogger(name: String): Logger {
|
||||
|
||||
@@ -60,6 +73,12 @@ class MingLiLoggerFactory {
|
||||
} else mingLiLoggerFactory.getLogger(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类对象获取日志器,日志器名称为类的全限定名。
|
||||
*
|
||||
* @param clazz 用于命名日志器的类对象
|
||||
* @return 对应的 Logger 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getLogger(clazz: Class<*>): Logger {
|
||||
return if (mingLiLoggerFactory.checkSLF4JImplementation()) {
|
||||
@@ -71,6 +90,12 @@ class MingLiLoggerFactory {
|
||||
} else mingLiLoggerFactory.getLogger(clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 reified 类型参数获取日志器,日志器名称为类型 T 的全限定名。
|
||||
*
|
||||
* @param T 用于命名日志器的类型
|
||||
* @return 对应的 Logger 实例
|
||||
*/
|
||||
inline fun <reified T> getLogger() = getLogger(T::class.java)
|
||||
}
|
||||
|
||||
|
||||
@@ -22,10 +22,32 @@
|
||||
|
||||
package com.mingliqiye.utils.logger
|
||||
|
||||
/**
|
||||
* 日志级别枚举,定义 MingLiLogger 支持的五个日志级别。
|
||||
*/
|
||||
enum class MingLiLoggerLevel {
|
||||
/**
|
||||
* 跟踪级别,最详细的调试信息
|
||||
*/
|
||||
TRACE,
|
||||
|
||||
/**
|
||||
* 调试级别
|
||||
*/
|
||||
DEBUG,
|
||||
|
||||
/**
|
||||
* 信息级别
|
||||
*/
|
||||
INFO,
|
||||
|
||||
/**
|
||||
* 警告级别
|
||||
*/
|
||||
WARN,
|
||||
|
||||
/**
|
||||
* 错误级别
|
||||
*/
|
||||
ERROR
|
||||
}
|
||||
|
||||
@@ -28,6 +28,14 @@ import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
|
||||
/**
|
||||
* 使用键值对可变参数创建一个新的 HashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 键值对可变参数列表
|
||||
* @return 新创建的 HashMap
|
||||
*/
|
||||
fun <K, V> newHashMap(vararg elements: Pair<K, V>): HashMap<K, V> {
|
||||
val map = HashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -36,14 +44,37 @@ fun <K, V> newHashMap(vararg elements: Pair<K, V>): HashMap<K, V> {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个指定初始容量的新 HashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param size 初始容量大小
|
||||
* @return 新创建的 HashMap
|
||||
*/
|
||||
fun <K, V> newHashMap(size: Int): HashMap<K, V> {
|
||||
return HashMap(size)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 HashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 HashMap
|
||||
*/
|
||||
fun <K, V> newHashMap(): HashMap<K, V> {
|
||||
return HashMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 Map.Entry 可变参数创建一个新的 HashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 映射项可变参数列表
|
||||
* @return 新创建的 HashMap
|
||||
*/
|
||||
fun <K, V> newHashMap(vararg elements: Map.Entry<K, V>): HashMap<K, V> {
|
||||
val map = HashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -52,6 +83,14 @@ fun <K, V> newHashMap(vararg elements: Map.Entry<K, V>): HashMap<K, V> {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 Map.Entry 可变参数创建一个新的 ConcurrentHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 映射项可变参数列表
|
||||
* @return 新创建的 ConcurrentHashMap
|
||||
*/
|
||||
fun <K, V> newConcurrentHashMap(vararg elements: Map.Entry<K, V>): ConcurrentHashMap<K, V> {
|
||||
val map = newConcurrentHashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -60,6 +99,14 @@ fun <K, V> newConcurrentHashMap(vararg elements: Map.Entry<K, V>): ConcurrentHas
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 Map.Entry 可变参数创建一个新的 LinkedHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 映射项可变参数列表
|
||||
* @return 新创建的 LinkedHashMap
|
||||
*/
|
||||
fun <K, V> newLinkedHashMap(vararg elements: Map.Entry<K, V>): LinkedHashMap<K, V> {
|
||||
val map = newLinkedHashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -68,6 +115,14 @@ fun <K, V> newLinkedHashMap(vararg elements: Map.Entry<K, V>): LinkedHashMap<K,
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 Map.Entry 可变参数创建一个新的 Hashtable 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 映射项可变参数列表
|
||||
* @return 新创建的 Hashtable
|
||||
*/
|
||||
fun <K, V> newHashtable(vararg elements: Map.Entry<K, V>): Hashtable<K, V> {
|
||||
val map = newHashtable<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -76,6 +131,14 @@ fun <K, V> newHashtable(vararg elements: Map.Entry<K, V>): Hashtable<K, V> {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 Map.Entry 可变参数创建一个新的 TreeMap 实例。
|
||||
*
|
||||
* @param K 键类型,必须实现 Comparable 接口
|
||||
* @param V 值类型
|
||||
* @param elements 映射项可变参数列表
|
||||
* @return 新创建的 TreeMap
|
||||
*/
|
||||
fun <K : Comparable<K>, V> newTreeMap(vararg elements: Map.Entry<K, V>): TreeMap<K, V> {
|
||||
val map = newTreeMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -84,18 +147,48 @@ fun <K : Comparable<K>, V> newTreeMap(vararg elements: Map.Entry<K, V>): TreeMap
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 Map 转换为新的 HashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 HashMap
|
||||
*/
|
||||
inline fun <reified K, reified V> Map<K, V>.newHashMap(): HashMap<K, V> {
|
||||
return HashMap(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 LinkedHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 LinkedHashMap
|
||||
*/
|
||||
fun <K, V> newLinkedHashMap(): LinkedHashMap<K, V> {
|
||||
return LinkedHashMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个指定初始容量的新 LinkedHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param size 初始容量大小
|
||||
* @return 新创建的 LinkedHashMap
|
||||
*/
|
||||
fun <K, V> newLinkedHashMap(size: Int): LinkedHashMap<K, V> {
|
||||
return LinkedHashMap(size)
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用键值对可变参数创建一个新的 LinkedHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 键值对可变参数列表
|
||||
* @return 新创建的 LinkedHashMap
|
||||
*/
|
||||
fun <K, V> newLinkedHashMap(vararg elements: Pair<K, V>): LinkedHashMap<K, V> {
|
||||
val map = newLinkedHashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -104,14 +197,36 @@ fun <K, V> newLinkedHashMap(vararg elements: Pair<K, V>): LinkedHashMap<K, V> {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 Map 转换为新的 LinkedHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 LinkedHashMap
|
||||
*/
|
||||
inline fun <reified K, reified V> Map<K, V>.newLinkedHashMap(): LinkedHashMap<K, V> {
|
||||
return LinkedHashMap(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 TreeMap 实例。
|
||||
*
|
||||
* @param K 键类型,必须实现 Comparable 接口
|
||||
* @param V 值类型
|
||||
* @return 新创建的 TreeMap
|
||||
*/
|
||||
fun <K : Comparable<K>, V> newTreeMap(): TreeMap<K, V> {
|
||||
return TreeMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用键值对可变参数创建一个新的 TreeMap 实例。
|
||||
*
|
||||
* @param K 键类型,必须实现 Comparable 接口
|
||||
* @param V 值类型
|
||||
* @param elements 键值对可变参数列表
|
||||
* @return 新创建的 TreeMap
|
||||
*/
|
||||
fun <K : Comparable<K>, V> newTreeMap(vararg elements: Pair<K, V>): TreeMap<K, V> {
|
||||
val map = newTreeMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -120,18 +235,48 @@ fun <K : Comparable<K>, V> newTreeMap(vararg elements: Pair<K, V>): TreeMap<K, V
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 Map 转换为新的 TreeMap 实例。
|
||||
*
|
||||
* @param K 键类型,必须实现 Comparable 接口
|
||||
* @param V 值类型
|
||||
* @return 新创建的 TreeMap
|
||||
*/
|
||||
inline fun <reified K : Comparable<K>, reified V> Map<K, V>.newTreeMap(): TreeMap<K, V> {
|
||||
return TreeMap(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 Hashtable 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 Hashtable
|
||||
*/
|
||||
fun <K, V> newHashtable(): Hashtable<K, V> {
|
||||
return Hashtable()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个指定初始容量的新 Hashtable 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param size 初始容量大小
|
||||
* @return 新创建的 Hashtable
|
||||
*/
|
||||
fun <K, V> newHashtable(size: Int): Hashtable<K, V> {
|
||||
return Hashtable(size)
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用键值对可变参数创建一个新的 Hashtable 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 键值对可变参数列表
|
||||
* @return 新创建的 Hashtable
|
||||
*/
|
||||
fun <K, V> newHashtable(vararg elements: Pair<K, V>): Hashtable<K, V> {
|
||||
val map = newHashtable<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -140,20 +285,50 @@ fun <K, V> newHashtable(vararg elements: Pair<K, V>): Hashtable<K, V> {
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 Map 转换为新的 Hashtable 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 Hashtable
|
||||
*/
|
||||
inline fun <reified K, reified V> Map<K, V>.newHashtable(): Hashtable<K, V> {
|
||||
val hashtable = Hashtable<K, V>()
|
||||
hashtable.putAll(this)
|
||||
return hashtable
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 ConcurrentHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 ConcurrentHashMap
|
||||
*/
|
||||
fun <K, V> newConcurrentHashMap(): ConcurrentHashMap<K, V> {
|
||||
return ConcurrentHashMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个指定初始容量的新 ConcurrentHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param size 初始容量大小
|
||||
* @return 新创建的 ConcurrentHashMap
|
||||
*/
|
||||
fun <K, V> newConcurrentHashMap(size: Int): ConcurrentHashMap<K, V> {
|
||||
return ConcurrentHashMap(size)
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用键值对可变参数创建一个新的 ConcurrentHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param elements 键值对可变参数列表
|
||||
* @return 新创建的 ConcurrentHashMap
|
||||
*/
|
||||
fun <K, V> newConcurrentHashMap(vararg elements: Pair<K, V>): ConcurrentHashMap<K, V> {
|
||||
val map = newConcurrentHashMap<K, V>()
|
||||
for (element in elements) {
|
||||
@@ -162,10 +337,25 @@ fun <K, V> newConcurrentHashMap(vararg elements: Pair<K, V>): ConcurrentHashMap<
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前 Map 转换为新的 ConcurrentHashMap 实例。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @return 新创建的 ConcurrentHashMap
|
||||
*/
|
||||
inline fun <reified K, reified V> Map<K, V>.newConcurrentHashMap(): ConcurrentHashMap<K, V> {
|
||||
return ConcurrentHashMap(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将映射项列表转换为 Map。
|
||||
*
|
||||
* @param K 键类型
|
||||
* @param V 值类型
|
||||
* @param entries 映射项列表
|
||||
* @return 由映射项组成的 Map
|
||||
*/
|
||||
fun <K, V> ofEntries(entries: List<MutableMap.MutableEntry<K, V>>): Map<K, V> {
|
||||
return entries.associate { it.key to it.value }
|
||||
}
|
||||
|
||||
@@ -26,8 +26,18 @@ package com.mingliqiye.utils.metadata
|
||||
import com.mingliqiye.utils.resource.ResourceUtils
|
||||
import java.util.stream.Collectors.toMap
|
||||
|
||||
/**
|
||||
* 元数据资源定位锚点类,仅用于定位 `/META-INF/meta-data` 资源所在的类加载器
|
||||
*/
|
||||
class MetaDataResource
|
||||
|
||||
/**
|
||||
* 读取并解析 `/META-INF/meta-data` 资源文件
|
||||
*
|
||||
* 文件内容按行解析为 `key=value` 形式的键值对,空行与格式不合法的行会被忽略。
|
||||
*
|
||||
* @return 元数据键值对映射
|
||||
*/
|
||||
fun getMetaData(): Map<String, String> {
|
||||
return ResourceUtils.getResourceAsString<MetaDataResource>("/META-INF/meta-data").split("\n").stream().map {
|
||||
if (it.isBlank()) {
|
||||
@@ -42,6 +52,11 @@ fun getMetaData(): Map<String, String> {
|
||||
}.filter { it != null }.collect(toMap({ it!!.first }, { it!!.second }))
|
||||
}
|
||||
|
||||
/**
|
||||
* mingli-utils 库的构建元数据,延迟初始化
|
||||
*
|
||||
* 从 `/META-INF/meta-data` 资源解析并填充 [MingliUtilsMetaData],缺失的字段以空字符串代替。
|
||||
*/
|
||||
val mingliUtilsMetaData: MingliUtilsMetaData by lazy {
|
||||
val metaData = getMetaData()
|
||||
MingliUtilsMetaData().apply {
|
||||
|
||||
@@ -22,6 +22,17 @@
|
||||
|
||||
package com.mingliqiye.utils.metadata
|
||||
|
||||
/**
|
||||
* mingli-utils 库的构建元数据数据类
|
||||
*
|
||||
* @property buildTime 构建时间
|
||||
* @property groupId 构建产物的 GroupId
|
||||
* @property artifactId 构建产物的 ArtifactId
|
||||
* @property version 库版本号
|
||||
* @property buildJdkVersion 构建所使用的 JDK 版本
|
||||
* @property author 作者
|
||||
* @property website 项目网站地址
|
||||
*/
|
||||
data class MingliUtilsMetaData(
|
||||
var buildTime: String = "",
|
||||
var groupId: String = "",
|
||||
|
||||
@@ -22,6 +22,11 @@
|
||||
|
||||
package com.mingliqiye.utils.mybatis
|
||||
|
||||
/**
|
||||
* 类型处理器取值时的调用类型枚举。
|
||||
* 标识当前是从 ResultSet 还是 CallableStatement、以及通过列索引还是列名获取数据,
|
||||
* 供 [QuickBaseTypeHandler.getValue] 在取值时选择对应的 JDBC 访问方式。
|
||||
*/
|
||||
enum class CallType {
|
||||
/**
|
||||
* 通过索引访问结果集
|
||||
|
||||
@@ -25,4 +25,12 @@ package com.mingliqiye.utils.mybatis
|
||||
import java.sql.CallableStatement
|
||||
import java.sql.ResultSet
|
||||
|
||||
/**
|
||||
* 类型处理器的值获取器,封装取值时可用的 JDBC 数据源。
|
||||
* 根据 [CallType] 的不同,实际使用 [resultSet] 或 [callableStatement] 读取数据,
|
||||
* 未使用的一方为 null。
|
||||
*
|
||||
* @property callableStatement 可调用语句对象,从存储过程输出参数取值时使用,否则为 null
|
||||
* @property resultSet 结果集对象,从查询结果取值时使用,否则为 null
|
||||
*/
|
||||
data class QuickBaseTypeHandlerValueGetter(var callableStatement: CallableStatement?, var resultSet: ResultSet?)
|
||||
|
||||
+6
@@ -38,6 +38,9 @@ import java.sql.Types.TIMESTAMP
|
||||
@MappedTypes(DateTime::class)
|
||||
class DateTimeTypeHandler : QuickBaseTypeHandler<DateTime>() {
|
||||
|
||||
/**
|
||||
* 从 JDBC 数据源读取 Timestamp 并转换为 [DateTime],数据库值为 null 时返回 null。
|
||||
*/
|
||||
override fun getValue(
|
||||
vg: QuickBaseTypeHandlerValueGetter,
|
||||
ct: CallType,
|
||||
@@ -57,6 +60,9 @@ class DateTimeTypeHandler : QuickBaseTypeHandler<DateTime>() {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 [DateTime] 以 Timestamp 形式写入 PreparedStatement,参数为 null 时写入 TIMESTAMP 类型的 NULL。
|
||||
*/
|
||||
override fun setValue(
|
||||
ps: PreparedStatement,
|
||||
index: Int,
|
||||
|
||||
+14
@@ -32,9 +32,17 @@ import org.apache.ibatis.type.JdbcType
|
||||
import org.apache.ibatis.type.MappedTypes
|
||||
import java.sql.PreparedStatement
|
||||
|
||||
/**
|
||||
* MySQL 的 UUID 二进制类型处理器。
|
||||
* 将 [UUID] 与 MySQL BINARY(16) 列互转,写入前按 MySQL 的字节序重排([uuidToMysql]),
|
||||
* 读取后再还原为标准 UUID 字节序([mysqlToUuid]),以便 UUID 在数据库中按时间有序存储。
|
||||
*/
|
||||
@MappedTypes(UUID::class)
|
||||
class MysqlUUIDBinaryTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||
|
||||
/**
|
||||
* 伴生对象,提供 UUID 与 MySQL 二进制字节数组之间的转换工具方法。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 将字节数组转换为UUID对象
|
||||
@@ -57,6 +65,9 @@ class MysqlUUIDBinaryTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 JDBC 数据源读取字节数组,按 MySQL 字节序还原为 [UUID],数据库值为 null 时返回 null。
|
||||
*/
|
||||
override fun getValue(
|
||||
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?,
|
||||
): UUID? {
|
||||
@@ -75,6 +86,9 @@ class MysqlUUIDBinaryTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 [UUID] 转换为 MySQL 字节序的字节数组写入 PreparedStatement,参数为 null 时写入 BINARY 类型的 NULL。
|
||||
*/
|
||||
override fun setValue(
|
||||
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?,
|
||||
) {
|
||||
|
||||
+11
@@ -30,8 +30,16 @@ import org.apache.ibatis.type.JdbcType
|
||||
import org.apache.ibatis.type.MappedTypes
|
||||
import java.sql.PreparedStatement
|
||||
|
||||
/**
|
||||
* PostgreSQL 的 UUID 类型处理器。
|
||||
* 将 [UUID] 与 PostgreSQL 原生 uuid 列互转:读取时以字符串形式取出并解析为 [UUID],
|
||||
* 写入时通过 setObject 传入 java.util.UUID,由驱动按 uuid 类型处理。
|
||||
*/
|
||||
@MappedTypes(UUID::class)
|
||||
class PgsqlUUIDTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||
/**
|
||||
* 从 JDBC 数据源读取 UUID 字符串并解析为 [UUID],数据库值为 null 时返回 null。
|
||||
*/
|
||||
override fun getValue(
|
||||
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?,
|
||||
): UUID? {
|
||||
@@ -51,6 +59,9 @@ class PgsqlUUIDTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||
return UUID.of(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 [UUID] 以 java.util.UUID 对象写入 PreparedStatement,参数为 null 时按指定 JDBC 类型(默认 JAVA_OBJECT)写入 NULL。
|
||||
*/
|
||||
override fun setValue(
|
||||
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?,
|
||||
) {
|
||||
|
||||
@@ -455,6 +455,9 @@ open class FastBaseIService<T, D : FastBaseMapper<T>> : ServiceImpl<D, T>(), ISe
|
||||
wrapper: LambdaQueryWrapper<T>,
|
||||
) = mapper.deleteByColumn(column, value, wrapper)
|
||||
|
||||
/**
|
||||
* 伴生对象,提供面向 [FastBaseIService] 的 Kotlin 包装器创建与链式查询、更新、删除扩展函数。
|
||||
*/
|
||||
companion object {
|
||||
|
||||
/**
|
||||
|
||||
@@ -456,6 +456,9 @@ open class FastBaseIServiceNew3518<T, D : FastBaseMapper<T>> : ServiceImpl<D, T>
|
||||
wrapper: LambdaQueryWrapper<T>,
|
||||
) = mapper.deleteByColumn(column, value, wrapper)
|
||||
|
||||
/**
|
||||
* 伴生对象,提供面向 [FastBaseIServiceNew3518] 的 Kotlin 包装器创建与链式查询、更新、删除扩展函数。
|
||||
*/
|
||||
companion object {
|
||||
|
||||
/**
|
||||
|
||||
@@ -216,7 +216,16 @@ interface FastBaseMapper<T> : BaseMapper<T> {
|
||||
val t1: String,
|
||||
val t2: Any,
|
||||
) : Tuple(2) {
|
||||
/**
|
||||
* 解构分量1
|
||||
* @return String 列名(数据库字段名)
|
||||
*/
|
||||
operator fun component1() = t1
|
||||
|
||||
/**
|
||||
* 解构分量2
|
||||
* @return Any 列值
|
||||
*/
|
||||
operator fun component2() = t2
|
||||
}
|
||||
|
||||
@@ -231,7 +240,16 @@ interface FastBaseMapper<T> : BaseMapper<T> {
|
||||
val t1: SFunction<T, D>,
|
||||
val t2: D,
|
||||
) : Tuple(2) {
|
||||
/**
|
||||
* 解构分量1
|
||||
* @return SFunction<T, D> 实体列的方法引用
|
||||
*/
|
||||
operator fun component1() = t1
|
||||
|
||||
/**
|
||||
* 解构分量2
|
||||
* @return D 列值
|
||||
*/
|
||||
operator fun component2() = t2
|
||||
}
|
||||
|
||||
@@ -507,6 +525,9 @@ interface FastBaseMapper<T> : BaseMapper<T> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 伴生对象,提供面向 [BaseMapper] / [FastBaseMapper] 的 Kotlin 包装器创建与链式查询、更新、删除扩展函数。
|
||||
*/
|
||||
companion object {
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,15 +40,43 @@ abstract class ClientScheduleReconnect {
|
||||
private var delay: Long = 10L
|
||||
private var timeUnit: TimeUnit = TimeUnit.SECONDS
|
||||
|
||||
/**
|
||||
* 使用默认 Bootstrap 构造,重连延迟默认 10 秒
|
||||
*/
|
||||
constructor() : this(Bootstrap())
|
||||
|
||||
/**
|
||||
* 使用指定 Bootstrap 构造,重连延迟默认 10 秒
|
||||
*
|
||||
* @param bootstrap Netty 引导配置对象
|
||||
*/
|
||||
constructor(bootstrap: Bootstrap) : this(bootstrap, 10L)
|
||||
|
||||
/**
|
||||
* 使用指定 Bootstrap 与重连延迟构造,时间单位默认秒
|
||||
*
|
||||
* @param bootstrap Netty 引导配置对象
|
||||
* @param delay 重连延迟时间
|
||||
*/
|
||||
constructor(bootstrap: Bootstrap, delay: Long) : this(bootstrap, delay, TimeUnit.SECONDS)
|
||||
|
||||
/**
|
||||
* 使用指定 Bootstrap、重连延迟与时间单位构造
|
||||
*
|
||||
* @param bootstrap Netty 引导配置对象
|
||||
* @param delay 重连延迟时间
|
||||
* @param timeUnit 延迟时间单位
|
||||
*/
|
||||
constructor(bootstrap: Bootstrap, delay: Long, timeUnit: TimeUnit) {
|
||||
this.bootstrap = bootstrap
|
||||
this.delay = delay
|
||||
this.timeUnit = timeUnit
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动客户端:初始化引导配置并发起首次连接。
|
||||
* 连接失败或通道断开后会自动调度重连,直到调用 [close] 停止。
|
||||
*/
|
||||
open fun start() {
|
||||
initBootstrap(bootstrap)
|
||||
connect()
|
||||
@@ -120,6 +148,10 @@ abstract class ClientScheduleReconnect {
|
||||
*/
|
||||
abstract fun onStoppedLog()
|
||||
|
||||
/**
|
||||
* 连接通道关闭后的回调,由子类实现(如记录日志、清理资源)。
|
||||
* 在调度重连之后被调用。
|
||||
*/
|
||||
abstract fun onConnectClosed()
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger
|
||||
*/
|
||||
open class NamedThreadFactory(private val getName: NamedThreadFactoryNameGetter) : ThreadFactory {
|
||||
|
||||
/** 伴生对象,提供线程名称生成器接口与工厂方法 */
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
|
||||
@@ -41,6 +41,7 @@ class NetworkAddress private constructor(domip: String) : Serializable {
|
||||
* IPv6标识
|
||||
*/
|
||||
companion object {
|
||||
/** IPv6 版本标识,值为 6 */
|
||||
const val IPV6 = 6
|
||||
|
||||
/**
|
||||
@@ -81,6 +82,12 @@ class NetworkAddress private constructor(domip: String) : Serializable {
|
||||
return NetworkAddress(domip)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 ByteBuffer 读取 4 字节构建 IPv4 地址实例。
|
||||
*
|
||||
* @param byteBuffer 包含 IPv4 地址数据的缓冲区(读取 4 字节)
|
||||
* @return 新建的 NetworkAddress 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun ofIpv4(byteBuffer: ByteBuffer): NetworkAddress {
|
||||
val byteArray = ByteArray(4)
|
||||
@@ -88,6 +95,12 @@ class NetworkAddress private constructor(domip: String) : Serializable {
|
||||
return ofIpv4(byteArray)
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 4 字节数组构建 IPv4 地址实例。
|
||||
*
|
||||
* @param byteArray 表示 IPv4 地址的字节数组(长度为 4)
|
||||
* @return 新建的 NetworkAddress 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun ofIpv4(byteArray: ByteArray): NetworkAddress {
|
||||
return of(".".join(byteArray.map {
|
||||
|
||||
@@ -35,10 +35,12 @@ import java.nio.ByteBuffer
|
||||
* @see java.net.InetSocketAddress
|
||||
*/
|
||||
class NetworkEndpoint private constructor(
|
||||
val networkAddress: NetworkAddress, val networkPort: NetworkPort,
|
||||
/** 网络地址(IP 或域名解析结果) */
|
||||
val networkAddress: NetworkAddress, /** 网络端口 */ val networkPort: NetworkPort,
|
||||
) : Serializable, InetSocketAddress(networkAddress.toInetAddress(), networkPort.port) {
|
||||
|
||||
|
||||
/** 伴生对象,提供工厂方法与 SocketAddress 扩展属性 */
|
||||
companion object {
|
||||
/**
|
||||
* 根据给定的InetSocketAddress对象创建NetworkEndpoint实例。
|
||||
@@ -122,8 +124,10 @@ class NetworkEndpoint private constructor(
|
||||
)
|
||||
}
|
||||
|
||||
/** 将 [SocketAddress] 转换为 [NetworkEndpoint],仅支持 InetSocketAddress */
|
||||
val SocketAddress.networkAddress: NetworkEndpoint
|
||||
get() = of(this)
|
||||
/** 获取 [SocketAddress] 的 "host:port" 格式字符串 */
|
||||
val SocketAddress.hostPortString: String
|
||||
get() = networkAddress.toHostPortString()
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
|
||||
package com.mingliqiye.utils.network
|
||||
|
||||
/**
|
||||
* 网络异常,用于表示 IP 地址、端口号或域名解析等网络相关错误。
|
||||
*/
|
||||
class NetworkException : RuntimeException {
|
||||
/**
|
||||
* 构造一个带有指定详细消息的网络异常
|
||||
|
||||
@@ -25,14 +25,31 @@ package com.mingliqiye.utils.network
|
||||
import java.io.Serializable
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* 网络端口类,封装合法的端口号(0-65535),并提供字节序列化能力。
|
||||
*
|
||||
* 实例创建后端口号不可变,线程安全。
|
||||
*/
|
||||
class NetworkPort : Serializable {
|
||||
/** 端口号,取值范围 0-65535 */
|
||||
val port: Int
|
||||
|
||||
/**
|
||||
* 构造端口实例,并校验端口号合法性
|
||||
*
|
||||
* @param port 端口号,必须在 0-65535 之间
|
||||
* @throws NetworkException 端口号超出合法范围时抛出
|
||||
*/
|
||||
constructor(port: Int) {
|
||||
testPort(port)
|
||||
this.port = port
|
||||
}
|
||||
|
||||
/**
|
||||
* 将端口号转换为 2 字节数组(大端序,高字节在前)
|
||||
*
|
||||
* @return 长度为 2 的字节数组
|
||||
*/
|
||||
fun toByteArray(): ByteArray {
|
||||
val byteArray = ByteArray(2)
|
||||
byteArray[0] = (port shr 8 and 0xFF).toByte()
|
||||
@@ -40,10 +57,17 @@ class NetworkPort : Serializable {
|
||||
return byteArray
|
||||
}
|
||||
|
||||
/**
|
||||
* 将端口号以 2 字节写入指定的 ByteBuffer
|
||||
*
|
||||
* @param byteBuffer 目标字节缓冲区
|
||||
* @return 写入后的缓冲区,便于链式调用
|
||||
*/
|
||||
fun writeToByteBuffer(byteBuffer: ByteBuffer): ByteBuffer {
|
||||
return byteBuffer.put(toByteArray())
|
||||
}
|
||||
|
||||
/** 伴生对象,提供工厂方法与端口校验 */
|
||||
companion object {
|
||||
|
||||
/**
|
||||
@@ -80,6 +104,12 @@ class NetworkPort : Serializable {
|
||||
return of(byteArray)
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验端口号是否在合法范围(0-65535)内
|
||||
*
|
||||
* @param port 待校验的端口号
|
||||
* @throws NetworkException 端口号非法时抛出
|
||||
*/
|
||||
@JvmStatic
|
||||
fun testPort(port: Int) {
|
||||
// 验证端口号范围是否在0-65535之间
|
||||
|
||||
@@ -22,8 +22,21 @@
|
||||
|
||||
package com.mingliqiye.utils.objects
|
||||
|
||||
/**
|
||||
* 布尔条件链式调用的辅助类。
|
||||
*
|
||||
* 由 [Boolean.ifs] 创建,用于在条件为 false 时执行 else 分支。
|
||||
*
|
||||
* @param boolean 取反后的条件值,为 true 时表示 if 分支未执行
|
||||
*/
|
||||
class BooleanIfFunction(private val boolean: Boolean) {
|
||||
|
||||
/**
|
||||
* 当 if 分支未执行(原条件为 false)时执行给定函数。
|
||||
*
|
||||
* @param function 条件为 false 时要执行的函数
|
||||
* @return 原条件为 true 时返回 true,否则返回 false
|
||||
*/
|
||||
fun elses(function: () -> Unit): Boolean {
|
||||
if (boolean) {
|
||||
function()
|
||||
@@ -32,6 +45,14 @@ class BooleanIfFunction(private val boolean: Boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 布尔值的链式 if 扩展:条件为 true 时立即执行给定函数。
|
||||
*
|
||||
* 可继续调用 [BooleanIfFunction.elses] 处理条件为 false 的分支。
|
||||
*
|
||||
* @param function 条件为 true 时要执行的函数
|
||||
* @return 用于衔接 else 分支的 [BooleanIfFunction] 实例
|
||||
*/
|
||||
inline fun Boolean.ifs(function: () -> Unit): BooleanIfFunction {
|
||||
return if (this) {
|
||||
function()
|
||||
|
||||
@@ -26,6 +26,13 @@ package com.mingliqiye.utils.objects
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.contract
|
||||
|
||||
/**
|
||||
* 判断当前对象是否不为 null。
|
||||
*
|
||||
* 通过 Kotlin 契约支持智能类型转换:返回 true 后编译器可将接收者视为非空。
|
||||
*
|
||||
* @return 不为 null 返回 true,否则返回 false
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
fun Any?.isNotNull(): Boolean {
|
||||
contract {
|
||||
@@ -34,6 +41,13 @@ fun Any?.isNotNull(): Boolean {
|
||||
return !isNull()
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前对象是否为 null。
|
||||
*
|
||||
* 通过 Kotlin 契约支持智能类型转换:返回 false 后编译器可将接收者视为非空。
|
||||
*
|
||||
* @return 为 null 返回 true,否则返回 false
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
fun Any?.isNull(): Boolean {
|
||||
contract {
|
||||
@@ -42,8 +56,24 @@ fun Any?.isNull(): Boolean {
|
||||
return this == null
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断两个对象是否相等,两者均为 null 时也视为相等。
|
||||
*
|
||||
* @param a 第一个对象
|
||||
* @param b 第二个对象
|
||||
* @return 相等(含均为 null)返回 true,否则返回 false
|
||||
*/
|
||||
fun areEquals(a: Any?, b: Any?) = (a == null && b == null) || (a == b)
|
||||
|
||||
/**
|
||||
* 判断两个非空对象是否相等,任一为 null 时返回 false。
|
||||
*
|
||||
* 通过 Kotlin 契约支持智能类型转换:返回 true 后编译器可将两个参数视为非空。
|
||||
*
|
||||
* @param a 第一个对象
|
||||
* @param b 第二个对象
|
||||
* @return 两者均非空且相等返回 true,否则返回 false
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
fun areEqualsNoNull(a: Any?, b: Any?): Boolean {
|
||||
contract {
|
||||
|
||||
@@ -30,6 +30,11 @@ import java.nio.file.*
|
||||
import java.util.*
|
||||
import java.util.function.Consumer
|
||||
|
||||
/**
|
||||
* 操作系统路径封装类,通过委托实现 Path 接口,并提供多种来源的静态工厂方法。
|
||||
*
|
||||
* @param path 被委托的底层 Path 实例
|
||||
*/
|
||||
class OsPath private constructor(private val path: Path) : Path by path {
|
||||
override fun forEach(action: Consumer<in Path>?) {
|
||||
path.forEach(action)
|
||||
@@ -74,27 +79,59 @@ class OsPath private constructor(private val path: Path) : Path by path {
|
||||
return path.iterator()
|
||||
}
|
||||
|
||||
/**
|
||||
* 伴生对象,提供从字符串、Path、URI、File 等创建 OsPath 实例的静态工厂方法。
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* 根据路径字符串创建 OsPath 实例。
|
||||
*
|
||||
* @param path 路径字符串
|
||||
* @return 对应的 OsPath 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun of(path: String): OsPath {
|
||||
return of(Paths.get(path))
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Path 对象创建 OsPath 实例。
|
||||
*
|
||||
* @param path Path 对象
|
||||
* @return 对应的 OsPath 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun of(path: Path): OsPath {
|
||||
return OsPath(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 URI 创建 OsPath 实例。
|
||||
*
|
||||
* @param uri 文件 URI
|
||||
* @return 对应的 OsPath 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun of(uri: URI): OsPath {
|
||||
return OsPath(Paths.get(uri))
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 File 对象创建 OsPath 实例。
|
||||
*
|
||||
* @param file File 对象
|
||||
* @return 对应的 OsPath 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun of(file: File): OsPath {
|
||||
return OsPath(file.toPath())
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表示当前工作目录的 OsPath 实例。
|
||||
*
|
||||
* @return 当前工作目录对应的 OsPath 实例
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getCwd(): OsPath {
|
||||
return OsPath(Paths.get(""))
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user