This commit is contained in:
2026-03-12 10:34:42 +08:00
parent bc8927db26
commit 454cd53553
17 changed files with 245 additions and 167 deletions
+9 -8
View File
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils
* CurrentFile build.gradle.kts
* LastUpdate 2026-03-10 09:16:54
* LastUpdate 2026-03-12 10:29:17
* UpdateUser MingLiPro
*/
@@ -50,14 +50,13 @@ base {
val buildDir: java.nio.file.Path = File("build").toPath()
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
toolchain.languageVersion.set(JavaLanguageVersion.of(JDKVERSIONS))
withSourcesJar()
}
kotlin {
jvmToolchain(JDKVERSIONS)
}
dependencies {
implementation("org.slf4j:slf4j-api:2.0.17")
implementation(kotlin("reflect"))
compileOnly("org.mindrot:jbcrypt:0.4")
@@ -93,12 +92,10 @@ tasks.named<Test>("test") {
}
tasks.withType<JavaExec>().configureEach {
jvmArgs?.addAll(
listOf(
tasks.withType<JavaExec> {
jvmArgs(
"-Dfile.encoding=UTF-8", "-Dsun.stdout.encoding=UTF-8", "-Dsun.stderr.encoding=UTF-8"
)
)
}
kapt {
@@ -158,7 +155,11 @@ publishing {
repositories {
maven {
name = "MavenRepositoryRaw"
url = uri("C:/data/git/maven-repository-raw")
url = uri("https://localhost:3000/")
credentials {
username = System.getenv("sonatype.username")
password = System.getenv("sonatype.password")
}
}
}
publications {
+2 -2
View File
@@ -16,10 +16,10 @@
# ProjectName mingli-utils
# ModuleName mingli-utils
# CurrentFile gradle.properties
# LastUpdate 2026-03-11 08:46:05
# LastUpdate 2026-03-12 10:34:01
# UpdateUser MingLiPro
#
JDKVERSIONS=17
GROUPSID=com.mingliqiye.utils
ARTIFACTID=mingli-utils
VERSIONS=5.0.8
VERSIONS=5.0.9
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile AesUtils.kt
* LastUpdate 2026-03-11 08:46:40
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
@@ -25,7 +25,7 @@
package com.mingliqiye.utils.aes
import com.mingliqiye.utils.base.code.Base64
import com.mingliqiye.utils.base.BaseType
import java.nio.charset.StandardCharsets
import java.security.GeneralSecurityException
import java.security.MessageDigest
@@ -71,8 +71,8 @@ fun encrypt(sSrc: String, sKey: String?): String? {
val encrypted = cipher.doFinal(
sSrc.toByteArray(StandardCharsets.UTF_8)
)
return Base64.encode(
"${Base64.encode(iv)}:${Base64.encode(encrypted)}".toByteArray()
return BaseType.BASE64.encode(
"${BaseType.BASE64.encode(iv)}:${BaseType.BASE64.encode(encrypted)}".toByteArray()
)
}
@@ -85,13 +85,13 @@ fun encrypt(sSrc: String, sKey: String?): String? {
fun decrypt(sSrc: String, sKey: String): String? {
try {
// 分割IV和加密数据
val sSrcs = String(Base64.decode(sSrc))
val sSrcs = String(BaseType.BASE64.decode(sSrc))
val parts: Array<String?> = sSrcs.split(":".toRegex(), limit = 2).toTypedArray()
if (parts.size != 2) {
return null
}
val iv = Base64.decode(parts[0]!!)
val encryptedData = Base64.decode(parts[1]!!)
val iv = BaseType.BASE64.decode(parts[0]!!)
val encryptedData = BaseType.BASE64.decode(parts[1]!!)
if (iv.size != GCM_IV_LENGTH) {
return null
}
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile ArrayUtils.kt
* LastUpdate 2026-03-11 08:42:08
* LastUpdate 2026-03-12 10:08:42
* UpdateUser MingLiPro
*/
@@ -25,7 +25,7 @@
package com.mingliqiye.utils.array
import com.mingliqiye.utils.base.code.Base16
import com.mingliqiye.utils.base.BaseType
/**
@@ -35,7 +35,7 @@ import com.mingliqiye.utils.base.code.Base16
* @since 4.6.0
* @see toHexByteArray
*/
fun ByteArray.toHexString(): String = Base16.encode(this).uppercase()
fun ByteArray.toHexString(): String = BaseType.BASE16.encode(this).uppercase()
/**
* 将十六进制字符串转换为字节数组
@@ -45,7 +45,7 @@ fun ByteArray.toHexString(): String = Base16.encode(this).uppercase()
* @since 4.6.0
* @see toHexString
*/
fun String.toHexByteArray(): ByteArray = Base16.decode(this.lowercase())
fun String.toHexByteArray(): ByteArray = BaseType.BASE16.decode(this.lowercase())
/**
* 将当前数组的指定范围复制到目标数组
@@ -15,8 +15,8 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base10.kt
* LastUpdate 2026-02-08 03:08:10
* CurrentFile Base10Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
@@ -24,7 +24,7 @@ package com.mingliqiye.utils.base
import java.math.BigInteger
internal class Base10 : BaseCodec {
internal class Base10Codec : BaseCodec {
/**
* 将字节数组编码为Base10字符串(十进制)
@@ -15,8 +15,8 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base16.kt
* LastUpdate 2026-02-08 03:08:10
* CurrentFile Base16Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
@@ -26,7 +26,7 @@ package com.mingliqiye.utils.base
* Base16编解码器实现类
* 提供字节数组与十六进制字符串之间的相互转换功能
*/
internal class Base16 : BaseCodec {
internal class Base16Codec : BaseCodec {
/**
* 将字节数组编码为十六进制字符串
* @param bytes 待编码的字节数组
@@ -15,8 +15,8 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base256.kt
* LastUpdate 2026-02-08 03:08:10
* CurrentFile Base256Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
@@ -30,7 +30,7 @@ package com.mingliqiye.utils.base
* !#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~§±×÷←↑→↓⇒⇔∀∃∅∆∇∈∉∋∌∏∑−∓∕∗∘∙√∛∜∞∟∠∣∥∧∨∩∪∫∬∭∮∯∰∱∲∳∴∵∶∷≈≠≡≤≥≦≧≪≫≺≻⊂⊃⊆⊇⊈⊉⊊⊋⊕⊖⊗⊘⊙⊚⊛⊜⊝⊞⊟⊠⊡⊢⊣⊤⊥⊦⊧⊨⊩⊪⊫⊬⊭⊮⊯⋀⋁⋂⋃⋄⋅⋆⋇⋈⋉⋊⋋⋌⋍⋎⋏⋐⋑⋒⋓⋔⋕⋖⋗⋘⋙⋚⋛⋜⋝⋞⋟⋠⋡⋢⋣⋤⋥⋦⋧⋨⋩▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙
*
*/
internal class Base256 : BaseCodec {
internal class Base256Codec : BaseCodec {
companion object {
val code = arrayOf(
@@ -15,14 +15,14 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base2.kt
* LastUpdate 2026-02-08 03:06:23
* CurrentFile Base2Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
internal class Base2 : BaseCodec {
internal class Base2Codec : BaseCodec {
override fun encode(bytes: ByteArray): String {
if (bytes.isEmpty()) return ""
val result = StringBuilder()
@@ -15,30 +15,32 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base64.kt
* LastUpdate 2026-02-08 03:08:10
* CurrentFile Base64Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
import java.util.*
/*
* Base64编解码工具类
* 提供Base64编码和解码功能的实现
*/
internal class Base64 : BaseCodec {
internal class Base64Codec : BaseCodec {
/*
* Base64编码器实例
* 用于执行字节数组到Base64字符串的编码操作
*/
val BASE_64_ENCODER: java.util.Base64.Encoder = java.util.Base64.getEncoder()
val encoder: Base64.Encoder = Base64.getEncoder()
/*
* Base64解码器实例
* 用于执行Base64字符串到字节数组的解码操作
*/
val BASE_64_DECODER: java.util.Base64.Decoder = java.util.Base64.getDecoder()
val decoder: Base64.Decoder = Base64.getDecoder()
/*
* 将字节数组编码为Base64字符串
@@ -47,7 +49,7 @@ internal class Base64 : BaseCodec {
* @return 编码后的Base64字符串
*/
override fun encode(bytes: ByteArray): String {
return BASE_64_ENCODER.encodeToString(bytes)
return encoder.encodeToString(bytes)
}
/*
@@ -57,6 +59,6 @@ internal class Base64 : BaseCodec {
* @return 解码后的字节数组
*/
override fun decode(string: String): ByteArray {
return BASE_64_DECODER.decode(string)
return decoder.decode(string)
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2026 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base64UrlCodec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
import java.util.*
internal class Base64UrlCodec : BaseCodec {
/*
* Base64编码器实例
* 用于执行字节数组到Base64字符串的编码操作
*/
val encoder: Base64.Encoder = Base64.getUrlEncoder()
/*
* Base64解码器实例
* 用于执行Base64字符串到字节数组的解码操作
*/
val decoder: Base64.Decoder = Base64.getUrlDecoder()
/*
* 将字节数组编码为Base64字符串
*
* @param bytes 待编码的字节数组
* @return 编码后的Base64字符串
*/
override fun encode(bytes: ByteArray): String {
return encoder.encodeToString(bytes)
}
/*
* 将Base64字符串解码为字节数组
*
* @param string 待解码的Base64字符串
* @return 解码后的字节数组
*/
override fun decode(string: String): ByteArray {
return decoder.decode(string)
}
}
@@ -15,8 +15,8 @@
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Base91.kt
* LastUpdate 2026-02-08 03:08:10
* CurrentFile Base91Codec.kt
* LastUpdate 2026-03-12 10:34:36
* UpdateUser MingLiPro
*/
@@ -27,7 +27,7 @@ package com.mingliqiye.utils.base
*
* Base91 是一种高效的二进制到文本的编码方式,相较于 Base64,它使用更少的字符来表示相同的数据。
*/
internal class Base91 : BaseCodec {
internal class Base91Codec : BaseCodec {
companion object {
/**
@@ -16,27 +16,21 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile BaseType.kt
* LastUpdate 2026-02-09 19:54:38
* LastUpdate 2026-03-12 10:08:42
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.base
import com.mingliqiye.utils.base.code.Base10
import com.mingliqiye.utils.base.code.Base16
import com.mingliqiye.utils.base.code.Base2
import com.mingliqiye.utils.base.code.Base256
import com.mingliqiye.utils.base.code.Base64
import com.mingliqiye.utils.base.code.Base91
enum class BaseType(val baseCodec: BaseCodec) : BaseCodec {
BASE2(Base2),
BASE10(Base10),
BASE16(Base16),
BASE64(Base64),
BASE91(Base91),
BASE256(Base256);
BASE2(com.mingliqiye.utils.base.code.BASE2),
BASE10(com.mingliqiye.utils.base.code.BASE10),
BASE16(com.mingliqiye.utils.base.code.BASE16),
BASE64(com.mingliqiye.utils.base.code.BASE64),
BASE64URL(com.mingliqiye.utils.base.code.BASE64URL),
BASE91(com.mingliqiye.utils.base.code.BASE91),
BASE256(com.mingliqiye.utils.base.code.BASE256);
override fun encode(bytes: ByteArray) = baseCodec.encode(bytes)
override fun decode(string: String) = baseCodec.decode(string)
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile BaseCodes.kt
* LastUpdate 2026-02-08 03:10:03
* LastUpdate 2026-03-12 10:03:32
* UpdateUser MingLiPro
*/
@@ -30,52 +30,57 @@ import com.mingliqiye.utils.base.*
* 提供Base2编码器的懒加载实例。
* 该编码器用于将数据编码为二进制格式。
*/
@get:JvmName("Base2")
val Base2: BaseCodec by lazy {
Base2()
@get:JvmName("base2")
val BASE2: BaseCodec by lazy {
Base2Codec()
}
/**
* 提供Base10编码器的懒加载实例。
* 该编码器用于将数据编码为十进制格式。
*/
@get:JvmName("Base10")
val Base10: BaseCodec by lazy {
Base10()
@get:JvmName("base10")
val BASE10: BaseCodec by lazy {
Base10Codec()
}
/**
* 提供Base16编码器的懒加载实例。
* 该编码器用于将数据编码为十六进制格式。
*/
@get:JvmName("Base16")
val Base16: BaseCodec by lazy {
Base16()
@get:JvmName("base16")
val BASE16: BaseCodec by lazy {
Base16Codec()
}
/**
* 提供Base64编码器的懒加载实例。
* 该编码器用于将数据编码为Base64格式,常用于URL安全传输或存储。
*/
@get:JvmName("Base64")
val Base64: BaseCodec by lazy {
Base64()
@get:JvmName("base64")
val BASE64: BaseCodec by lazy {
Base64Codec()
}
@get:JvmName("base64Url")
val BASE64URL: BaseCodec by lazy {
Base64UrlCodec()
}
/**
* 提供Base91编码器的懒加载实例。
* 该编码器用于将数据编码为Base91格式,具有较高的压缩效率。
*/
@get:JvmName("Base91")
val Base91: BaseCodec by lazy {
Base91()
@get:JvmName("base91")
val BASE91: BaseCodec by lazy {
Base91Codec()
}
/**
* 提供Base256编码器的懒加载实例。
* 该编码器用于将数据编码为Base256格式,适用于字节级数据处理。
*/
@get:JvmName("Base256")
val Base256: BaseCodec by lazy {
Base256()
@get:JvmName("base256")
val BASE256: BaseCodec by lazy {
Base256Codec()
}
@@ -16,14 +16,14 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile ByteUtils.kt
* LastUpdate 2026-03-11 08:46:40
* LastUpdate 2026-03-12 10:09:05
* UpdateUser MingLiPro
*/
@file:JvmName("ByteUtils")
package com.mingliqiye.utils.bytes
import com.mingliqiye.utils.base.code.Base16
import com.mingliqiye.utils.base.BaseType
import com.mingliqiye.utils.stream.SuperStream
/**
@@ -125,5 +125,5 @@ fun hexStringToByteArray(string: String): ByteArray {
* @return 对应的十六进制字符串
*/
fun bytesToHex(bytes: ByteArray): String {
return Base16.encode(bytes)
return BaseType.BASE16.encode(bytes)
}
@@ -16,61 +16,77 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile Response.kt
* LastUpdate 2026-02-07 22:18:42
* LastUpdate 2026-03-12 10:33:32
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.http
import com.mingliqiye.utils.annotation.DateTimeJsonFormat
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
import com.mingliqiye.utils.json.converters.base.AnnotationGetter
import com.mingliqiye.utils.time.DateTime
/**
* HTTP响应数据类
* 封装了时间、消息、数据和状态码等响应信息
*
* 该类用于统一API响应的格式,支持链式调用和不可变对象的创建。
* 提供了多种工厂方法用于创建不同类型的响应对象。
*
* @param T 响应数据的类型
* @property time 响应时间
* @property time 响应时间(格式化的字符串)
* @property message 响应消息
* @property data 响应数据
* @property statusCode 状态码
* @property data 响应数据,可能为空
* @property statusCode HTTP状态码
*/
data class Response<T>(
private var time: DateTime,
private var message: String,
private var data: T?,
private var statusCode: Int,
private val time: String, // 响应时间,一旦创建不可修改
private var message: String, // 响应消息,可通过setter修改
private var data: T?, // 响应数据,可通过setter修改
private var statusCode: Int, // HTTP状态码,可通过setter修改
) {
private var timeFormat: DateTimeJsonFormat = DateTimeJsonFormat()
companion object {
/**
* 创建一个默认的成功响应对象
* 使用默认消息"操作成功",无数据,状态码200
*
* @return Response<Any> 默认的成功响应对象
*/
fun ok() = ok("操作成功")
/**
* 创建一个成功的响应对象(仅包含消息)
*
* @param message 响应消息
* @return Response<Any> 成功的响应对象
* @return Response<Any> 成功的响应对象,数据为null
*/
@JvmStatic
fun ok(message: String) = Response(
time = DateTime.now(),
time = DateTime.now().format(),
message = message,
data = null,
statusCode = 200
)
/**
* 创建一个错误的响应对象
*
* @param message 错误消息,默认为"服务器错误"
* @param statusCode HTTP状态码,默认为500
* @return Response<Any> 错误的响应对象
*/
fun error(message: String = "服务器错误", statusCode: Int = 500) = ok(message).withStatusCode(statusCode)
/**
* 创建一个成功的响应对象(仅包含数据)
* 使用默认消息"操作成功"
*
* @param T 响应数据的类型
* @param data 响应数据
* @return Response<T> 成功的响应对象
* @return Response<T> 成功的响应对象,包含数据
*/
@JvmStatic
fun <T> okData(data: T) = Response(
time = DateTime.now(),
time = DateTime.now().format(),
message = "操作成功",
data = data,
statusCode = 200
@@ -82,26 +98,26 @@ data class Response<T>(
* @param T 响应数据的类型
* @param data 响应数据
* @param message 响应消息
* @return Response<T> 成功的响应对象
* @return Response<T> 成功的响应对象,包含数据和消息
*/
@JvmStatic
fun <T> okData(data: T, message: String) = Response(
time = DateTime.now(),
time = DateTime.now().format(),
message = message,
data = data,
statusCode = 200
)
/**
* 创建一个指定状态码的响应对象(仅包含状态码)
* 使用默认消息"操作成功",无数据
*
* @param statusCode 状态码
* @param statusCode HTTP状态码
* @return Response<Any> 指定状态码的响应对象
*/
@JvmStatic
fun code(statusCode: Int) = Response(
time = DateTime.now(),
time = DateTime.now().format(),
message = "操作成功",
data = null,
statusCode = statusCode
@@ -109,14 +125,15 @@ data class Response<T>(
/**
* 创建一个指定状态码的响应对象(包含状态码和消息)
* 无数据
*
* @param statusCode 状态码
* @param statusCode HTTP状态码
* @param message 响应消息
* @return Response<Any> 指定状态码的响应对象
* @return Response<Any> 指定状态码和消息的响应对象
*/
@JvmStatic
fun code(statusCode: Int, message: String) = Response(
time = DateTime.now(),
time = DateTime.now().format(),
message = message,
data = null,
statusCode = statusCode
@@ -125,45 +142,36 @@ data class Response<T>(
/**
* 默认构造函数,创建一个默认的成功响应对象
* 使用当前时间、默认消息"操作成功"、状态码200、无数据
*/
constructor() : this(
time = DateTime.now(), message = "操作成功", statusCode = 200, data = null
time = DateTime.now().format(),
message = "操作成功",
statusCode = 200,
data = null
)
/**
* 获取格式化后的时间字符串。
*
* @return 格式化后的时间字符串,使用 [DateTimeJsonConverter] 和 [timeFormat] 注解进行转换。
* @return 格式化后的时间字符串
*/
fun getTime(): String =
DateTimeJsonConverter.getJsonConverter().convert(time, AnnotationGetter.oneGetter(timeFormat))!!
/**
* 设置时间字段的值。
*
* @param dateTime 格式化后的时间字符串,将被反序列化为内部时间对象。
* @return 返回当前对象实例,支持链式调用。
*/
fun setTime(dateTime: String): Response<T> {
// 使用 DateTimeJsonConverter 将输入的字符串反序列化为时间对象,并更新内部 time 字段
time = DateTimeJsonConverter.getJsonConverter().deConvert(dateTime, AnnotationGetter.oneGetter(timeFormat))!!
return this
}
fun getTime(): String = time
/**
* 获取响应消息
*
* @return String 响应消息
* @return String 当前响应消息
*/
fun getMessage(): String = message
/**
* 设置响应消息
*
* @param message 响应消息
* @return Response<T> 当前响应对象(用于链式调用)
* 该方法允许修改响应消息,并返回当前对象以支持链式调用。
*
* @param message 新的响应消息
* @return Response<T> 当前响应对象(支持链式调用)
*/
fun setMessage(message: String): Response<T> {
this.message = message
@@ -173,6 +181,8 @@ data class Response<T>(
/**
* 获取响应数据
*
* 返回的响应数据可能为null,调用前应进行空值检查。
*
* @return T? 响应数据,可能为空
*/
fun getData(): T? = data
@@ -180,10 +190,10 @@ data class Response<T>(
/**
* 获取非空数据对象
*
* 此方法强制解包data属性,如果data为null则会抛出NullPointerException异常
* 该方法强制解包data属性,如果data为null则会抛出NullPointerException异常。
* 适用于确定data不为null的场景,避免重复的空值检查。
*
* @param T 泛型类型参数,表示返回的数据类型
* @return 返回非空的数据对象,类型为T
* @return T 非空的数据对象
* @throws NullPointerException 当data属性为null时抛出此异常
*/
@Throws(NullPointerException::class)
@@ -191,14 +201,13 @@ data class Response<T>(
return data ?: throw NullPointerException("at Response.notNullData() because data is null")
}
/**
* 设置响应数据
*
* @param D 数据类型
* @param data 响应数据
* @return Response<D> 当前响应对象(用于链式调用)
* 该方法允许修改响应数据,并返回当前对象以支持链式调用。
*
* @param data 新的响应数据
* @return Response<T> 当前响应对象(支持链式调用)
*/
fun setData(data: T): Response<T> {
this.data = data
@@ -208,18 +217,24 @@ data class Response<T>(
/**
* 创建一个新的Response对象,使用指定的时间
*
* 该方法用于创建具有新时间的响应对象,遵循不可变对象的设计模式。
* 如果时间相同,则返回原对象避免不必要的对象创建。
*
* @param time 新的响应时间
* @return Response<T> 新的响应对象
* @return Response<T> 新的响应对象(如果时间相同则返回原对象)
*/
fun withTime(time: DateTime): Response<T> =
fun withTime(time: String): Response<T> =
if (this.time == time) this else Response(time, message, data, statusCode)
/**
* 创建一个新的Response对象,使用指定的数据
*
* 该方法允许改变响应数据的类型,创建一个新的响应对象。
* 适用于需要在保持其他字段不变的情况下更新数据类型的场景。
*
* @param D 新的数据类型
* @param data 新的响应数据
* @return Response<D> 新的响应对象
* @return Response<D> 新的响应对象,数据类型为D
*/
fun <D> withData(data: D): Response<D> =
Response(this.time, this.message, data, this.statusCode)
@@ -227,8 +242,11 @@ data class Response<T>(
/**
* 创建一个新的Response对象,使用指定的消息
*
* 该方法用于创建具有新消息的响应对象,遵循不可变对象的设计模式。
* 如果消息相同,则返回原对象避免不必要的对象创建。
*
* @param message 新的响应消息
* @return Response<T> 新的响应对象
* @return Response<T> 新的响应对象(如果消息相同则返回原对象)
*/
fun withMessage(message: String): Response<T> =
if (this.message == message) this else Response(time, message, data, statusCode)
@@ -236,8 +254,11 @@ data class Response<T>(
/**
* 创建一个新的Response对象,使用指定的状态码
*
* 该方法用于创建具有新状态码的响应对象,遵循不可变对象的设计模式。
* 如果状态码相同,则返回原对象避免不必要的对象创建。
*
* @param statusCode 新的状态码
* @return Response<T> 新的响应对象
* @return Response<T> 新的响应对象(如果状态码相同则返回原对象)
*/
fun withStatusCode(statusCode: Int): Response<T> =
if (this.statusCode == statusCode) this else Response(time, message, data, statusCode)
@@ -245,30 +266,30 @@ data class Response<T>(
/**
* 获取状态码
*
* @return Int 状态码
* @return Int 当前HTTP状态码
*/
fun getStatusCode(): Int = statusCode
/**
* 设置状态码
*
* @param statusCode 状态码
* @return Response<T> 当前响应对象(用于链式调用)
* 该方法允许修改状态码,并返回当前对象以支持链式调用。
*
* @param statusCode 新的HTTP状态码
* @return Response<T> 当前响应对象(支持链式调用)
*/
fun setStatusCode(statusCode: Int): Response<T> {
this.statusCode = statusCode
return this
}
fun writeTimeFormat(timeFormat: DateTimeJsonFormat): Response<T> {
this.timeFormat = timeFormat
return this
}
fun readTimeFormat(): DateTimeJsonFormat = timeFormat
/**
* 返回响应对象的字符串表示
*
* 重写toString方法以提供更有意义的对象表示,便于日志记录和调试。
* 显示时间、消息、数据类型和值以及状态码。
*
* @return 格式化的字符串表示
*/
override fun toString(): String =
"Response(time=${getTime()}, message=${getMessage()}, data:[${data?.javaClass?.simpleName}]=${getData()}, statusCode=${getStatusCode()})"
@@ -16,14 +16,14 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile StringUtils.kt
* LastUpdate 2026-03-11 08:46:40
* LastUpdate 2026-03-12 10:05:22
* UpdateUser MingLiPro
*/
@file:JvmName("StringUtils")
package com.mingliqiye.utils.string
import com.mingliqiye.utils.base.code.Base16
import com.mingliqiye.utils.base.BaseType.BASE16
import com.mingliqiye.utils.logger.MingLiLoggerFactory
import com.mingliqiye.utils.objects.isNull
import java.net.URLDecoder
@@ -266,9 +266,7 @@ fun String?.parserTemplate(template: String): List<String>? {
.replace("]", "\\]")
.replace("+", "\\+")
.replace("=", "\\=")
.replace("{}", "((?s).*)")
.toRegex() +
"$"
.replace("{}", "((?s).*)") + "$".toRegex()
)
val datas = regex.find(this)?.groupValues ?: return null
return List(datas.size - 1) {
@@ -286,14 +284,14 @@ fun String.hmacSHA256String(keyS: String): String {
val key = keyS.toByteArray()
instance.init(SecretKeySpec(key, 0, key.size, "HmacSHA256"))
val bytes = instance.doFinal(this.toByteArray())
return Base16.encode(bytes)
return BASE16.encode(bytes)
}
fun String.md5String(): String {
val instance = MessageDigest.getInstance("MD5")
instance.update(this.toByteArray())
val bytes = instance.digest()
return Base16.encode(bytes)
return BASE16.encode(bytes)
}
fun String.hmacMd5String(keyS: String): String {
@@ -301,5 +299,5 @@ fun String.hmacMd5String(keyS: String): String {
val key = keyS.toByteArray()
instance.init(SecretKeySpec(key, 0, key.size, "HmacSHA256"))
val bytes = instance.doFinal(this.toByteArray())
return Base16.encode(bytes)
return BASE16.encode(bytes)
}
@@ -16,7 +16,7 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile UUID.kt
* LastUpdate 2026-03-07 11:02:29
* LastUpdate 2026-03-12 10:07:08
* UpdateUser MingLiPro
*/
@@ -25,9 +25,6 @@ package com.mingliqiye.utils.uuid
import com.mingliqiye.utils.array.copyTo
import com.mingliqiye.utils.base.BaseCodec
import com.mingliqiye.utils.base.BaseType
import com.mingliqiye.utils.base.code.Base256
import com.mingliqiye.utils.base.code.Base64
import com.mingliqiye.utils.base.code.Base91
import com.mingliqiye.utils.random.randomByte
import com.mingliqiye.utils.random.secureRandom
import com.mingliqiye.utils.system.macAddressBytes
@@ -83,17 +80,17 @@ class UUID : Serializable {
@JvmStatic
fun ofBase64ShortString(baseShortString: String): UUID {
return UUID(Base64.decode(baseShortString))
return UUID(BaseType.BASE64.decode(baseShortString))
}
@JvmStatic
fun ofBase256ShortString(baseShortString: String): UUID {
return UUID(Base256.decode(baseShortString))
return UUID(BaseType.BASE256.decode(baseShortString))
}
@JvmStatic
fun ofBase91ShortString(baseShortString: String): UUID {
return UUID(Base91.decode(baseShortString))
return UUID(BaseType.BASE91.decode(baseShortString))
}
@JvmStatic
@@ -747,15 +744,15 @@ class UUID : Serializable {
}
fun getBase64ShortString(): String {
return Base64.encode(data).substring(0, 22)
return BaseType.BASE64.encode(data).substring(0, 22)
}
fun getBase91ShortString(): String {
return Base91.encode(data)
return BaseType.BASE91.encode(data)
}
fun getBase256ShortString(): String {
return Base256.encode(data)
return BaseType.BASE256.encode(data)
}
/**