Compare commits
16
Commits
f7abd6c271
...
5.4.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
510ff69e2c
|
||
|
|
184db461d7
|
||
|
|
535c03d14a
|
||
|
|
5714aef444
|
||
|
|
7a3bc01b11
|
||
|
|
792ddf0255
|
||
|
|
f4025cbad9 | ||
|
|
eaf7ee7a31 | ||
|
|
38ad9884d0 | ||
|
|
0e5ce5597b | ||
|
|
85fd391849 | ||
|
|
390afa5462
|
||
|
|
31987b9b14 | ||
|
|
474071e43b
|
||
|
|
6624c00553
|
||
|
|
2c423c4aff
|
+3
-1
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils
|
* ModuleName mingli-utils
|
||||||
* CurrentFile build.gradle.kts
|
* CurrentFile build.gradle.kts
|
||||||
* LastUpdate 2026-10-11 16:44:37
|
* LastUpdate 2026-10-28 21:50:32
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -84,6 +84,8 @@ dependencies {
|
|||||||
compileOnly("net.java.dev.jna:jna:5.17.0")
|
compileOnly("net.java.dev.jna:jna:5.17.0")
|
||||||
compileOnly("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.8")
|
compileOnly("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.8")
|
||||||
compileOnly("com.baomidou:mybatis-plus-extension:3.5.15")
|
compileOnly("com.baomidou:mybatis-plus-extension:3.5.15")
|
||||||
|
compileOnly("com.baomidou:mybatis-plus-jsqlparser:3.5.15")
|
||||||
|
compileOnly("com.baomidou:mybatis-plus-spring:3.5.9")
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
|
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
testImplementation("com.mingliqiye.logger:logger-log4j2:1.0.9")
|
testImplementation("com.mingliqiye.logger:logger-log4j2:1.0.9")
|
||||||
|
|||||||
+2
-2
@@ -16,10 +16,10 @@
|
|||||||
# ProjectName mingli-utils
|
# ProjectName mingli-utils
|
||||||
# ModuleName mingli-utils
|
# ModuleName mingli-utils
|
||||||
# CurrentFile gradle.properties
|
# CurrentFile gradle.properties
|
||||||
# LastUpdate 2026-10-11 16:28:05
|
# LastUpdate 2026-11-09 19:49:52
|
||||||
# UpdateUser MingLiPro
|
# UpdateUser MingLiPro
|
||||||
#
|
#
|
||||||
JDKVERSIONS=17
|
JDKVERSIONS=17
|
||||||
GROUPSID=com.mingliqiye.utils
|
GROUPSID=com.mingliqiye.utils
|
||||||
ARTIFACTID=mingli-utils
|
ARTIFACTID=mingli-utils
|
||||||
VERSIONS=5.3.1
|
VERSIONS=5.4.3
|
||||||
|
|||||||
Binary file not shown.
@@ -13,10 +13,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* ProjectName mingli-utils
|
* ProjectName FastBaseMapper.kt
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile SuperStream.java
|
* CurrentFile SuperStream.java
|
||||||
* LastUpdate 2026-10-11 17:23:57
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 SuprtStreamUtils.kt
|
|
||||||
* LastUpdate 2026-10-11 17:32:17
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.mingliqiye.utils.stream
|
|
||||||
|
|
||||||
import java.util.stream.Stream
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [SuperStream] 的工厂工具类,提供把各类常见数据源(Java [Stream]、数组、集合、映射)
|
|
||||||
* 转换为 [SuperStream] 的扩展函数。
|
|
||||||
*
|
|
||||||
* 所有扩展函数均以 `toSuperStream` 命名,遵循 Kotlin 的 `toXxx` 转换命名惯例。
|
|
||||||
*
|
|
||||||
* 使用示例:
|
|
||||||
* ```kotlin
|
|
||||||
* intArrayOf(1, 2, 3).toSuperStream()
|
|
||||||
* listOf("a", "b").toSuperStream(parallel = true)
|
|
||||||
* mapOf(1 to "a").toSuperStream()
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
object SuperStreamUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 Java [Stream] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param T 流中元素的类型,必须是不可空类型。
|
|
||||||
* @return 包装当前 [Stream] 的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun <T : Any> Stream<T>.toSuperStream() = SuperStream.of(this)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [ByteArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun ByteArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [ShortArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun ShortArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [IntArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun IntArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [LongArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun LongArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [FloatArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun FloatArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [DoubleArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun DoubleArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [BooleanArray] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun BooleanArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将对象数组 [Array] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param T 数组元素类型,必须是不可空类型。
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前数组元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun <T : Any> Array<T>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [Collection] 转换为 [SuperStream]。
|
|
||||||
*
|
|
||||||
* @param T 集合元素类型,必须是不可空类型。
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前集合元素构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun <T : Any> Collection<T>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [Map] 转换为 [SuperStream],流中元素为键值对 [Map.Entry]。
|
|
||||||
*
|
|
||||||
* @param K 键的类型。
|
|
||||||
* @param V 值的类型。
|
|
||||||
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
|
||||||
* @return 由当前映射的条目构成的 [SuperStream]。
|
|
||||||
*/
|
|
||||||
fun <K, V> Map<K, V>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
|
||||||
}
|
|
||||||
@@ -37,5 +37,5 @@ import kotlin.reflect.KClass
|
|||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
@Retention(AnnotationRetention.SOURCE)
|
@Retention(AnnotationRetention.SOURCE)
|
||||||
annotation class AutoService(
|
annotation class AutoService(
|
||||||
val value: Array<KClass<*>> = []
|
val value: Array<KClass<*>> = [],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -34,5 +34,5 @@ import com.mingliqiye.utils.uuid.UUIDFormatType
|
|||||||
@Target(AnnotationTarget.FIELD)
|
@Target(AnnotationTarget.FIELD)
|
||||||
annotation class UUIDJsonFormat(
|
annotation class UUIDJsonFormat(
|
||||||
val value: UUIDFormatType = UUIDFormatType.NO_UPPER_SPACE,
|
val value: UUIDFormatType = UUIDFormatType.NO_UPPER_SPACE,
|
||||||
val base: BaseType = BaseType.BASE16
|
val base: BaseType = BaseType.BASE16,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ inline fun <reified T> Array<T>.getLast(): T? {
|
|||||||
* @return 指定索引处的元素或默认值
|
* @return 指定索引处的元素或默认值
|
||||||
*/
|
*/
|
||||||
inline fun <reified T> Collection<T>.getOrDefault(
|
inline fun <reified T> Collection<T>.getOrDefault(
|
||||||
index: Int, defaultValue: T
|
index: Int, defaultValue: T,
|
||||||
): T {
|
): T {
|
||||||
if (!(!this.isEmpty() && index < this.size)) {
|
if (!(!this.isEmpty() && index < this.size)) {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
|
|||||||
@@ -143,4 +143,3 @@ private fun getAllFields(clazz: Class<*>): List<Field> {
|
|||||||
}
|
}
|
||||||
return fields
|
return fields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile BadGatewayException.kt
|
* CurrentFile BadGatewayException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:07:34
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Bad Gateway"
|
* @param message 异常信息,默认为 "Bad Gateway"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class BadGatewayException(
|
class BadGatewayException @JvmOverloads constructor(
|
||||||
override val message: String? = "Bad Gateway",
|
override val message: String? = "Bad Gateway",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(502, message, cause)
|
) : HttpStatusException(502, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile BadRequestException.kt
|
* CurrentFile BadRequestException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Bad Request"
|
* @param message 异常信息,默认为 "Bad Request"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class BadRequestException(
|
class BadRequestException @JvmOverloads constructor(
|
||||||
override val message: String? = "Bad Request",
|
override val message: String? = "Bad Request",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(400, message, cause)
|
) : HttpStatusException(400, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile ConflictException.kt
|
* CurrentFile ConflictException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Conflict"
|
* @param message 异常信息,默认为 "Conflict"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class ConflictException(
|
class ConflictException @JvmOverloads constructor(
|
||||||
override val message: String? = "Conflict",
|
override val message: String? = "Conflict",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(409, message, cause)
|
) : HttpStatusException(409, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile ForbiddenException.kt
|
* CurrentFile ForbiddenException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Forbidden"
|
* @param message 异常信息,默认为 "Forbidden"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class ForbiddenException(
|
class ForbiddenException @JvmOverloads constructor(
|
||||||
override val message: String? = "Forbidden",
|
override val message: String? = "Forbidden",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(403, message, cause)
|
) : HttpStatusException(403, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile FoundException.kt
|
* CurrentFile FoundException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Found"
|
* @param message 异常信息,默认为 "Found"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class FoundException(
|
class FoundException @JvmOverloads constructor(
|
||||||
override val message: String? = "Found",
|
override val message: String? = "Found",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(302, message, cause)
|
) : HttpStatusException(302, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile GatewayTimeoutException.kt
|
* CurrentFile GatewayTimeoutException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Gateway Timeout"
|
* @param message 异常信息,默认为 "Gateway Timeout"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class GatewayTimeoutException(
|
class GatewayTimeoutException @JvmOverloads constructor(
|
||||||
override val message: String? = "Gateway Timeout",
|
override val message: String? = "Gateway Timeout",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(504, message, cause)
|
) : HttpStatusException(504, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile GoneException.kt
|
* CurrentFile GoneException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Gone"
|
* @param message 异常信息,默认为 "Gone"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class GoneException(
|
class GoneException @JvmOverloads constructor(
|
||||||
override val message: String? = "Gone",
|
override val message: String? = "Gone",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(410, message, cause)
|
) : HttpStatusException(410, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile HttpVersionNotSupportedException.kt
|
* CurrentFile HttpVersionNotSupportedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "HTTP Version Not Supported"
|
* @param message 异常信息,默认为 "HTTP Version Not Supported"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class HttpVersionNotSupportedException(
|
class HttpVersionNotSupportedException @JvmOverloads constructor(
|
||||||
override val message: String? = "HTTP Version Not Supported",
|
override val message: String? = "HTTP Version Not Supported",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(505, message, cause)
|
) : HttpStatusException(505, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile InternalServerErrorException.kt
|
* CurrentFile InternalServerErrorException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Internal Server Error"
|
* @param message 异常信息,默认为 "Internal Server Error"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class InternalServerErrorException(
|
class InternalServerErrorException @JvmOverloads constructor(
|
||||||
override val message: String? = "Internal Server Error",
|
override val message: String? = "Internal Server Error",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(500, message, cause)
|
) : HttpStatusException(500, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile LengthRequiredException.kt
|
* CurrentFile LengthRequiredException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Length Required"
|
* @param message 异常信息,默认为 "Length Required"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class LengthRequiredException(
|
class LengthRequiredException @JvmOverloads constructor(
|
||||||
override val message: String? = "Length Required",
|
override val message: String? = "Length Required",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(411, message, cause)
|
) : HttpStatusException(411, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile MethodNotAllowedException.kt
|
* CurrentFile MethodNotAllowedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Method Not Allowed"
|
* @param message 异常信息,默认为 "Method Not Allowed"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class MethodNotAllowedException(
|
class MethodNotAllowedException @JvmOverloads constructor(
|
||||||
override val message: String? = "Method Not Allowed",
|
override val message: String? = "Method Not Allowed",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(405, message, cause)
|
) : HttpStatusException(405, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile MovedPermanentlyException.kt
|
* CurrentFile MovedPermanentlyException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Moved Permanently"
|
* @param message 异常信息,默认为 "Moved Permanently"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class MovedPermanentlyException(
|
class MovedPermanentlyException @JvmOverloads constructor(
|
||||||
override val message: String? = "Moved Permanently",
|
override val message: String? = "Moved Permanently",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(301, message, cause)
|
) : HttpStatusException(301, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile MultipleChoicesException.kt
|
* CurrentFile MultipleChoicesException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Multiple Choices"
|
* @param message 异常信息,默认为 "Multiple Choices"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class MultipleChoicesException(
|
class MultipleChoicesException @JvmOverloads constructor(
|
||||||
override val message: String? = "Multiple Choices",
|
override val message: String? = "Multiple Choices",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(300, message, cause)
|
) : HttpStatusException(300, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile NotAcceptableException.kt
|
* CurrentFile NotAcceptableException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Not Acceptable"
|
* @param message 异常信息,默认为 "Not Acceptable"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class NotAcceptableException(
|
class NotAcceptableException @JvmOverloads constructor(
|
||||||
override val message: String? = "Not Acceptable",
|
override val message: String? = "Not Acceptable",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(406, message, cause)
|
) : HttpStatusException(406, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile NotFoundException.kt
|
* CurrentFile NotFoundException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Not Found"
|
* @param message 异常信息,默认为 "Not Found"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class NotFoundException(
|
class NotFoundException @JvmOverloads constructor(
|
||||||
override val message: String? = "Not Found",
|
override val message: String? = "Not Found",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(404, message, cause)
|
) : HttpStatusException(404, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile NotImplementedException.kt
|
* CurrentFile NotImplementedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Not Implemented"
|
* @param message 异常信息,默认为 "Not Implemented"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class NotImplementedException(
|
class NotImplementedException @JvmOverloads constructor(
|
||||||
override val message: String? = "Not Implemented",
|
override val message: String? = "Not Implemented",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(501, message, cause)
|
) : HttpStatusException(501, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile NotModifiedException.kt
|
* CurrentFile NotModifiedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Not Modified"
|
* @param message 异常信息,默认为 "Not Modified"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class NotModifiedException(
|
class NotModifiedException @JvmOverloads constructor(
|
||||||
override val message: String? = "Not Modified",
|
override val message: String? = "Not Modified",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(304, message, cause)
|
) : HttpStatusException(304, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile PaymentRequiredException.kt
|
* CurrentFile PaymentRequiredException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Payment Required"
|
* @param message 异常信息,默认为 "Payment Required"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class PaymentRequiredException(
|
class PaymentRequiredException @JvmOverloads constructor(
|
||||||
override val message: String? = "Payment Required",
|
override val message: String? = "Payment Required",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(402, message, cause)
|
) : HttpStatusException(402, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile PreconditionFailedException.kt
|
* CurrentFile PreconditionFailedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Precondition Failed"
|
* @param message 异常信息,默认为 "Precondition Failed"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class PreconditionFailedException(
|
class PreconditionFailedException @JvmOverloads constructor(
|
||||||
override val message: String? = "Precondition Failed",
|
override val message: String? = "Precondition Failed",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(412, message, cause)
|
) : HttpStatusException(412, message, cause)
|
||||||
|
|||||||
+2
-2
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile ProxyAuthenticationRequiredException.kt
|
* CurrentFile ProxyAuthenticationRequiredException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Proxy Authentication Required"
|
* @param message 异常信息,默认为 "Proxy Authentication Required"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class ProxyAuthenticationRequiredException(
|
class ProxyAuthenticationRequiredException @JvmOverloads constructor(
|
||||||
override val message: String? = "Proxy Authentication Required",
|
override val message: String? = "Proxy Authentication Required",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(407, message, cause)
|
) : HttpStatusException(407, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile RequestEntityTooLargeException.kt
|
* CurrentFile RequestEntityTooLargeException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Request Entity Too Large"
|
* @param message 异常信息,默认为 "Request Entity Too Large"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class RequestEntityTooLargeException(
|
class RequestEntityTooLargeException @JvmOverloads constructor(
|
||||||
override val message: String? = "Request Entity Too Large",
|
override val message: String? = "Request Entity Too Large",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(413, message, cause)
|
) : HttpStatusException(413, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile RequestTimeoutException.kt
|
* CurrentFile RequestTimeoutException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Request Timeout"
|
* @param message 异常信息,默认为 "Request Timeout"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class RequestTimeoutException(
|
class RequestTimeoutException @JvmOverloads constructor(
|
||||||
override val message: String? = "Request Timeout",
|
override val message: String? = "Request Timeout",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(408, message, cause)
|
) : HttpStatusException(408, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile RequestUriTooLargeException.kt
|
* CurrentFile RequestUriTooLargeException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Request URI Too Large"
|
* @param message 异常信息,默认为 "Request URI Too Large"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class RequestUriTooLargeException(
|
class RequestUriTooLargeException @JvmOverloads constructor(
|
||||||
override val message: String? = "Request URI Too Large",
|
override val message: String? = "Request URI Too Large",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(414, message, cause)
|
) : HttpStatusException(414, message, cause)
|
||||||
|
|||||||
+2
-2
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile RequestedRangeNotSatisfiableException.kt
|
* CurrentFile RequestedRangeNotSatisfiableException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Requested Range Not Satisfiable"
|
* @param message 异常信息,默认为 "Requested Range Not Satisfiable"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class RequestedRangeNotSatisfiableException(
|
class RequestedRangeNotSatisfiableException @JvmOverloads constructor(
|
||||||
override val message: String? = "Requested Range Not Satisfiable",
|
override val message: String? = "Requested Range Not Satisfiable",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(416, message, cause)
|
) : HttpStatusException(416, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile SeeOtherException.kt
|
* CurrentFile SeeOtherException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "See Other"
|
* @param message 异常信息,默认为 "See Other"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class SeeOtherException(
|
class SeeOtherException @JvmOverloads constructor(
|
||||||
override val message: String? = "See Other",
|
override val message: String? = "See Other",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(303, message, cause)
|
) : HttpStatusException(303, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile ServiceUnavailableException.kt
|
* CurrentFile ServiceUnavailableException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Service Unavailable"
|
* @param message 异常信息,默认为 "Service Unavailable"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class ServiceUnavailableException(
|
class ServiceUnavailableException @JvmOverloads constructor(
|
||||||
override val message: String? = "Service Unavailable",
|
override val message: String? = "Service Unavailable",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(503, message, cause)
|
) : HttpStatusException(503, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile TemporaryRedirectException.kt
|
* CurrentFile TemporaryRedirectException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Temporary Redirect"
|
* @param message 异常信息,默认为 "Temporary Redirect"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class TemporaryRedirectException(
|
class TemporaryRedirectException @JvmOverloads constructor(
|
||||||
override val message: String? = "Temporary Redirect",
|
override val message: String? = "Temporary Redirect",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(307, message, cause)
|
) : HttpStatusException(307, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile UnauthorizedException.kt
|
* CurrentFile UnauthorizedException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Unauthorized"
|
* @param message 异常信息,默认为 "Unauthorized"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class UnauthorizedException(
|
class UnauthorizedException @JvmOverloads constructor(
|
||||||
override val message: String? = "Unauthorized",
|
override val message: String? = "Unauthorized",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(401, message, cause)
|
) : HttpStatusException(401, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile UnsupportedMediaTypeException.kt
|
* CurrentFile UnsupportedMediaTypeException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:19
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Unsupported Media Type"
|
* @param message 异常信息,默认为 "Unsupported Media Type"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class UnsupportedMediaTypeException(
|
class UnsupportedMediaTypeException @JvmOverloads constructor(
|
||||||
override val message: String? = "Unsupported Media Type",
|
override val message: String? = "Unsupported Media Type",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(415, message, cause)
|
) : HttpStatusException(415, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile UseProxyException.kt
|
* CurrentFile UseProxyException.kt
|
||||||
* LastUpdate 2026-02-05 14:55:04
|
* LastUpdate 2026-10-22 22:09:48
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ package com.mingliqiye.utils.exception
|
|||||||
* @param message 异常信息,默认为 "Use Proxy"
|
* @param message 异常信息,默认为 "Use Proxy"
|
||||||
* @param cause 异常原因,默认为 null
|
* @param cause 异常原因,默认为 null
|
||||||
*/
|
*/
|
||||||
class UseProxyException(
|
class UseProxyException @JvmOverloads constructor(
|
||||||
override val message: String? = "Use Proxy",
|
override val message: String? = "Use Proxy",
|
||||||
override val cause: Throwable? = null,
|
override val cause: Throwable? = null,
|
||||||
) : HttpStatusException(305, message, cause)
|
) : HttpStatusException(305, message, cause)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile FileUtils.kt
|
* CurrentFile FileUtils.kt
|
||||||
* LastUpdate 2026-01-28 10:49:14
|
* LastUpdate 2026-10-28 22:40:34
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
@file:JvmName("FileUtils")
|
@file:JvmName("FileUtils")
|
||||||
@@ -627,7 +627,9 @@ fun File.writeLines(lines: List<String>) {
|
|||||||
*/
|
*/
|
||||||
fun File.writeLines(lines: List<String>, charset: Charset) {
|
fun File.writeLines(lines: List<String>, charset: Charset) {
|
||||||
this.parentFile?.mkdirs()
|
this.parentFile?.mkdirs()
|
||||||
this.writeLines(lines, charset)
|
for (line in lines) {
|
||||||
|
this.writeText(line + System.lineSeparator(), charset)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 复制文件
|
// 复制文件
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ import com.mingliqiye.utils.functions.*
|
|||||||
* @param <T> 可迭代对象中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEach(
|
fun <T> forEach(
|
||||||
iterable: Iterable<T>, action: P2Function<in T, in Int>
|
iterable: Iterable<T>, action: P2Function<in T, in Int>,
|
||||||
) {
|
) {
|
||||||
when (iterable) {
|
when (iterable) {
|
||||||
is RandomAccess if iterable is MutableList<*> -> {
|
is RandomAccess if iterable is MutableList<*> -> {
|
||||||
@@ -74,7 +74,7 @@ fun <T> forEach(
|
|||||||
* @param <T> 可迭代对象中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEach(
|
fun <T> forEach(
|
||||||
iterable: Iterable<T>, action: P1Function<in T>
|
iterable: Iterable<T>, action: P1Function<in T>,
|
||||||
) {
|
) {
|
||||||
if (iterable is RandomAccess) {
|
if (iterable is RandomAccess) {
|
||||||
// 如果是支持随机访问的列表,则使用索引遍历以提高性能
|
// 如果是支持随机访问的列表,则使用索引遍历以提高性能
|
||||||
@@ -100,7 +100,7 @@ fun <T> forEach(
|
|||||||
* @param <T> 可迭代对象中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEachB(
|
fun <T> forEachB(
|
||||||
iterable: Iterable<T>, action: P2RFunction<in T, in Int, out Boolean>
|
iterable: Iterable<T>, action: P2RFunction<in T, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
when (iterable) {
|
when (iterable) {
|
||||||
is RandomAccess if iterable is MutableList<*> -> {
|
is RandomAccess if iterable is MutableList<*> -> {
|
||||||
@@ -142,7 +142,7 @@ fun <T> forEachB(
|
|||||||
* @param <T> 可迭代对象中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEachB(
|
fun <T> forEachB(
|
||||||
iterable: Iterable<T>, action: P1RFunction<in T, out Boolean>
|
iterable: Iterable<T>, action: P1RFunction<in T, out Boolean>,
|
||||||
) {
|
) {
|
||||||
if (iterable is RandomAccess) {
|
if (iterable is RandomAccess) {
|
||||||
// 如果是支持随机访问的列表,则使用索引遍历以提高性能
|
// 如果是支持随机访问的列表,则使用索引遍历以提高性能
|
||||||
@@ -166,7 +166,7 @@ fun <T> forEachB(
|
|||||||
* @param <T> 数组中元素的类型
|
* @param <T> 数组中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEach(
|
fun <T> forEach(
|
||||||
array: Array<T>, action: P2Function<in T, in Int>
|
array: Array<T>, action: P2Function<in T, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ fun <T> forEach(
|
|||||||
* @param <T> 数组中元素的类型
|
* @param <T> 数组中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEach(
|
fun <T> forEach(
|
||||||
array: Array<T>, action: P1Function<in T>
|
array: Array<T>, action: P1Function<in T>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -193,7 +193,7 @@ fun <T> forEach(
|
|||||||
* @param <T> 数组中元素的类型
|
* @param <T> 数组中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEachB(
|
fun <T> forEachB(
|
||||||
array: Array<T>, action: P2RFunction<in T, in Int, out Boolean>
|
array: Array<T>, action: P2RFunction<in T, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -207,7 +207,7 @@ fun <T> forEachB(
|
|||||||
* @param <T> 数组中元素的类型
|
* @param <T> 数组中元素的类型
|
||||||
*/
|
*/
|
||||||
fun <T> forEachB(
|
fun <T> forEachB(
|
||||||
array: Array<T>, action: P1RFunction<in T, out Boolean>
|
array: Array<T>, action: P1RFunction<in T, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -219,7 +219,7 @@ fun <T> forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: ByteArray, action: P2Function<in Byte, in Int>
|
array: ByteArray, action: P2Function<in Byte, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: ByteArray, action: P1Function<in Byte>
|
array: ByteArray, action: P1Function<in Byte>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -244,7 +244,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: ByteArray, action: P2RFunction<in Byte, in Int, out Boolean>
|
array: ByteArray, action: P2RFunction<in Byte, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -257,7 +257,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: ByteArray, action: P1RFunction<in Byte, out Boolean>
|
array: ByteArray, action: P1RFunction<in Byte, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -269,7 +269,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: CharArray, action: P2Function<in Char, in Int>
|
array: CharArray, action: P2Function<in Char, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -281,7 +281,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: CharArray, action: P1Function<in Char>
|
array: CharArray, action: P1Function<in Char>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -294,7 +294,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: CharArray, action: P2RFunction<in Char, in Int, out Boolean>
|
array: CharArray, action: P2RFunction<in Char, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: CharArray, action: P1RFunction<in Char, out Boolean>
|
array: CharArray, action: P1RFunction<in Char, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -319,7 +319,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: IntArray, action: P2Function<in Int, in Int>
|
array: IntArray, action: P2Function<in Int, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -331,7 +331,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: IntArray, action: P1Function<in Int>
|
array: IntArray, action: P1Function<in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -344,7 +344,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: IntArray, action: P2RFunction<in Int, in Int, out Boolean>
|
array: IntArray, action: P2RFunction<in Int, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -357,7 +357,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: IntArray, action: P1RFunction<in Int, out Boolean>
|
array: IntArray, action: P1RFunction<in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -369,7 +369,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: LongArray, action: P2Function<in Long, in Int>
|
array: LongArray, action: P2Function<in Long, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -381,7 +381,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: LongArray, action: P1Function<in Long>
|
array: LongArray, action: P1Function<in Long>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -394,7 +394,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: LongArray, action: P2RFunction<in Long, in Int, out Boolean>
|
array: LongArray, action: P2RFunction<in Long, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -407,7 +407,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: LongArray, action: P1RFunction<in Long, out Boolean>
|
array: LongArray, action: P1RFunction<in Long, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -419,7 +419,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: ShortArray, action: P2Function<in Short, in Int>
|
array: ShortArray, action: P2Function<in Short, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -431,7 +431,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: ShortArray, action: P1Function<in Short>
|
array: ShortArray, action: P1Function<in Short>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -444,7 +444,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: ShortArray, action: P2RFunction<in Short, in Int, out Boolean>
|
array: ShortArray, action: P2RFunction<in Short, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -457,7 +457,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: ShortArray, action: P1RFunction<in Short, out Boolean>
|
array: ShortArray, action: P1RFunction<in Short, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -469,7 +469,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: FloatArray, action: P2Function<in Float, in Int>
|
array: FloatArray, action: P2Function<in Float, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -481,7 +481,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: FloatArray, action: P1Function<in Float>
|
array: FloatArray, action: P1Function<in Float>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -494,7 +494,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: FloatArray, action: P2RFunction<in Float, in Int, out Boolean>
|
array: FloatArray, action: P2RFunction<in Float, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -507,7 +507,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: FloatArray, action: P1RFunction<in Float, out Boolean>
|
array: FloatArray, action: P1RFunction<in Float, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -519,7 +519,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: DoubleArray, action: P2Function<in Double, in Int>
|
array: DoubleArray, action: P2Function<in Double, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -531,7 +531,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: DoubleArray, action: P1Function<in Double>
|
array: DoubleArray, action: P1Function<in Double>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -544,7 +544,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: DoubleArray, action: P2RFunction<in Double, in Int, out Boolean>
|
array: DoubleArray, action: P2RFunction<in Double, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -557,7 +557,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: DoubleArray, action: P1RFunction<in Double, out Boolean>
|
array: DoubleArray, action: P1RFunction<in Double, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -569,7 +569,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: BooleanArray, action: P2Function<in Boolean, in Int>
|
array: BooleanArray, action: P2Function<in Boolean, in Int>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -581,7 +581,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
*/
|
*/
|
||||||
fun forEach(
|
fun forEach(
|
||||||
array: BooleanArray, action: P1Function<in Boolean>
|
array: BooleanArray, action: P1Function<in Boolean>,
|
||||||
) {
|
) {
|
||||||
forEach(array.toList(), action)
|
forEach(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -594,7 +594,7 @@ fun forEach(
|
|||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: BooleanArray, action: P2RFunction<in Boolean, in Int, out Boolean>
|
array: BooleanArray, action: P2RFunction<in Boolean, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -607,7 +607,7 @@ fun forEachB(
|
|||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数,返回 Boolean 表示是否提前终止
|
||||||
*/
|
*/
|
||||||
fun forEachB(
|
fun forEachB(
|
||||||
array: BooleanArray, action: P1RFunction<in Boolean, out Boolean>
|
array: BooleanArray, action: P1RFunction<in Boolean, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array.toList(), action)
|
forEachB(array.toList(), action)
|
||||||
}
|
}
|
||||||
@@ -623,7 +623,7 @@ fun forEachB(
|
|||||||
* @param <A> 集合的具体类型
|
* @param <A> 集合的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachB(
|
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachB(
|
||||||
array: A, action: P3RFunction<in K, in V, in Int, out Boolean>
|
array: A, action: P3RFunction<in K, in V, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array, P2RFunction<Map.Entry<K, V>, Int, Boolean> { p1, p2 -> action.call(p1.key, p1.value, p2) })
|
forEachB(array, P2RFunction<Map.Entry<K, V>, Int, Boolean> { p1, p2 -> action.call(p1.key, p1.value, p2) })
|
||||||
}
|
}
|
||||||
@@ -639,7 +639,7 @@ fun <K, V, A : Collection<Map.Entry<K, V>>> forEachB(
|
|||||||
* @param <A> 映射的具体类型
|
* @param <A> 映射的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Map<K, V>> forEachB(
|
fun <K, V, A : Map<K, V>> forEachB(
|
||||||
map: A, action: P3RFunction<in K, in V, in Int, out Boolean>
|
map: A, action: P3RFunction<in K, in V, in Int, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(map.entries, P2RFunction<Map.Entry<K, V>, Int, Boolean> { p1, p2 -> action.call(p1.key, p1.value, p2) })
|
forEachB(map.entries, P2RFunction<Map.Entry<K, V>, Int, Boolean> { p1, p2 -> action.call(p1.key, p1.value, p2) })
|
||||||
}
|
}
|
||||||
@@ -654,7 +654,7 @@ fun <K, V, A : Map<K, V>> forEachB(
|
|||||||
* @param <A> 集合的具体类型
|
* @param <A> 集合的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMap(
|
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMap(
|
||||||
array: A, action: P2Function<in K, in V>
|
array: A, action: P2Function<in K, in V>,
|
||||||
) {
|
) {
|
||||||
forEach(array, P1Function<Map.Entry<K, V>> { p1 -> action.call(p1.key, p1.value) })
|
forEach(array, P1Function<Map.Entry<K, V>> { p1 -> action.call(p1.key, p1.value) })
|
||||||
}
|
}
|
||||||
@@ -669,7 +669,7 @@ fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMap(
|
|||||||
* @param <A> 映射的具体类型
|
* @param <A> 映射的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Map<K, V>> forEachMap(
|
fun <K, V, A : Map<K, V>> forEachMap(
|
||||||
map: A, action: P2Function<in K, in V>
|
map: A, action: P2Function<in K, in V>,
|
||||||
) {
|
) {
|
||||||
forEach(map.entries, P1Function<Map.Entry<K, V>> { p1 -> action.call(p1.key, p1.value) })
|
forEach(map.entries, P1Function<Map.Entry<K, V>> { p1 -> action.call(p1.key, p1.value) })
|
||||||
}
|
}
|
||||||
@@ -685,7 +685,7 @@ fun <K, V, A : Map<K, V>> forEachMap(
|
|||||||
* @param <A> 集合的具体类型
|
* @param <A> 集合的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMapB(
|
fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMapB(
|
||||||
array: A, action: P2RFunction<in K, in V, out Boolean>
|
array: A, action: P2RFunction<in K, in V, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(array, P1RFunction<Map.Entry<K, V>, Boolean> { p1 -> action.call(p1.key, p1.value) })
|
forEachB(array, P1RFunction<Map.Entry<K, V>, Boolean> { p1 -> action.call(p1.key, p1.value) })
|
||||||
}
|
}
|
||||||
@@ -701,7 +701,7 @@ fun <K, V, A : Collection<Map.Entry<K, V>>> forEachMapB(
|
|||||||
* @param <A> 映射的具体类型
|
* @param <A> 映射的具体类型
|
||||||
*/
|
*/
|
||||||
fun <K, V, A : Map<K, V>> forEachMapB(
|
fun <K, V, A : Map<K, V>> forEachMapB(
|
||||||
map: A, action: P2RFunction<in K, in V, out Boolean>
|
map: A, action: P2RFunction<in K, in V, out Boolean>,
|
||||||
) {
|
) {
|
||||||
forEachB(map.entries, P1RFunction<Map.Entry<K, V>, Boolean> { p1 -> action.call(p1.key, p1.value) })
|
forEachB(map.entries, P1RFunction<Map.Entry<K, V>, Boolean> { p1 -> action.call(p1.key, p1.value) })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ fun interface P13RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p9: P9,
|
p9: P9,
|
||||||
p10: P10,
|
p10: P10,
|
||||||
p11: P11,
|
p11: P11,
|
||||||
p12: P12
|
p12: P12,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,7 +202,7 @@ fun interface P14Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p10: P10,
|
p10: P10,
|
||||||
p11: P11,
|
p11: P11,
|
||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13
|
p13: P13,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +222,7 @@ fun interface P14RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p10: P10,
|
p10: P10,
|
||||||
p11: P11,
|
p11: P11,
|
||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13
|
p13: P13,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +243,7 @@ fun interface P15Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p11: P11,
|
p11: P11,
|
||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14
|
p14: P14,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +264,7 @@ fun interface P15RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p11: P11,
|
p11: P11,
|
||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14
|
p14: P14,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +286,7 @@ fun interface P16Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15
|
p15: P15,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +308,7 @@ fun interface P16RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p12: P12,
|
p12: P12,
|
||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15
|
p15: P15,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +331,7 @@ fun interface P17Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16
|
p16: P16,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,7 +354,7 @@ fun interface P17RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p13: P13,
|
p13: P13,
|
||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16
|
p16: P16,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +378,7 @@ fun interface P18Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17
|
p17: P17,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,7 +402,7 @@ fun interface P18RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p14: P14,
|
p14: P14,
|
||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17
|
p17: P17,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -427,7 +427,7 @@ fun interface P19Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18
|
p18: P18,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,7 +452,7 @@ fun interface P19RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p15: P15,
|
p15: P15,
|
||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18
|
p18: P18,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,7 +478,7 @@ fun interface P20Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19
|
p19: P19,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,7 +504,7 @@ fun interface P20RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p16: P16,
|
p16: P16,
|
||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19
|
p19: P19,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ fun interface P21Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20
|
p20: P20,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,7 +558,7 @@ fun interface P21RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p17: P17,
|
p17: P17,
|
||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20
|
p20: P20,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,7 +586,7 @@ fun interface P22Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21
|
p21: P21,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -614,7 +614,7 @@ fun interface P22RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p18: P18,
|
p18: P18,
|
||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21
|
p21: P21,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -643,7 +643,7 @@ fun interface P23Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21,
|
p21: P21,
|
||||||
p22: P22
|
p22: P22,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -672,7 +672,7 @@ fun interface P23RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p19: P19,
|
p19: P19,
|
||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21,
|
p21: P21,
|
||||||
p22: P22
|
p22: P22,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -702,7 +702,7 @@ fun interface P24Function<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21,
|
p21: P21,
|
||||||
p22: P22,
|
p22: P22,
|
||||||
p23: P23
|
p23: P23,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -732,6 +732,6 @@ fun interface P24RFunction<P, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12,
|
|||||||
p20: P20,
|
p20: P20,
|
||||||
p21: P21,
|
p21: P21,
|
||||||
p22: P22,
|
p22: P22,
|
||||||
p23: P23
|
p23: P23,
|
||||||
): R
|
): R
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,13 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Pipeline.kt
|
* CurrentFile Pipeline.kt
|
||||||
* LastUpdate 2026-02-24 09:10:26
|
* LastUpdate 2026-10-20 20:59:13
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.mingliqiye.utils.functions
|
package com.mingliqiye.utils.functions
|
||||||
|
|
||||||
import com.mingliqiye.utils.require.Require
|
import com.mingliqiye.utils.require.requireTrue
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,9 +159,9 @@ class Pipeline<T>(private val value: T) {
|
|||||||
* @return 当前Pipeline实例
|
* @return 当前Pipeline实例
|
||||||
*/
|
*/
|
||||||
fun require(
|
fun require(
|
||||||
any: Any, message: String, exception: Class<out Exception> = IllegalArgumentException::class.java
|
any: Any, message: String, exception: Class<out Exception> = IllegalArgumentException::class.java,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(any == value, message, exception)
|
requireTrue(any == value, exception, message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,9 +173,9 @@ class Pipeline<T>(private val value: T) {
|
|||||||
* @return 当前Pipeline实例
|
* @return 当前Pipeline实例
|
||||||
*/
|
*/
|
||||||
fun require(
|
fun require(
|
||||||
any: Any, message: String
|
any: Any, message: String,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(any == value, message)
|
requireTrue(any == value, message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,9 +189,9 @@ class Pipeline<T>(private val value: T) {
|
|||||||
fun require(
|
fun require(
|
||||||
must: P1RFunction<T, Boolean>,
|
must: P1RFunction<T, Boolean>,
|
||||||
message: String,
|
message: String,
|
||||||
exception: Class<out Throwable> = IllegalArgumentException::class.java
|
exception: Class<out Throwable> = IllegalArgumentException::class.java,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(must.call(value), message, exception)
|
requireTrue(must.call(value), exception, message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,9 +205,9 @@ class Pipeline<T>(private val value: T) {
|
|||||||
fun require(
|
fun require(
|
||||||
any: Any,
|
any: Any,
|
||||||
message: String,
|
message: String,
|
||||||
exception: KClass<out Throwable> = IllegalArgumentException::class
|
exception: KClass<out Throwable> = IllegalArgumentException::class,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(any == value, message, exception)
|
requireTrue(any == value, exception.java, message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,11 +219,11 @@ class Pipeline<T>(private val value: T) {
|
|||||||
* @return 当前Pipeline实例
|
* @return 当前Pipeline实例
|
||||||
*/
|
*/
|
||||||
fun require(
|
fun require(
|
||||||
must: P1RFunction<T, Boolean>,
|
must: (T) -> Boolean,
|
||||||
message: String,
|
message: String,
|
||||||
exception: KClass<out Throwable> = IllegalArgumentException::class
|
exception: KClass<out Throwable> = IllegalArgumentException::class,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(must.call(value), message, exception)
|
requireTrue(must(value), exception.java, message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,9 +234,9 @@ class Pipeline<T>(private val value: T) {
|
|||||||
* @return 当前Pipeline实例
|
* @return 当前Pipeline实例
|
||||||
*/
|
*/
|
||||||
fun require(
|
fun require(
|
||||||
must: P1RFunction<T, Boolean>, message: String
|
must: P1RFunction<T, Boolean>, message: String,
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
Require.require(must.call(value), message, IllegalArgumentException::class.java)
|
requireTrue(must.call(value), message)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +259,7 @@ class Pipeline<T>(private val value: T) {
|
|||||||
*/
|
*/
|
||||||
fun require(
|
fun require(
|
||||||
predicate: P1RFunction<T, Boolean>,
|
predicate: P1RFunction<T, Boolean>,
|
||||||
lazyMessage: P1RFunction<T, Any> = P1RFunction { "Requirement failed" }
|
lazyMessage: P1RFunction<T, Any> = P1RFunction { "Requirement failed" },
|
||||||
): Pipeline<T> {
|
): Pipeline<T> {
|
||||||
if (!predicate.call(value)) {
|
if (!predicate.call(value)) {
|
||||||
throw IllegalArgumentException(lazyMessage.call(value).toString())
|
throw IllegalArgumentException(lazyMessage.call(value).toString())
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseMapper.kt
|
||||||
|
* ModuleName mingli-utils.main
|
||||||
|
* CurrentFile BaseResponse.kt
|
||||||
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.http
|
||||||
|
|
||||||
|
open class BaseResponse<T>(
|
||||||
|
open val time: String,
|
||||||
|
open var message: String,
|
||||||
|
open var data: T,
|
||||||
|
open var statusCode: Int,
|
||||||
|
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* 返回响应对象的字符串表示
|
||||||
|
*
|
||||||
|
* 重写toString方法以提供更有意义的对象表示,便于日志记录和调试。
|
||||||
|
* 显示时间、消息、数据类型和值以及状态码。
|
||||||
|
*
|
||||||
|
* @return 格式化的字符串表示
|
||||||
|
*/
|
||||||
|
override fun toString(): String =
|
||||||
|
"${javaClass.simpleName}(time=${time}, message=${message}, data=${data}, statusCode=${statusCode})"
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseMapper.kt
|
||||||
|
* ModuleName mingli-utils.main
|
||||||
|
* CurrentFile PageData.kt
|
||||||
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.http
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页数据类
|
||||||
|
*/
|
||||||
|
data class PageData<T>(
|
||||||
|
/**
|
||||||
|
* 当前页的数据列表
|
||||||
|
*/
|
||||||
|
val records: Collection<T> = emptyList(),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总条数(总记录数)
|
||||||
|
*/
|
||||||
|
val totalCount: Long = 0,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总页数
|
||||||
|
*/
|
||||||
|
val totalPages: Long = 0,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每页大小
|
||||||
|
*/
|
||||||
|
val pageSize: Long = 0,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前页码(从 1 开始)
|
||||||
|
*/
|
||||||
|
val pageIndex: Long = 0,
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun <T> of(page: Page<T>): PageData<T> = PageData(
|
||||||
|
records = page.records,
|
||||||
|
totalCount = page.total,
|
||||||
|
totalPages = page.pages,
|
||||||
|
pageSize = page.size,
|
||||||
|
pageIndex = page.current,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,216 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseMapper.kt
|
||||||
|
* ModuleName mingli-utils.main
|
||||||
|
* CurrentFile PagedResponse.kt
|
||||||
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.http
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
|
import com.mingliqiye.utils.time.DateTime
|
||||||
|
|
||||||
|
data class PagedResponse<T>(
|
||||||
|
override val time: String, // 响应时间,一旦创建不可修改
|
||||||
|
override var message: String, // 响应消息,可通过setter修改
|
||||||
|
override var data: PageData<T>, // 响应数据,可通过setter修改
|
||||||
|
override var statusCode: Int, // HTTP状态码,可通过setter修改
|
||||||
|
) : BaseResponse<PageData<T>>(
|
||||||
|
time,
|
||||||
|
message,
|
||||||
|
data,
|
||||||
|
statusCode
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* 创建一个默认的成功响应对象
|
||||||
|
* 使用默认消息"操作成功",无数据,状态码200
|
||||||
|
*
|
||||||
|
* @return Response<Nothing> 默认的成功响应对象
|
||||||
|
*/
|
||||||
|
fun ok() = ok("操作成功")
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个默认的错误响应对象
|
||||||
|
* 使用默认消息"服务器错误",无数据,状态码500
|
||||||
|
*
|
||||||
|
* @return Response<Nothing> 默认的成功响应对象
|
||||||
|
*/
|
||||||
|
fun error() = error("服务器错误")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个成功的响应对象(仅包含消息)
|
||||||
|
*
|
||||||
|
* @param message 响应消息
|
||||||
|
* @return Response<Any> 成功的响应对象,数据为null
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun ok(message: String) = PagedResponse(
|
||||||
|
time = DateTime.now().format(), message = message, data = PageData<Nothing>(), 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> 成功的响应对象,包含数据
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun <T> okData(data: PageData<T>) = PagedResponse(
|
||||||
|
time = DateTime.now().format(), message = "操作成功", data = data, statusCode = 200
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从mybatisplus的page构建
|
||||||
|
*/
|
||||||
|
@JvmOverloads
|
||||||
|
fun <T> okPgae(
|
||||||
|
page: Page<T>,
|
||||||
|
time: String = DateTime.now().format(),
|
||||||
|
message: String = "操作成功",
|
||||||
|
statusCode: Int = 200,
|
||||||
|
): PagedResponse<T> {
|
||||||
|
|
||||||
|
return PagedResponse(
|
||||||
|
time = time, message = message, data = PageData.of(page), statusCode = statusCode
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个成功的响应对象(包含数据和消息)
|
||||||
|
*
|
||||||
|
* @param T 响应数据的类型
|
||||||
|
* @param data 响应数据
|
||||||
|
* @param message 响应消息
|
||||||
|
* @return Response<T> 成功的响应对象,包含数据和消息
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun <T> okData(data: PageData<T>, message: String) = PagedResponse(
|
||||||
|
time = DateTime.now().format(), message = message, data = data, statusCode = 200
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个指定状态码的响应对象(仅包含状态码)
|
||||||
|
* 使用默认消息"操作成功",无数据
|
||||||
|
*
|
||||||
|
* @param statusCode HTTP状态码
|
||||||
|
* @return Response<Any> 指定状态码的响应对象
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun code(statusCode: Int) = PagedResponse(
|
||||||
|
time = DateTime.now().format(), message = "操作成功", data = PageData<Nothing>(), statusCode = statusCode
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个指定状态码的响应对象(包含状态码和消息)
|
||||||
|
* 无数据
|
||||||
|
*
|
||||||
|
* @param statusCode HTTP状态码
|
||||||
|
* @param message 响应消息
|
||||||
|
* @return Response<Any> 指定状态码和消息的响应对象
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun code(statusCode: Int, message: String) = PagedResponse(
|
||||||
|
time = DateTime.now().format(), message = message, data = PageData<Nothing>(), statusCode = statusCode
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认构造函数,创建一个默认的成功响应对象
|
||||||
|
* 使用当前时间、默认消息"操作成功"、状态码200、无数据
|
||||||
|
*/
|
||||||
|
constructor() : this(
|
||||||
|
time = DateTime.now().format(), message = "操作成功", statusCode = 200, data = PageData()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置响应数据
|
||||||
|
*
|
||||||
|
* 该方法允许修改响应数据,并返回当前对象以支持链式调用。
|
||||||
|
*
|
||||||
|
* @param data 新的响应数据
|
||||||
|
* @return Response<T> 当前响应对象(支持链式调用)
|
||||||
|
*/
|
||||||
|
fun setData(data: PageData<T>): PagedResponse<T> {
|
||||||
|
this.data = data
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的Response对象,使用指定的时间
|
||||||
|
*
|
||||||
|
* 该方法用于创建具有新时间的响应对象,遵循不可变对象的设计模式。
|
||||||
|
* 如果时间相同,则返回原对象避免不必要的对象创建。
|
||||||
|
*
|
||||||
|
* @param time 新的响应时间
|
||||||
|
* @return Response<T> 新的响应对象(如果时间相同则返回原对象)
|
||||||
|
*/
|
||||||
|
fun withTime(time: String): PagedResponse<T> =
|
||||||
|
if (this.time == time) this else PagedResponse(time, message, data, statusCode)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的Response对象,使用指定的数据
|
||||||
|
*
|
||||||
|
* 该方法允许改变响应数据的类型,创建一个新的响应对象。
|
||||||
|
* 适用于需要在保持其他字段不变的情况下更新数据类型的场景。
|
||||||
|
*
|
||||||
|
* @param D 新的数据类型
|
||||||
|
* @param data 新的响应数据
|
||||||
|
* @return Response<D> 新的响应对象,数据类型为D
|
||||||
|
*/
|
||||||
|
fun <D> withData(data: PageData<D>): PagedResponse<D> =
|
||||||
|
PagedResponse(this.time, this.message, data, this.statusCode)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的Response对象,使用指定的消息
|
||||||
|
*
|
||||||
|
* 该方法用于创建具有新消息的响应对象,遵循不可变对象的设计模式。
|
||||||
|
* 如果消息相同,则返回原对象避免不必要的对象创建。
|
||||||
|
*
|
||||||
|
* @param message 新的响应消息
|
||||||
|
* @return Response<T> 新的响应对象(如果消息相同则返回原对象)
|
||||||
|
*/
|
||||||
|
fun withMessage(message: String): PagedResponse<T> =
|
||||||
|
if (this.message == message) this else PagedResponse(time, message, data, statusCode)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的Response对象,使用指定的状态码
|
||||||
|
*
|
||||||
|
* 该方法用于创建具有新状态码的响应对象,遵循不可变对象的设计模式。
|
||||||
|
* 如果状态码相同,则返回原对象避免不必要的对象创建。
|
||||||
|
*
|
||||||
|
* @param statusCode 新的状态码
|
||||||
|
* @return Response<T> 新的响应对象(如果状态码相同则返回原对象)
|
||||||
|
*/
|
||||||
|
fun withStatusCode(statusCode: Int): PagedResponse<T> =
|
||||||
|
if (this.statusCode == statusCode) this else PagedResponse(time, message, data, statusCode)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,10 +13,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* ProjectName mingli-utils
|
* ProjectName FastBaseMapper.kt
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Response.kt
|
* CurrentFile Response.kt
|
||||||
* LastUpdate 2026-03-14 08:01:32
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -38,10 +38,15 @@ import com.mingliqiye.utils.time.DateTime
|
|||||||
* @property statusCode HTTP状态码
|
* @property statusCode HTTP状态码
|
||||||
*/
|
*/
|
||||||
data class Response<T>(
|
data class Response<T>(
|
||||||
private val time: String, // 响应时间,一旦创建不可修改
|
override val time: String, // 响应时间,一旦创建不可修改
|
||||||
private var message: String, // 响应消息,可通过setter修改
|
override var message: String, // 响应消息,可通过setter修改
|
||||||
private var data: T?, // 响应数据,可通过setter修改
|
override var data: T?, // 响应数据,可通过setter修改
|
||||||
private var statusCode: Int, // HTTP状态码,可通过setter修改
|
override var statusCode: Int, // HTTP状态码,可通过setter修改
|
||||||
|
) : BaseResponse<T?>(
|
||||||
|
time,
|
||||||
|
message,
|
||||||
|
data,
|
||||||
|
statusCode,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -70,10 +75,7 @@ data class Response<T>(
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun ok(message: String) = Response(
|
fun ok(message: String) = Response(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = message, data = null, statusCode = 200
|
||||||
message = message,
|
|
||||||
data = null,
|
|
||||||
statusCode = 200
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,10 +97,7 @@ data class Response<T>(
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> okData(data: T) = Response(
|
fun <T> okData(data: T) = Response(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = "操作成功", data = data, statusCode = 200
|
||||||
message = "操作成功",
|
|
||||||
data = data,
|
|
||||||
statusCode = 200
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -111,10 +110,7 @@ data class Response<T>(
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> okData(data: T, message: String) = Response(
|
fun <T> okData(data: T, message: String) = Response(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = message, data = data, statusCode = 200
|
||||||
message = message,
|
|
||||||
data = data,
|
|
||||||
statusCode = 200
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,10 +122,7 @@ data class Response<T>(
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun code(statusCode: Int) = Response(
|
fun code(statusCode: Int) = Response(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = "操作成功", data = null, statusCode = statusCode
|
||||||
message = "操作成功",
|
|
||||||
data = null,
|
|
||||||
statusCode = statusCode
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -142,10 +135,7 @@ data class Response<T>(
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun code(statusCode: Int, message: String) = Response(
|
fun code(statusCode: Int, message: String) = Response(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = message, data = null, statusCode = statusCode
|
||||||
message = message,
|
|
||||||
data = null,
|
|
||||||
statusCode = statusCode
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,75 +144,9 @@ data class Response<T>(
|
|||||||
* 使用当前时间、默认消息"操作成功"、状态码200、无数据
|
* 使用当前时间、默认消息"操作成功"、状态码200、无数据
|
||||||
*/
|
*/
|
||||||
constructor() : this(
|
constructor() : this(
|
||||||
time = DateTime.now().format(),
|
time = DateTime.now().format(), message = "操作成功", statusCode = 200, data = null
|
||||||
message = "操作成功",
|
|
||||||
statusCode = 200,
|
|
||||||
data = null
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取格式化后的时间字符串。
|
|
||||||
*
|
|
||||||
* @return 格式化后的时间字符串
|
|
||||||
*/
|
|
||||||
fun getTime(): String = time
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取响应消息
|
|
||||||
*
|
|
||||||
* @return String 当前响应消息
|
|
||||||
*/
|
|
||||||
fun getMessage(): String = message
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置响应消息
|
|
||||||
*
|
|
||||||
* 该方法允许修改响应消息,并返回当前对象以支持链式调用。
|
|
||||||
*
|
|
||||||
* @param message 新的响应消息
|
|
||||||
* @return Response<T> 当前响应对象(支持链式调用)
|
|
||||||
*/
|
|
||||||
fun setMessage(message: String): Response<T> {
|
|
||||||
this.message = message
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取响应数据
|
|
||||||
*
|
|
||||||
* 返回的响应数据可能为null,调用前应进行空值检查。
|
|
||||||
*
|
|
||||||
* @return T? 响应数据,可能为空
|
|
||||||
*/
|
|
||||||
fun getData(): T? = data
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取非空数据对象
|
|
||||||
*
|
|
||||||
* 该方法强制解包data属性,如果data为null则会抛出NullPointerException异常。
|
|
||||||
* 适用于确定data不为null的场景,避免重复的空值检查。
|
|
||||||
*
|
|
||||||
* @return T 非空的数据对象
|
|
||||||
* @throws NullPointerException 当data属性为null时抛出此异常
|
|
||||||
*/
|
|
||||||
@Throws(NullPointerException::class)
|
|
||||||
fun notNullData(): T {
|
|
||||||
return data ?: throw NullPointerException("at Response.notNullData() because data is null")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置响应数据
|
|
||||||
*
|
|
||||||
* 该方法允许修改响应数据,并返回当前对象以支持链式调用。
|
|
||||||
*
|
|
||||||
* @param data 新的响应数据
|
|
||||||
* @return Response<T> 当前响应对象(支持链式调用)
|
|
||||||
*/
|
|
||||||
fun setData(data: T): Response<T> {
|
|
||||||
this.data = data
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的Response对象,使用指定的时间
|
* 创建一个新的Response对象,使用指定的时间
|
||||||
*
|
*
|
||||||
@@ -232,8 +156,7 @@ data class Response<T>(
|
|||||||
* @param time 新的响应时间
|
* @param time 新的响应时间
|
||||||
* @return Response<T> 新的响应对象(如果时间相同则返回原对象)
|
* @return Response<T> 新的响应对象(如果时间相同则返回原对象)
|
||||||
*/
|
*/
|
||||||
fun withTime(time: String): Response<T> =
|
fun withTime(time: String): Response<T> = if (this.time == time) this else Response(time, message, data, statusCode)
|
||||||
if (this.time == time) this else Response(time, message, data, statusCode)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的Response对象,使用指定的数据
|
* 创建一个新的Response对象,使用指定的数据
|
||||||
@@ -245,8 +168,7 @@ data class Response<T>(
|
|||||||
* @param data 新的响应数据
|
* @param data 新的响应数据
|
||||||
* @return Response<D> 新的响应对象,数据类型为D
|
* @return Response<D> 新的响应对象,数据类型为D
|
||||||
*/
|
*/
|
||||||
fun <D> withData(data: D): Response<D> =
|
fun <D> withData(data: D): Response<D> = Response(this.time, this.message, data, this.statusCode)
|
||||||
Response(this.time, this.message, data, this.statusCode)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的Response对象,使用指定的消息
|
* 创建一个新的Response对象,使用指定的消息
|
||||||
@@ -272,25 +194,6 @@ data class Response<T>(
|
|||||||
fun withStatusCode(statusCode: Int): Response<T> =
|
fun withStatusCode(statusCode: Int): Response<T> =
|
||||||
if (this.statusCode == statusCode) this else Response(time, message, data, statusCode)
|
if (this.statusCode == statusCode) this else Response(time, message, data, statusCode)
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取状态码
|
|
||||||
*
|
|
||||||
* @return Int 当前HTTP状态码
|
|
||||||
*/
|
|
||||||
fun getStatusCode(): Int = statusCode
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置状态码
|
|
||||||
*
|
|
||||||
* 该方法允许修改状态码,并返回当前对象以支持链式调用。
|
|
||||||
*
|
|
||||||
* @param statusCode 新的HTTP状态码
|
|
||||||
* @return Response<T> 当前响应对象(支持链式调用)
|
|
||||||
*/
|
|
||||||
fun setStatusCode(statusCode: Int): Response<T> {
|
|
||||||
this.statusCode = statusCode
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回响应对象的字符串表示
|
* 返回响应对象的字符串表示
|
||||||
@@ -301,5 +204,5 @@ data class Response<T>(
|
|||||||
* @return 格式化的字符串表示
|
* @return 格式化的字符串表示
|
||||||
*/
|
*/
|
||||||
override fun toString(): String =
|
override fun toString(): String =
|
||||||
"Response<${data?.javaClass?.simpleName ?: "Nothing"}>(time=${getTime()}, message=${getMessage()}, data=${getData()}, statusCode=${getStatusCode()})"
|
"Response<${data?.javaClass?.simpleName ?: "Nothing"}>(time=${time}, message=${message}, data=${data}, statusCode=${statusCode})"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile OkHttpUtils.kt
|
* CurrentFile OkHttpUtils.kt
|
||||||
* LastUpdate 2026-02-27 22:16:15
|
* LastUpdate 2026-11-09 19:18:03
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
@file:JvmName("OkHttpUtils")
|
@file:JvmName("OkHttpUtils")
|
||||||
@@ -26,25 +26,280 @@ package com.mingliqiye.utils.http.okhttp
|
|||||||
import com.mingliqiye.utils.http.MediaType
|
import com.mingliqiye.utils.http.MediaType
|
||||||
import com.mingliqiye.utils.http.okhttp
|
import com.mingliqiye.utils.http.okhttp
|
||||||
import com.mingliqiye.utils.json.api.JSONA
|
import com.mingliqiye.utils.json.api.JSONA
|
||||||
import com.mingliqiye.utils.json.api.JSONA.toJson
|
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||||
import okhttp3.OkHttpClient
|
import com.mingliqiye.utils.json.api.base.JsonApi.Companion.parse
|
||||||
import okhttp3.Request
|
import okhttp3.*
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
|
||||||
|
|
||||||
|
/**
|
||||||
inline fun <reified T> Response.json() = body.use {
|
* 将 OkHttp 的 [Response] 响应体解析为指定泛型类型的对象。
|
||||||
JSONA.parse<T>(it.bytes())
|
*
|
||||||
|
* 会读取并关闭响应体。未显式传入 [jsonApi] 时使用 [JSONA.jsonApi]。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
* @throws NullPointerException 未传入 [jsonApi] 且 [JSONA] 尚未注入实例时抛出
|
||||||
|
*/
|
||||||
|
inline fun <reified T> Response.json(jsonApi: JsonApi? = null): T = body.use {
|
||||||
|
(jsonApi ?: JSONA.jsonApi).parse<T>(it.bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Response.text() = body.use {
|
/**
|
||||||
String(it.bytes())
|
* 读取 [Response] 响应体为字符串,并关闭响应体。
|
||||||
|
*
|
||||||
|
* 按响应 `Content-Type` 的 `charset` 解码,缺省 UTF-8;读取后响应体被关闭。
|
||||||
|
*
|
||||||
|
* @return 响应体内容字符串
|
||||||
|
*/
|
||||||
|
fun Response.text() = this.body.string()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个 [Request],通过 [calld] 配置 [Request.Builder]。
|
||||||
|
*
|
||||||
|
* @param calld 在 [Request.Builder] 上执行的配置块
|
||||||
|
* @return 构建完成的 [Request]
|
||||||
|
*/
|
||||||
|
fun newRequest(calld: Request.Builder.() -> Unit): Request =
|
||||||
|
Request.Builder().apply(calld).build()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用当前 [OkHttpClient] 创建一个 [Call],请求通过 [calld] 配置。
|
||||||
|
*
|
||||||
|
* 调用返回的 [Call] 的 `execute()` 或 `enqueue()` 才会真正发起请求。
|
||||||
|
*
|
||||||
|
* @param calld 在 [Request.Builder] 上执行的配置块
|
||||||
|
* @return 未执行的 [Call]
|
||||||
|
*/
|
||||||
|
fun OkHttpClient.newCall(calld: Request.Builder.() -> Unit): Call {
|
||||||
|
return this.newCall(newRequest(calld))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun OkHttpClient.request() = Request.Builder()
|
/**
|
||||||
fun Request.Builder.call(okHttpClient: OkHttpClient) = okHttpClient.newCall(this.build())
|
* 使用当前 [OkHttpClient] 同步执行一次请求,请求通过 [calld] 配置。
|
||||||
|
*
|
||||||
fun Request.Builder.postWithJson(data: Any): Request.Builder {
|
* 注意:在 Android 主线程调用会抛出 `NetworkOnMainThreadException`。
|
||||||
data.toJson().toRequestBody(MediaType.APPLICATION_JSON.okhttp())
|
*
|
||||||
return this
|
* @param calld 在 [Request.Builder] 上执行的配置块
|
||||||
|
* @return 同步执行得到的 [Response]
|
||||||
|
* @throws java.io.IOException 网络或 I/O 失败时抛出
|
||||||
|
*/
|
||||||
|
fun OkHttpClient.newExecute(calld: Request.Builder.() -> Unit): Response {
|
||||||
|
return this.newCall(newRequest(calld)).execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 URL,并通过 [calld] 配置查询参数。
|
||||||
|
*
|
||||||
|
* 示例:
|
||||||
|
* ```
|
||||||
|
* Request.Builder().url("https://example.com/api") {
|
||||||
|
* addQueryParameter("page", "1")
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @param url 基础 URL 字符串
|
||||||
|
* @param calld 可选的 [HttpUrl.Builder] 配置块(如添加查询参数);为 `null` 时直接使用 [url]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.url(
|
||||||
|
url: String,
|
||||||
|
calld: (HttpUrl.Builder.() -> Unit)? = null,
|
||||||
|
): Request.Builder {
|
||||||
|
return if (calld != null) {
|
||||||
|
this.url(url.toHttpUrl().newBuilder().apply { calld() }.build())
|
||||||
|
} else {
|
||||||
|
this.url(url.toHttpUrl())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置请求方法和字节数组请求体。
|
||||||
|
*
|
||||||
|
* @param method 请求方法
|
||||||
|
* @param body 请求体字节数组
|
||||||
|
* @param mediaType 可选的媒体类型;为 `null` 时不设置 Content-Type
|
||||||
|
* @param offset 起始偏移量,默认 0
|
||||||
|
* @param length 读取长度,默认从 [offset] 到 [body] 末尾
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.methodBody(
|
||||||
|
method: Method,
|
||||||
|
body: ByteArray,
|
||||||
|
mediaType: MediaType? = null,
|
||||||
|
offset: Int = 0,
|
||||||
|
length: Int = body.size - offset,
|
||||||
|
): Request.Builder {
|
||||||
|
require(offset >= 0 && length >= 0 && offset + length <= body.size) {
|
||||||
|
"offset/length 越界: offset=$offset, length=$length, size=${body.size}"
|
||||||
|
}
|
||||||
|
return method(method.name, body.toRequestBody(mediaType?.okhttp(), offset, length))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置请求方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* @param method 请求方法
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.methodJsonBody(
|
||||||
|
method: Method,
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
): Request.Builder = methodBody(
|
||||||
|
method,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 GET 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* 注意:GET 请求携带请求体在 HTTP 语义中不常见,部分服务端可能忽略或拒绝。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.getJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.GET,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 POST 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.postJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.POST,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 PUT 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.putJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.PUT,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 DELETE 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.deleteJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.DELETE,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 HEAD 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* 注意:HEAD 请求通常不应携带请求体,部分服务端可能忽略或拒绝。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.headJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.HEAD,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 OPTIONS 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.optionsJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.OPTIONS,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 [Request.Builder] 上设置 QUERY 方法,并将对象序列化为 JSON 作为请求体。
|
||||||
|
*
|
||||||
|
* QUERY 是 HTTP 扩展方法,需服务端支持。
|
||||||
|
*
|
||||||
|
* @param body 待序列化的对象
|
||||||
|
* @param jsonApi 可选的 [JsonApi] 实例;为 `null` 时使用 [JSONA.jsonApi]
|
||||||
|
* @return 当前 [Request.Builder],便于链式调用
|
||||||
|
*/
|
||||||
|
fun Request.Builder.queryJsonBody(
|
||||||
|
body: Any,
|
||||||
|
jsonApi: JsonApi? = null,
|
||||||
|
) = methodBody(
|
||||||
|
Method.QUERY,
|
||||||
|
(jsonApi ?: JSONA.jsonApi).formatBytes(body),
|
||||||
|
MediaType.APPLICATION_JSON,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP 请求方法枚举,供 [Request.Builder.methodBody] 等扩展使用。
|
||||||
|
*/
|
||||||
|
enum class Method {
|
||||||
|
/** GET 方法。 */
|
||||||
|
GET,
|
||||||
|
|
||||||
|
/** POST 方法。 */
|
||||||
|
POST,
|
||||||
|
|
||||||
|
/** PUT 方法。 */
|
||||||
|
PUT,
|
||||||
|
|
||||||
|
/** DELETE 方法。 */
|
||||||
|
DELETE,
|
||||||
|
|
||||||
|
/** HEAD 方法。 */
|
||||||
|
HEAD,
|
||||||
|
|
||||||
|
/** OPTIONS 方法。 */
|
||||||
|
OPTIONS,
|
||||||
|
|
||||||
|
/** QUERY 方法(HTTP 扩展方法,需服务端支持)。 */
|
||||||
|
QUERY,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun Request.Builder.post() = post(RequestBody.EMPTY)
|
||||||
|
fun Request.Builder.put() = put(RequestBody.EMPTY)
|
||||||
|
fun Request.Builder.patch() = patch(RequestBody.EMPTY)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile SIO.kt
|
* CurrentFile SIO.kt
|
||||||
* LastUpdate 2026-06-21 10:40:58
|
* LastUpdate 2026-10-22 20:21:49
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -29,68 +29,6 @@ import org.slf4j.Logger
|
|||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
|
|
||||||
/**
|
|
||||||
* 已弃用的IO对象,所有方法都委托给SIO对象
|
|
||||||
* @deprecated 请使用SIO对象替代
|
|
||||||
*/
|
|
||||||
@Deprecated("This IO is deprecated. reName SIO", replaceWith = ReplaceWith("SIO"), level = DeprecationLevel.WARNING)
|
|
||||||
object IO {
|
|
||||||
/**
|
|
||||||
* 打印多个参数,使用空格分隔
|
|
||||||
* @param args 要打印的参数数组
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun print(vararg args: Any?) = SIO.print(*args)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印多个参数并换行,使用空格分隔
|
|
||||||
* @param args 要打印的参数数组
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun println(vararg args: Any?) = SIO.println(*args)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印多个参数并换行,指定分隔符
|
|
||||||
* @param sp 分隔符
|
|
||||||
* @param args 要打印的参数数组
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun printlnA(sp: String, vararg args: Any?) = SIO.printlnA(sp, *args)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 打印多个参数,指定分隔符
|
|
||||||
* @param sp 分隔符,默认为空字符串
|
|
||||||
* @param args 要打印的参数数组
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun printA(sp: String = "", vararg args: Any?) = SIO.printA(sp, *args)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重定向 System.out 到 INFO 级别日志
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun redirectOutToInfo() = SIO.redirectOutToInfo()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重定向 System.err 到 ERROR 级别
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun redirectErrToError() = SIO.redirectErrToError()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 完全重定向(包括第三方库的输出)
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun redirectAll() = SIO.redirectAll()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 恢复原始输出流
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun restore() = SIO.restore()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IO工具类,提供打印功能和系统输出流重定向到日志的功能
|
* IO工具类,提供打印功能和系统输出流重定向到日志的功能
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile FieldStructure.kt
|
* CurrentFile FieldStructure.kt
|
||||||
* LastUpdate 2026-07-02 10:22:42
|
* LastUpdate 2026-11-04 11:53:07
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ import java.lang.reflect.Modifier
|
|||||||
* 该类继承自JNA的Structure类,通过反射自动获取子类的公共非静态字段,
|
* 该类继承自JNA的Structure类,通过反射自动获取子类的公共非静态字段,
|
||||||
* 并按声明顺序返回字段名列表,用于JNA结构体的字段映射。
|
* 并按声明顺序返回字段名列表,用于JNA结构体的字段映射。
|
||||||
*/
|
*/
|
||||||
open class FieldStructure : Structure() {
|
abstract class FieldStructure : Structure() {
|
||||||
/**
|
/**
|
||||||
* 获取结构体字段顺序列表
|
* 获取结构体字段顺序列表
|
||||||
* 通过反射遍历当前类及其父类的所有声明字段,过滤出公共非静态字段,
|
* 通过反射遍历当前类及其父类的所有声明字段,过滤出公共非静态字段,
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile JSONA.kt
|
* CurrentFile JSONA.kt
|
||||||
* LastUpdate 2026-03-18 09:14:06
|
* LastUpdate 2026-11-07 13:29:54
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -24,6 +24,8 @@ package com.mingliqiye.utils.json.api
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.fasterxml.jackson.module.kotlin.kotlinModule
|
import com.fasterxml.jackson.module.kotlin.kotlinModule
|
||||||
|
import com.mingliqiye.utils.json.api.JSONA.jsonApi
|
||||||
|
import com.mingliqiye.utils.json.api.JSONA.setJsonApi
|
||||||
import com.mingliqiye.utils.json.api.base.JsonApi
|
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||||
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||||
import com.mingliqiye.utils.json.api.type.listType
|
import com.mingliqiye.utils.json.api.type.listType
|
||||||
@@ -38,523 +40,582 @@ import java.nio.file.Path
|
|||||||
import kotlin.io.path.readBytes
|
import kotlin.io.path.readBytes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON工具类,提供静态方法访问JSON API功能
|
* JSON 工具门面,提供静态方法访问 JSON API 功能。
|
||||||
|
*
|
||||||
|
* [JSONA] 本身不实现 JSON 逻辑,而是持有一个可替换的 [JsonApi] 实例,
|
||||||
|
* 所有方法都转发到当前实例。使用前必须先通过 [setJsonApi] 注入实现,
|
||||||
|
* 未注入时访问 [jsonApi] 或调用任何转发方法都会抛出 [NullPointerException]。
|
||||||
|
*
|
||||||
|
* @see JsonApi
|
||||||
|
* @see setJsonApi
|
||||||
*/
|
*/
|
||||||
object JSONA {
|
object JSONA {
|
||||||
@JvmStatic
|
|
||||||
private var jsonApi: JsonApi? = null
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取JSON API实例
|
* 当前 JSON API 实例,未注入时为 `null`。
|
||||||
*
|
*
|
||||||
* @return JSON API实例
|
* 使用 `@Volatile` 保证多线程下的可见性;仅在启动时注入一次的场景下无副作用。
|
||||||
* @throws NullPointerException 当JSON API未初始化时抛出异常
|
|
||||||
*/
|
*/
|
||||||
@Throws(NullPointerException::class)
|
@Volatile
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getJsonApi(): JsonApi {
|
private var privateJsonApi: JsonApi? = null
|
||||||
if (jsonApi == null) {
|
|
||||||
throw NullPointerException("jsonApi is null plase setJsonApi first")
|
|
||||||
}
|
|
||||||
return jsonApi!!
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置JSON API实例
|
* 获取当前 JSON API 实例。
|
||||||
|
*
|
||||||
|
* @return 已注入的 [JsonApi] 实例
|
||||||
|
* @throws NullPointerException 尚未调用 [setJsonApi] 时抛出
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
val jsonApi: JsonApi
|
||||||
|
get() = privateJsonApi ?: throw NullPointerException("jsonApi is null plase setJsonApi first")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置或替换 JSON API 实例。
|
||||||
|
*
|
||||||
|
* 设置后,[JSONA] 上所有转发方法立即使用新实例。
|
||||||
*
|
*
|
||||||
* @param jsa JSON API 实例
|
* @param jsa JSON API 实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun setJsonApi(jsa: JsonApi) {
|
fun setJsonApi(jsa: JsonApi) {
|
||||||
jsonApi = jsa
|
privateJsonApi = jsa
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用反射创建并设置指定类型的JSON API实例
|
* 通过反射创建 [T] 的无参实例并设置为当前 JSON API。
|
||||||
*
|
*
|
||||||
* @param T 继承自JsonApi的具体实现类
|
* @param T 继承自 [JsonApi] 的具体实现类,必须提供公开无参构造
|
||||||
|
* @throws NoSuchMethodException 实现类没有无参构造时抛出
|
||||||
|
* @throws ReflectiveOperationException 反射实例化失败时抛出
|
||||||
*/
|
*/
|
||||||
inline fun <reified T : JsonApi> setJsonApi() {
|
inline fun <reified T : JsonApi> setJsonApi() {
|
||||||
setJsonApi(T::class.java.getConstructor().newInstance() as JsonApi)
|
setJsonApi(T::class.java.getConstructor().newInstance() as JsonApi)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定类型的对象
|
* 将 JSON 字符串解析为指定类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param clazz 目标对象的Class类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(json: String, clazz: Class<T>): T = getJsonApi().parse(json, clazz)
|
fun <T> parse(json: String, clazz: Class<T>): T = jsonApi.parse(json, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定泛型类型对象
|
* 将 JSON 字符串解析为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
inline fun <reified T> parse(json: String): T {
|
inline fun <reified T> parse(json: String): T {
|
||||||
return getJsonApi().parse(json, (object : JsonTypeReference<T>() {}))
|
return jsonApi.parse(json, (object : JsonTypeReference<T>() {}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字节数组形式的JSON解析为指定泛型类型对象
|
* 将字节数组形式的 JSON 解析为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
* @param json 待解析的 JSON 字节数组
|
* @param json 待解析的 JSON 字节数组
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
inline fun <reified T> parse(json: ByteArray): T = getJsonApi().parse(json, object : JsonTypeReference<T>() {})
|
inline fun <reified T> parse(json: ByteArray): T = jsonApi.parse(json, object : JsonTypeReference<T>() {})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定泛型类型对象
|
* 将 JSON 字符串解析为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(json: String, type: JsonTypeReference<T>): T = getJsonApi().parse(json, type)
|
fun <T> parse(json: String, type: JsonTypeReference<T>): T = jsonApi.parse(json, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字符串
|
* 将对象格式化为 JSON 字符串。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @return 格式化后的 JSON 字符串
|
* @return 格式化后的 JSON 字符串
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun format(obj: Any): String = getJsonApi().format(obj)
|
fun format(obj: Any): String = jsonApi.format(obj)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从文件路径解析JSON为指定类型的对象
|
* 从文件路径解析 JSON 为指定类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param path 文件路径
|
* @param path 文件路径
|
||||||
* @param clazz 目标对象的Class类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(path: String, clazz: Class<T>): T = getJsonApi().parseFrom(path, clazz)
|
fun <T> parseFrom(path: String, clazz: Class<T>): T = jsonApi.parseFrom(path, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Path对象解析JSON为指定类型的对象
|
* 从 [Path] 对象解析 JSON 为指定类型的对象。
|
||||||
*
|
*
|
||||||
* @param path Path对象
|
* @param T 目标对象类型
|
||||||
* @param clazz 目标对象的Class类型
|
* @param path [Path] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(path: Path, clazz: Class<T>): T = getJsonApi().parseFrom(path, clazz)
|
fun <T> parseFrom(path: Path, clazz: Class<T>): T = jsonApi.parseFrom(path, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从File对象解析JSON为指定类型的对象
|
* 从 [File] 对象解析 JSON 为指定类型的对象。
|
||||||
*
|
*
|
||||||
* @param file File对象
|
* @param T 目标对象类型
|
||||||
* @param clazz 目标对象的Class类型
|
* @param file [File] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(file: File, clazz: Class<T>): T = getJsonApi().parseFrom(file, clazz)
|
fun <T> parseFrom(file: File, clazz: Class<T>): T = jsonApi.parseFrom(file, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从InputStream解析JSON为指定类型的对象
|
* 从 [InputStream] 解析 JSON 为指定类型的对象。
|
||||||
*
|
*
|
||||||
* @param inputStream InputStream对象
|
* @param T 目标对象类型
|
||||||
* @param clazz 目标对象的Class类型
|
* @param inputStream [InputStream] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(inputStream: InputStream, clazz: Class<T>): T = getJsonApi().parseFrom(inputStream, clazz)
|
fun <T> parseFrom(inputStream: InputStream, clazz: Class<T>): T = jsonApi.parseFrom(inputStream, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从文件路径解析JSON为指定泛型类型对象
|
* 从文件路径解析 JSON 为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param path 文件路径
|
* @param path 文件路径
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(path: String, type: JsonTypeReference<T>): T = getJsonApi().parseFrom(path, type)
|
fun <T> parseFrom(path: String, type: JsonTypeReference<T>): T = jsonApi.parseFrom(path, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Path对象解析JSON为指定泛型类型对象
|
* 从 [Path] 对象解析 JSON 为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
* @param path Path对象
|
* @param T 目标对象类型
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param path [Path] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(path: Path, type: JsonTypeReference<T>): T = getJsonApi().parseFrom(path, type)
|
fun <T> parseFrom(path: Path, type: JsonTypeReference<T>): T = jsonApi.parseFrom(path, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从File对象解析JSON为指定泛型类型对象
|
* 从 [File] 对象解析 JSON 为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
* @param file File对象
|
* @param T 目标对象类型
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param file [File] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(file: File, type: JsonTypeReference<T>): T = getJsonApi().parseFrom(file, type)
|
fun <T> parseFrom(file: File, type: JsonTypeReference<T>): T = jsonApi.parseFrom(file, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从InputStream解析JSON为指定泛型类型对象
|
* 从 [InputStream] 解析 JSON 为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
* @param inputStream InputStream对象
|
* @param T 目标对象类型
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param inputStream [InputStream] 对象
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseFrom(inputStream: InputStream, type: JsonTypeReference<T>): T =
|
fun <T> parseFrom(inputStream: InputStream, type: JsonTypeReference<T>): T =
|
||||||
getJsonApi().parseFrom(inputStream, type)
|
jsonApi.parseFrom(inputStream, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字节数组形式的JSON解析为指定类型的对象
|
* 将字节数组形式的 JSON 解析为指定类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字节数组
|
* @param json 待解析的 JSON 字节数组
|
||||||
* @param clazz 目标对象的Class类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(json: ByteArray, clazz: Class<T>): T = getJsonApi().parse(json, clazz)
|
fun <T> parse(json: ByteArray, clazz: Class<T>): T = jsonApi.parse(json, clazz)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字节数组形式的JSON解析为指定泛型类型对象
|
* 将字节数组形式的 JSON 解析为指定泛型类型的对象。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字节数组
|
* @param json 待解析的 JSON 字节数组
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例
|
* @return 解析后的对象实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(json: ByteArray, type: JsonTypeReference<T>): T = getJsonApi().parse(json, type)
|
fun <T> parse(json: ByteArray, type: JsonTypeReference<T>): T = jsonApi.parse(json, type)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定类型的对象,解析失败时返回默认值
|
* 将 JSON 字符串解析为指定类型的对象,解析失败时返回默认值。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param clazz 目标对象的Class类型
|
* @param clazz 目标对象的 [Class] 类型
|
||||||
* @param defaultValue 解析失败时返回的默认值
|
* @param defaultValue 解析失败时返回的默认值
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例或默认值
|
* @return 解析后的对象实例或默认值
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(json: String, clazz: Class<T>, defaultValue: T): T = getJsonApi().parse(json, clazz, defaultValue)
|
fun <T> parse(json: String, clazz: Class<T>, defaultValue: T): T = jsonApi.parse(json, clazz, defaultValue)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定泛型类型对象,解析失败时返回默认值
|
* 将 JSON 字符串解析为指定泛型类型的对象,解析失败时返回默认值。
|
||||||
*
|
*
|
||||||
|
* @param T 目标对象类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param type 目标对象的Type类型(支持泛型)
|
* @param type 目标对象的 [JsonTypeReference] 类型引用(支持泛型)
|
||||||
* @param defaultValue 解析失败时返回的默认值
|
* @param defaultValue 解析失败时返回的默认值
|
||||||
* @param <T> 泛型参数,表示目标对象的类型
|
|
||||||
* @return 解析后的对象实例或默认值
|
* @return 解析后的对象实例或默认值
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parse(
|
fun <T> parse(
|
||||||
json: String, type: JsonTypeReference<T>, defaultValue: T,
|
json: String, type: JsonTypeReference<T>, defaultValue: T,
|
||||||
): T = getJsonApi().parse(json, type, defaultValue)
|
): T = jsonApi.parse(json, type, defaultValue)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字符串(带缩进和换行)
|
* 将对象格式化为美化格式的 JSON 字符串(带缩进和换行)。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @return 格式化后的美化 JSON 字符串
|
* @return 格式化后的美化 JSON 字符串
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPretty(obj: Any): String = getJsonApi().formatPretty(obj)
|
fun formatPretty(obj: Any): String = jsonApi.formatPretty(obj)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字节数组
|
* 将对象格式化为美化格式的 JSON 字节数组。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @return 格式化后的美化 JSON 字节数组
|
* @return 格式化后的美化 JSON 字节数组
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPrettyBytes(obj: Any): ByteArray = getJsonApi().formatPrettyBytes(obj)
|
fun formatPrettyBytes(obj: Any): ByteArray = jsonApi.formatPrettyBytes(obj)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字符串并写入文件
|
* 将对象格式化为美化格式的 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file 文件路径
|
* @param file 文件路径
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPretty(obj: Any, file: String) = getJsonApi().formatPretty(obj, file)
|
fun formatPretty(obj: Any, file: String) = jsonApi.formatPretty(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字符串并写入文件
|
* 将对象格式化为美化格式的 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file Path对象
|
* @param file [Path] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPretty(obj: Any, file: Path) = getJsonApi().formatPretty(obj, file)
|
fun formatPretty(obj: Any, file: Path) = jsonApi.formatPretty(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字符串并写入文件
|
* 将对象格式化为美化格式的 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file File对象
|
* @param file [File] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPretty(obj: Any, file: File) = getJsonApi().formatPretty(obj, file)
|
fun formatPretty(obj: Any, file: File) = jsonApi.formatPretty(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为美化格式的JSON字符串并写入输出流
|
* 将对象格式化为美化格式的 JSON 字符串并写入输出流。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param stream OutputStream对象
|
* @param stream [OutputStream] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatPretty(obj: Any, stream: OutputStream) = getJsonApi().formatPretty(obj, stream)
|
fun formatPretty(obj: Any, stream: OutputStream) = jsonApi.formatPretty(obj, stream)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字节数组
|
* 将对象格式化为 JSON 字节数组。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @return 格式化后的 JSON 字节数组
|
* @return 格式化后的 JSON 字节数组
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun formatBytes(obj: Any): ByteArray = getJsonApi().formatBytes(obj)
|
fun formatBytes(obj: Any): ByteArray = jsonApi.formatBytes(obj)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字符串并写入文件
|
* 将对象格式化为 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file 文件路径
|
* @param file 文件路径
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun format(obj: Any, file: String) = getJsonApi().format(obj, file)
|
fun format(obj: Any, file: String) = jsonApi.format(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字符串并写入文件
|
* 将对象格式化为 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file Path对象
|
* @param file [Path] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun format(obj: Any, file: Path) = getJsonApi().format(obj, file)
|
fun format(obj: Any, file: Path) = jsonApi.format(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字符串并写入文件
|
* 将对象格式化为 JSON 字符串并写入文件。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param file File对象
|
* @param file [File] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun format(obj: Any, file: File) = getJsonApi().format(obj, file)
|
fun format(obj: Any, file: File) = jsonApi.format(obj, file)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象格式化为JSON字符串并写入输出流
|
* 将对象格式化为 JSON 字符串并写入输出流。
|
||||||
*
|
*
|
||||||
* @param obj 待格式化的对象
|
* @param obj 待格式化的对象
|
||||||
* @param stream OutputStream对象
|
* @param stream [OutputStream] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun format(obj: Any, stream: OutputStream) = getJsonApi().format(obj, stream)
|
fun format(obj: Any, stream: OutputStream) = jsonApi.format(obj, stream)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定元素类型的List集合
|
* 将 JSON 字符串解析为指定元素类型的 [List] 集合。
|
||||||
*
|
*
|
||||||
|
* @param T [List] 中元素的类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param elementType List中元素的类型
|
* @param elementType [List] 中元素的 [Class] 类型
|
||||||
* @param <T> 泛型参数,表示List中元素的类型
|
* @return 解析后的 [List] 集合
|
||||||
* @return 解析后的List集合
|
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <T> parseList(json: String, elementType: Class<T>): List<T> = parse(json, listType(elementType))
|
fun <T> parseList(json: String, elementType: Class<T>): List<T> = parse(json, listType(elementType))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定元素类型的List集合
|
* 将 JSON 字符串解析为指定元素类型的 [List] 集合。
|
||||||
*
|
*
|
||||||
|
* @param T [List] 中元素的类型,由调用方通过 reified 推断
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param <T> 泛型参数,表示List中元素的类型
|
* @return 解析后的 [List] 集合
|
||||||
* @return 解析后的List集合
|
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
inline fun <reified T> parseList(json: String): List<T> = parse(json, listType(T::class.java))
|
inline fun <reified T> parseList(json: String): List<T> = parse(json, listType(T::class.java))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定键值类型的Map集合
|
* 将 JSON 字符串解析为指定键值类型的 [MutableMap] 集合。
|
||||||
*
|
*
|
||||||
|
* @param K [Map] 中键的类型
|
||||||
|
* @param V [Map] 中值的类型
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param keyType Map中键的类型
|
* @param keyType [Map] 中键的 [Class] 类型
|
||||||
* @param valueType Map中值的类型
|
* @param valueType [Map] 中值的 [Class] 类型
|
||||||
* @param <K> 泛型参数,表示Map中键的类型
|
* @return 解析后的 [MutableMap] 集合
|
||||||
* @param <V> 泛型参数,表示Map中值的类型
|
|
||||||
* @return 解析后的Map集合
|
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <K, V> parseMap(
|
fun <K, V> parseMap(
|
||||||
json: String, keyType: Class<K>, valueType: Class<V>,
|
json: String, keyType: Class<K>, valueType: Class<V>,
|
||||||
): MutableMap<K, V> = getJsonApi().parseMap(json, keyType, valueType)
|
): MutableMap<K, V> = jsonApi.parseMap(json, keyType, valueType)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON字符串解析为指定键值类型的Map集合
|
* 将 JSON 字符串解析为指定键值类型的 [MutableMap] 集合。
|
||||||
*
|
*
|
||||||
|
* @param K [Map] 中键的类型,由调用方通过 reified 推断
|
||||||
|
* @param V [Map] 中值的类型,由调用方通过 reified 推断
|
||||||
* @param json 待解析的 JSON 字符串
|
* @param json 待解析的 JSON 字符串
|
||||||
* @param <K> 泛型参数,表示Map中键的类型
|
* @return 解析后的 [MutableMap] 集合
|
||||||
* @param <V> 泛型参数,表示Map中值的类型
|
|
||||||
* @return 解析后的Map集合
|
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
inline fun <reified K, reified V> parseMap(
|
inline fun <reified K, reified V> parseMap(
|
||||||
json: String,
|
json: String,
|
||||||
): MutableMap<K, V> = getJsonApi().parseMap(json, K::class.java, V::class.java)
|
): MutableMap<K, V> = jsonApi.parseMap(json, K::class.java, V::class.java)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证字符串是否为有效的JSON格式
|
* 验证字符串是否为有效的 JSON 格式。
|
||||||
*
|
*
|
||||||
* @param json 待验证的字符串
|
* @param json 待验证的字符串
|
||||||
* @return 如果是有效的JSON格式返回true,否则返回false
|
* @return 如果是有效的 JSON 格式返回 `true`,否则返回 `false`
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun isValidJson(json: String): Boolean = getJsonApi().isValidJson(json)
|
fun isValidJson(json: String): Boolean = jsonApi.isValidJson(json)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象转换为JSON字节数组
|
* 将对象转换为 JSON 字节数组。
|
||||||
*
|
*
|
||||||
|
* @param obj 待转换的对象
|
||||||
* @return 转换后的 JSON 字节数组
|
* @return 转换后的 JSON 字节数组
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toBytes(obj: Any): ByteArray = getJsonApi().toBytes(obj)
|
fun toBytes(obj: Any): ByteArray = jsonApi.toBytes(obj)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象转换为美化格式的JSON字节数组
|
* 将对象转换为美化格式的 JSON 字节数组。
|
||||||
*
|
*
|
||||||
|
* @param obj 待转换的对象
|
||||||
* @return 转换后的美化格式 JSON 字节数组
|
* @return 转换后的美化格式 JSON 字节数组
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toBytesPretty(obj: Any): ByteArray = getJsonApi().toBytesPretty(obj)
|
fun toBytesPretty(obj: Any): ByteArray = jsonApi.toBytesPretty(obj)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 合并多个JSON字符串为一个JSON对象
|
* 合并多个 JSON 字符串为一个 JSON 对象。
|
||||||
*
|
*
|
||||||
* @param jsons 待合并的 JSON 字符串数组
|
* @param jsons 待合并的 JSON 字符串数组
|
||||||
* @return 合并后的 JSON 字符串
|
* @return 合并后的 JSON 字符串
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun merge(vararg jsons: String): String = getJsonApi().merge(*jsons)
|
fun merge(vararg jsons: String): String = jsonApi.merge(*jsons)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取JSON字符串中指定路径节点的值
|
* 获取 JSON 字符串中指定路径节点的值。
|
||||||
*
|
*
|
||||||
* @param json JSON 字符串
|
* @param json JSON 字符串
|
||||||
* @param path 节点路径(如:"user.name")
|
* @param path 节点路径(如 `"user.name"`)
|
||||||
* @return 节点值的字符串表示
|
* @return 节点值的字符串表示;节点不存在时返回 `null`
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getNodeValue(json: String, path: String): String? = getJsonApi().getNodeValue(json, path)
|
fun getNodeValue(json: String, path: String): String? = jsonApi.getNodeValue(json, path)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新JSON字符串中指定路径节点的值
|
* 更新 JSON 字符串中指定路径节点的值。
|
||||||
*
|
*
|
||||||
* @param json 原始 JSON 字符串
|
* @param json 原始 JSON 字符串
|
||||||
* @param path 节点路径(如:"user.name")
|
* @param path 节点路径(如 `"user.name"`)
|
||||||
* @param newValue 新的节点值
|
* @param newValue 新的节点值
|
||||||
* @return 更新后的 JSON 字符串
|
* @return 更新后的 JSON 字符串
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun updateNodeValue(json: String, path: String, newValue: Any): String =
|
fun updateNodeValue(json: String, path: String, newValue: Any): String =
|
||||||
getJsonApi().updateNodeValue(json, path, newValue)
|
jsonApi.updateNodeValue(json, path, newValue)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将源对象转换为目标类型的对象
|
* 将源对象转换为目标类型的对象。
|
||||||
*
|
*
|
||||||
* @param D 目标对象的类型
|
* @param D 目标对象类型
|
||||||
* @param source 需要转换的源对象
|
* @param source 需要转换的源对象
|
||||||
* @param destinationClass 目标对象的Class类型
|
* @param destinationClass 目标对象的 [Class] 类型
|
||||||
* @return 转换后的目标类型对象
|
* @return 转换后的目标类型对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <D> convert(source: Any, destinationClass: Class<D>): D = getJsonApi().convert(source, destinationClass)
|
fun <D> convert(source: Any, destinationClass: Class<D>): D = jsonApi.convert(source, destinationClass)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用内联函数和reified类型参数将源对象转换为目标类型的对象
|
* 将源对象转换为目标类型的对象,目标类型通过 reified 推断。
|
||||||
*
|
*
|
||||||
* @param D 目标对象的类型(通过reified类型参数推断)
|
* @param D 目标对象类型,由调用方通过 reified 推断
|
||||||
* @param source 需要转换的源对象
|
* @param source 需要转换的源对象
|
||||||
* @return 转换后的目标类型对象
|
* @return 转换后的目标类型对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
inline fun <reified D> convert(source: Any): D = getJsonApi().convert(source, object : JsonTypeReference<D>() {})
|
inline fun <reified D> convert(source: Any): D = jsonApi.convert(source, object : JsonTypeReference<D>() {})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将源对象转换为目标类型的对象,使用JsonTypeReference指定目标类型
|
* 将源对象转换为目标类型的对象,使用 [JsonTypeReference] 指定目标类型。
|
||||||
*
|
*
|
||||||
* @param D 目标对象的类型
|
* @param D 目标对象类型
|
||||||
* @param source 需要转换的源对象
|
* @param source 需要转换的源对象
|
||||||
* @param destinationType 目标对象的JsonTypeReference类型引用
|
* @param destinationType 目标对象的 [JsonTypeReference] 类型引用
|
||||||
* @return 转换后的目标类型对象
|
* @return 转换后的目标类型对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D =
|
fun <D> convert(source: Any, destinationType: JsonTypeReference<D>): D =
|
||||||
getJsonApi().convert(source, destinationType)
|
jsonApi.convert(source, destinationType)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加JSON转换器到API中
|
* 添加 JSON 转换器到 API 中。
|
||||||
*
|
*
|
||||||
* @param c 需要添加的JSON转换器实例
|
* @param c 需要添加的 [BaseJsonConverter] 实例
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun addJsonConverter(c: BaseJsonConverter<*, *>) = getJsonApi().addJsonConverter(c)
|
fun addJsonConverter(c: BaseJsonConverter<*, *>) = jsonApi.addJsonConverter(c)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用反射创建并添加指定类型的JSON转换器到API中
|
* 使用反射创建并添加指定类型的 JSON 转换器到 API 中。
|
||||||
*
|
*
|
||||||
* @param T 继承自BaseJsonConverter的具体实现类
|
* @param T 继承自 [BaseJsonConverter] 的具体实现类
|
||||||
*/
|
*/
|
||||||
inline fun <reified T : BaseJsonConverter<*, *>> addJsonConverter() = addJsonConverter(getJsonConverter<T>())
|
inline fun <reified T : BaseJsonConverter<*, *>> addJsonConverter() = addJsonConverter(getJsonConverter<T>())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前字符串解析为指定泛型类型的对象。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
*/
|
||||||
inline fun <reified T> String.parseJson() = parse<T>(this)
|
inline fun <reified T> String.parseJson() = parse<T>(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前字节数组解析为指定泛型类型的对象。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
*/
|
||||||
inline fun <reified T> ByteArray.parseJson() = parse<T>(this)
|
inline fun <reified T> ByteArray.parseJson() = parse<T>(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前 [InputStream] 的内容解析为指定泛型类型的对象,并自动关闭流。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
*/
|
||||||
inline fun <reified T> InputStream.parseJson() = this.use { parse<T>(readBytes()) }
|
inline fun <reified T> InputStream.parseJson() = this.use { parse<T>(readBytes()) }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前 [File] 的内容解析为指定泛型类型的对象。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
*/
|
||||||
inline fun <reified T> File.parseJson() = this.inputStream().parseJson<T>()
|
inline fun <reified T> File.parseJson() = this.inputStream().parseJson<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前 [Path] 的内容解析为指定泛型类型的对象。
|
||||||
|
*
|
||||||
|
* @param T 目标对象类型,由调用方通过 reified 推断
|
||||||
|
* @return 解析后的对象实例
|
||||||
|
*/
|
||||||
inline fun <reified T> Path.parseJson() = this.toFile().inputStream().parseJson<T>()
|
inline fun <reified T> Path.parseJson() = this.toFile().inputStream().parseJson<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将当前对象格式化为 JSON 字符串。
|
||||||
|
*
|
||||||
|
* @return 格式化后的 JSON 字符串
|
||||||
|
*/
|
||||||
fun Any.toJson() = format(this)
|
fun Any.toJson() = format(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建并返回一个注册了 Kotlin 模块的 Jackson [ObjectMapper]。
|
||||||
|
*
|
||||||
|
* @return 注册了 Kotlin 模块的 [ObjectMapper]
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun jacksonKotlinObjectMapper(): ObjectMapper = ObjectMapper().jacksonKotlinObjectMapper()
|
fun jacksonKotlinObjectMapper(): ObjectMapper = ObjectMapper().jacksonKotlinObjectMapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为当前 [ObjectMapper] 注册 Kotlin 模块并返回自身。
|
||||||
|
*
|
||||||
|
* @return 注册了 Kotlin 模块的 [ObjectMapper](即当前实例)
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun ObjectMapper.jacksonKotlinObjectMapper(): ObjectMapper = this.registerModule(kotlinModule())
|
fun ObjectMapper.jacksonKotlinObjectMapper(): ObjectMapper = this.registerModule(kotlinModule())
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 JSON 字符串解析为 JsonItem 对象
|
* 将 JSON 字符串解析为 [JsonItem] 对象。
|
||||||
*
|
*
|
||||||
* @param json JSON 格式的字符串
|
* @param json JSON 格式的字符串
|
||||||
* @return 解析后的 JsonItem 对象,可以是 JsonObject、JsonArray 或 JsonValue
|
* @return 解析后的 [JsonItem] 对象,可能是 `JsonObject`、`JsonArray` 或 `JsonValue`
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun parseJsonItem(json: String): JsonItem = getJsonApi().parseJsonItem(json)
|
fun parseJsonItem(json: String): JsonItem = jsonApi.parseJsonItem(json)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字节数组解析为 JsonItem 对象
|
* 将字节数组解析为 [JsonItem] 对象。
|
||||||
* 字节数组会被转换为字符串后再解析
|
*
|
||||||
|
* 字节数组会被转换为字符串后再解析。
|
||||||
*
|
*
|
||||||
* @param byte JSON 格式的字节数组(通常是 UTF-8 编码)
|
* @param byte JSON 格式的字节数组(通常是 UTF-8 编码)
|
||||||
* @return 解析后的 JsonItem 对象
|
* @return 解析后的 [JsonItem] 对象
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun parseJsonItem(byte: ByteArray): JsonItem {
|
fun parseJsonItem(byte: ByteArray): JsonItem {
|
||||||
@@ -562,17 +623,16 @@ object JSONA {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将输入流解析为 JsonItem 对象
|
* 将输入流解析为 [JsonItem] 对象,并自动关闭输入流。
|
||||||
* 会自动关闭输入流
|
|
||||||
*
|
*
|
||||||
* @param inputStream JSON 数据的输入流
|
* @param inputStream JSON 数据的输入流
|
||||||
* @return 解析后的 JsonItem 对象
|
* @return 解析后的 [JsonItem] 对象
|
||||||
* @throws IOException 当读取输入流失败时抛出
|
* @throws IOException 当读取输入流失败时抛出
|
||||||
*
|
*
|
||||||
* 使用示例:
|
* 使用示例:
|
||||||
* ```
|
* ```
|
||||||
* val inputStream = FileInputStream("data.json")
|
* val inputStream = FileInputStream("data.json")
|
||||||
* val jsonItem = converter.parseJsonItem(inputStream)
|
* val jsonItem = JSONA.parseJsonItem(inputStream)
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@@ -584,16 +644,16 @@ object JSONA {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将文件解析为 JsonItem 对象
|
* 将文件解析为 [JsonItem] 对象。
|
||||||
*
|
*
|
||||||
* @param file 包含 JSON 数据的文件
|
* @param file 包含 JSON 数据的文件
|
||||||
* @return 解析后的 JsonItem 对象
|
* @return 解析后的 [JsonItem] 对象
|
||||||
* @throws IOException 当读取文件失败时抛出
|
* @throws IOException 当读取文件失败时抛出
|
||||||
*
|
*
|
||||||
* 使用示例:
|
* 使用示例:
|
||||||
* ```
|
* ```
|
||||||
* val file = File("config.json")
|
* val file = File("config.json")
|
||||||
* val jsonItem = converter.parseJsonItem(file)
|
* val jsonItem = JSONA.parseJsonItem(file)
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@@ -603,16 +663,16 @@ object JSONA {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将文件路径解析为 JsonItem 对象
|
* 将文件路径解析为 [JsonItem] 对象。
|
||||||
*
|
*
|
||||||
* @param path 包含 JSON 数据的文件路径
|
* @param path 包含 JSON 数据的文件路径
|
||||||
* @return 解析后的 JsonItem 对象
|
* @return 解析后的 [JsonItem] 对象
|
||||||
* @throws IOException 当读取文件失败时抛出
|
* @throws IOException 当读取文件失败时抛出
|
||||||
*
|
*
|
||||||
* 使用示例:
|
* 使用示例:
|
||||||
* ```
|
* ```
|
||||||
* val path = Paths.get("data/user.json")
|
* val path = Paths.get("data/user.json")
|
||||||
* val jsonItem = converter.parseJsonItem(path)
|
* val jsonItem = JSONA.parseJsonItem(path)
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Jackson3JsonApi.kt
|
* CurrentFile Jackson3JsonApi.kt
|
||||||
* LastUpdate 2026-06-21 10:40:58
|
* LastUpdate 2026-10-12 13:59:59
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -44,9 +44,9 @@ class Jackson3JsonApi : JsonApi {
|
|||||||
private var _prObjectMapper: ObjectMapper? = null
|
private var _prObjectMapper: ObjectMapper? = null
|
||||||
|
|
||||||
|
|
||||||
private var objectMapper: ObjectMapper
|
var objectMapper: ObjectMapper
|
||||||
get() = get()
|
get() = get()
|
||||||
set(value) = set(value)
|
private set(value) = set(value)
|
||||||
|
|
||||||
constructor(objectMapper: ObjectMapper) {
|
constructor(objectMapper: ObjectMapper) {
|
||||||
_prObjectMapper = objectMapper
|
_prObjectMapper = objectMapper
|
||||||
@@ -178,8 +178,7 @@ class Jackson3JsonApi : JsonApi {
|
|||||||
return objectMapper.convertValue(source, destinationClass)
|
return objectMapper.convertValue(source, destinationClass)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw JsonException(
|
throw JsonException(
|
||||||
"Failed to convert from ${source.javaClass.simpleName} to ${destinationClass.simpleName}",
|
"Failed to convert from ${source.javaClass.simpleName} to ${destinationClass.simpleName}", e
|
||||||
e
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile JacksonJsonApi.kt
|
* CurrentFile JacksonJsonApi.kt
|
||||||
* LastUpdate 2026-10-11 16:54:15
|
* LastUpdate 2026-10-12 10:48:31
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -51,6 +51,8 @@ class JacksonJsonApi : JsonApi {
|
|||||||
this.objectMapper = ObjectMapper()
|
this.objectMapper = ObjectMapper()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getObjectMapper() = objectMapper
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用指定的ObjectMapper构造实例
|
* 使用指定的ObjectMapper构造实例
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import java.lang.reflect.WildcardType
|
|||||||
|
|
||||||
class TypeReference(
|
class TypeReference(
|
||||||
val rawClass: Type,
|
val rawClass: Type,
|
||||||
vararg paramType: Type
|
vararg paramType: Type,
|
||||||
) : ParameterizedType {
|
) : ParameterizedType {
|
||||||
|
|
||||||
private val typeArguments: Array<out Type> = paramType
|
private val typeArguments: Array<out Type> = paramType
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile DateTimeJsonConverter.kt
|
* CurrentFile DateTimeJsonConverter.kt
|
||||||
* LastUpdate 2026-02-07 22:13:41
|
* LastUpdate 2026-11-09 14:35:06
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* 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 NetworkEndpointJsonConverter.kt
|
||||||
|
* LastUpdate 2026-11-09 14:36:36
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.json.converters
|
||||||
|
|
||||||
|
import com.mingliqiye.utils.json.api.type.JsonTypeReference
|
||||||
|
import com.mingliqiye.utils.json.converters.NetworkEndpointJsonConverter.Companion.getJsonConverter
|
||||||
|
import com.mingliqiye.utils.json.converters.base.AnnotationGetter
|
||||||
|
import com.mingliqiye.utils.json.converters.base.BaseJsonStringConverter
|
||||||
|
import com.mingliqiye.utils.network.NetworkEndpoint
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [NetworkEndpoint] 的 JSON 字符串转换器。
|
||||||
|
*
|
||||||
|
* 继承 [BaseJsonStringConverter],负责在网络端点对象与 JSON 字符串之间进行转换:
|
||||||
|
* 序列化时输出形如 `host:port` 的字符串,反序列化时将 `host:port` 字符串还原为
|
||||||
|
* [NetworkEndpoint] 实例。
|
||||||
|
*
|
||||||
|
* 本类为单例:构造器私有,通过 [getJsonConverter] 获取全局共享实例,
|
||||||
|
* 避免在序列化框架中重复创建。
|
||||||
|
*
|
||||||
|
* @see BaseJsonStringConverter
|
||||||
|
* @see NetworkEndpoint
|
||||||
|
*/
|
||||||
|
class NetworkEndpointJsonConverter private constructor() : BaseJsonStringConverter<NetworkEndpoint> {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val networkEndpointJsonConverter by lazy {
|
||||||
|
NetworkEndpointJsonConverter()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 [NetworkEndpointJsonConverter] 的全局单例。
|
||||||
|
*
|
||||||
|
* @return 共享的转换器实例。
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun getJsonConverter(): NetworkEndpointJsonConverter = networkEndpointJsonConverter
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [NetworkEndpoint] 序列化为 JSON 字符串。
|
||||||
|
*
|
||||||
|
* 输出格式为 `host:port`,由 [NetworkEndpoint.toHostPortString] 生成。
|
||||||
|
*
|
||||||
|
* @param obj 待序列化的网络端点,可能为 `null`。
|
||||||
|
* @param annotationGetter 注解信息提供者,用于在需要时读取格式化相关注解;
|
||||||
|
* 当前实现不依赖注解,保留该参数以符合转换器契约。
|
||||||
|
* @return 序列化后的 `host:port` 字符串;当 [obj] 为 `null` 时返回 `null`。
|
||||||
|
* @throws Exception 若序列化过程中发生异常。
|
||||||
|
*/
|
||||||
|
@Throws(Exception::class)
|
||||||
|
override fun convert(obj: NetworkEndpoint?, annotationGetter: AnnotationGetter): String? {
|
||||||
|
return obj?.let(NetworkEndpoint::toHostPortString)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 JSON 字符串反序列化为 [NetworkEndpoint]。
|
||||||
|
*
|
||||||
|
* 期望输入为 `host:port` 形式的字符串,由 [NetworkEndpoint.Companion.of] 解析。
|
||||||
|
*
|
||||||
|
* @param obj 待反序列化的字符串,可能为 `null`。
|
||||||
|
* @param annotationGetter 注解信息提供者,用于在需要时读取解析相关注解;
|
||||||
|
* 当前实现不依赖注解,保留该参数以符合转换器契约。
|
||||||
|
* @return 解析得到的 [NetworkEndpoint];当 [obj] 为 `null` 时返回 `null`。
|
||||||
|
* @throws Exception 若字符串格式非法或解析过程中发生异常。
|
||||||
|
*/
|
||||||
|
@Throws(Exception::class)
|
||||||
|
override fun deConvert(obj: String?, annotationGetter: AnnotationGetter): NetworkEndpoint? {
|
||||||
|
return obj?.let(NetworkEndpoint.Companion::of)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取本转换器支持的目标类型引用。
|
||||||
|
*
|
||||||
|
* @return 指向 [NetworkEndpoint] 的 [JsonTypeReference]。
|
||||||
|
* @throws Exception 若获取类型引用时发生异常。
|
||||||
|
*/
|
||||||
|
@Throws(Exception::class)
|
||||||
|
override fun getFromType(): JsonTypeReference<NetworkEndpoint> = JsonTypeReference.Companion.of()
|
||||||
|
}
|
||||||
@@ -58,7 +58,7 @@ class UUIDJsonConverter private constructor() : BaseJsonStringConverter<UUID> {
|
|||||||
*/
|
*/
|
||||||
override fun convert(
|
override fun convert(
|
||||||
obj: UUID?,
|
obj: UUID?,
|
||||||
annotationGetter: AnnotationGetter
|
annotationGetter: AnnotationGetter,
|
||||||
): String? {
|
): String? {
|
||||||
// 如果输入对象为 null,直接返回 null
|
// 如果输入对象为 null,直接返回 null
|
||||||
if (obj.isNull()) return null
|
if (obj.isNull()) return null
|
||||||
@@ -85,7 +85,7 @@ class UUIDJsonConverter private constructor() : BaseJsonStringConverter<UUID> {
|
|||||||
*/
|
*/
|
||||||
override fun deConvert(
|
override fun deConvert(
|
||||||
obj: String?,
|
obj: String?,
|
||||||
annotationGetter: AnnotationGetter
|
annotationGetter: AnnotationGetter,
|
||||||
): UUID? {
|
): UUID? {
|
||||||
// 如果输入字符串为 null 或空,直接返回 null
|
// 如果输入字符串为 null 或空,直接返回 null
|
||||||
if (obj.isNullish()) return null
|
if (obj.isNullish()) return null
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ import tools.jackson.databind.ValueDeserializer
|
|||||||
|
|
||||||
class JackJson3Deserializer<F, T>(
|
class JackJson3Deserializer<F, T>(
|
||||||
val property: BeanProperty? = null,
|
val property: BeanProperty? = null,
|
||||||
val jsonConverter: JsonConverter<F, T>
|
val jsonConverter: JsonConverter<F, T>,
|
||||||
) : ValueDeserializer<F>() {
|
) : ValueDeserializer<F>() {
|
||||||
|
|
||||||
override fun deserialize(
|
override fun deserialize(
|
||||||
p: JsonParser,
|
p: JsonParser,
|
||||||
ctxt: DeserializationContext?
|
ctxt: DeserializationContext?,
|
||||||
): F? {
|
): F? {
|
||||||
if (p.currentToken() == JsonToken.VALUE_NULL) return null
|
if (p.currentToken() == JsonToken.VALUE_NULL) return null
|
||||||
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
|
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
|
||||||
@@ -53,7 +53,7 @@ class JackJson3Deserializer<F, T>(
|
|||||||
*/
|
*/
|
||||||
override fun createContextual(
|
override fun createContextual(
|
||||||
ctxt: DeserializationContext?,
|
ctxt: DeserializationContext?,
|
||||||
property: BeanProperty?
|
property: BeanProperty?,
|
||||||
): JackJson3Deserializer<F, T> {
|
): JackJson3Deserializer<F, T> {
|
||||||
return JackJson3Deserializer(property, jsonConverter)
|
return JackJson3Deserializer(property, jsonConverter)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import java.math.BigInteger
|
|||||||
|
|
||||||
class JackJson3Serializer<F, T>(
|
class JackJson3Serializer<F, T>(
|
||||||
val property: BeanProperty? = null,
|
val property: BeanProperty? = null,
|
||||||
val jsonConverter: JsonConverter<F, T>
|
val jsonConverter: JsonConverter<F, T>,
|
||||||
) : ValueSerializer<F>() {
|
) : ValueSerializer<F>() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -31,11 +31,11 @@ import com.fasterxml.jackson.databind.deser.ContextualDeserializer
|
|||||||
|
|
||||||
class JackJsonDeserializer<F, T>(
|
class JackJsonDeserializer<F, T>(
|
||||||
val property: BeanProperty? = null,
|
val property: BeanProperty? = null,
|
||||||
val jsonConverter: JsonConverter<F, T>
|
val jsonConverter: JsonConverter<F, T>,
|
||||||
) : JsonDeserializer<F>(), ContextualDeserializer {
|
) : JsonDeserializer<F>(), ContextualDeserializer {
|
||||||
override fun deserialize(
|
override fun deserialize(
|
||||||
p: JsonParser,
|
p: JsonParser,
|
||||||
ctxt: DeserializationContext?
|
ctxt: DeserializationContext?,
|
||||||
): F? {
|
): F? {
|
||||||
if (p.currentToken == JsonToken.VALUE_NULL) return null
|
if (p.currentToken == JsonToken.VALUE_NULL) return null
|
||||||
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
|
return jsonConverter.deConvert(p.readValueAs(jsonConverter.getToClass()), object : AnnotationGetter {
|
||||||
@@ -52,7 +52,7 @@ class JackJsonDeserializer<F, T>(
|
|||||||
*/
|
*/
|
||||||
override fun createContextual(
|
override fun createContextual(
|
||||||
ctxt: DeserializationContext?,
|
ctxt: DeserializationContext?,
|
||||||
property: BeanProperty?
|
property: BeanProperty?,
|
||||||
): JackJsonDeserializer<F, T> {
|
): JackJsonDeserializer<F, T> {
|
||||||
return JackJsonDeserializer(property, jsonConverter)
|
return JackJsonDeserializer(property, jsonConverter)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import java.math.BigInteger
|
|||||||
|
|
||||||
class JackJsonSerializer<F, T>(
|
class JackJsonSerializer<F, T>(
|
||||||
val property: BeanProperty?,
|
val property: BeanProperty?,
|
||||||
val jsonConverter: JsonConverter<F, T>
|
val jsonConverter: JsonConverter<F, T>,
|
||||||
) : JsonSerializer<F>(), ContextualSerializer {
|
) : JsonSerializer<F>(), ContextualSerializer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +45,7 @@ class JackJsonSerializer<F, T>(
|
|||||||
override fun serialize(
|
override fun serialize(
|
||||||
value: F?,
|
value: F?,
|
||||||
gen: JsonGenerator,
|
gen: JsonGenerator,
|
||||||
provider: SerializerProvider?
|
provider: SerializerProvider?,
|
||||||
) {
|
) {
|
||||||
// 执行转换逻辑
|
// 执行转换逻辑
|
||||||
val data: T? = jsonConverter.convert(value, object : AnnotationGetter {
|
val data: T? = jsonConverter.convert(value, object : AnnotationGetter {
|
||||||
@@ -78,7 +78,7 @@ class JackJsonSerializer<F, T>(
|
|||||||
*/
|
*/
|
||||||
override fun createContextual(
|
override fun createContextual(
|
||||||
prov: SerializerProvider?,
|
prov: SerializerProvider?,
|
||||||
property: BeanProperty?
|
property: BeanProperty?,
|
||||||
): JsonSerializer<F> {
|
): JsonSerializer<F> {
|
||||||
return JackJsonSerializer(property, jsonConverter)
|
return JackJsonSerializer(property, jsonConverter)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* ProjectName mingli-utils
|
* ProjectName FastBaseMapper.kt
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile JsonConverterUtils.kt
|
* CurrentFile JsonConverterUtils.kt
|
||||||
* LastUpdate 2026-02-26 16:01:49
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
@file:JvmName("JsonConverterUtils")
|
@file:JvmName("JsonConverterUtils")
|
||||||
@@ -74,11 +74,11 @@ inline fun <reified T : BaseJsonConverter<*, *>> com.fasterxml.jackson.databind.
|
|||||||
* @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。
|
* @return 配置了自定义序列化器和反序列化器的 SimpleModule 对象。
|
||||||
*/
|
*/
|
||||||
fun <F, T> JsonConverter<F, T>.getJacksonModule(): com.fasterxml.jackson.databind.module.SimpleModule =
|
fun <F, T> JsonConverter<F, T>.getJacksonModule(): com.fasterxml.jackson.databind.module.SimpleModule =
|
||||||
com.fasterxml.jackson.databind.module.SimpleModule("${getFromClass().name}To${getToClass().name}")
|
com.fasterxml.jackson.databind.module.SimpleModule("(${getFromClass().name})-(${getToClass().name})")
|
||||||
.addSerializer(getFromClass(), JackJsonSerializer(null, this))
|
.addSerializer(getFromClass(), JackJsonSerializer(null, this))
|
||||||
.addDeserializer(getFromClass(), JackJsonDeserializer(null, this))
|
.addDeserializer(getFromClass(), JackJsonDeserializer(null, this))
|
||||||
|
|
||||||
fun <F, T> JsonConverter<F, T>.getJackson3Module(): tools.jackson.databind.module.SimpleModule =
|
fun <F, T> JsonConverter<F, T>.getJackson3Module(): tools.jackson.databind.module.SimpleModule =
|
||||||
tools.jackson.databind.module.SimpleModule("${getFromClass().name}To${getToClass().name}")
|
tools.jackson.databind.module.SimpleModule("(${getFromClass().name})-(${getToClass().name})")
|
||||||
.addSerializer(getFromClass(), JackJson3Serializer(null, this))
|
.addSerializer(getFromClass(), JackJson3Serializer(null, this))
|
||||||
.addDeserializer(getFromClass(), JackJson3Deserializer(null, this))
|
.addDeserializer(getFromClass(), JackJson3Deserializer(null, this))
|
||||||
|
|||||||
@@ -16,18 +16,18 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile MingliUtilsMetaData.kt
|
* CurrentFile MingliUtilsMetaData.kt
|
||||||
* LastUpdate 2026-03-11 08:46:40
|
* LastUpdate 2026-10-12 13:32:30
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.mingliqiye.utils.metadata
|
package com.mingliqiye.utils.metadata
|
||||||
|
|
||||||
class MingliUtilsMetaData {
|
data class MingliUtilsMetaData(
|
||||||
var buildTime: String = ""
|
var buildTime: String = "",
|
||||||
var groupId: String = ""
|
var groupId: String = "",
|
||||||
var artifactId: String = ""
|
var artifactId: String = "",
|
||||||
var version: String = ""
|
var version: String = "",
|
||||||
var buildJdkVersion: String = ""
|
var buildJdkVersion: String = "",
|
||||||
var author: String = ""
|
var author: String = "",
|
||||||
var website: String = ""
|
var website: String = "",
|
||||||
}
|
)
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ abstract class QuickBaseTypeHandler<T> : BaseTypeHandler<T>() {
|
|||||||
vg: QuickBaseTypeHandlerValueGetter,
|
vg: QuickBaseTypeHandlerValueGetter,
|
||||||
ct: CallType,
|
ct: CallType,
|
||||||
ci: Int?,
|
ci: Int?,
|
||||||
cn: String?
|
cn: String?,
|
||||||
): T?
|
): T?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
-2
@@ -42,7 +42,7 @@ class DateTimeTypeHandler : QuickBaseTypeHandler<DateTime>() {
|
|||||||
vg: QuickBaseTypeHandlerValueGetter,
|
vg: QuickBaseTypeHandlerValueGetter,
|
||||||
ct: CallType,
|
ct: CallType,
|
||||||
ci: Int?,
|
ci: Int?,
|
||||||
cn: String?
|
cn: String?,
|
||||||
): DateTime? {
|
): DateTime? {
|
||||||
val data = (when (ct) {
|
val data = (when (ct) {
|
||||||
CallType.RESULTSET_INDEX -> vg.resultSet!!.getTimestamp(ci!!)
|
CallType.RESULTSET_INDEX -> vg.resultSet!!.getTimestamp(ci!!)
|
||||||
@@ -61,7 +61,7 @@ class DateTimeTypeHandler : QuickBaseTypeHandler<DateTime>() {
|
|||||||
ps: PreparedStatement,
|
ps: PreparedStatement,
|
||||||
index: Int,
|
index: Int,
|
||||||
parameter: DateTime?,
|
parameter: DateTime?,
|
||||||
jdbcType: JdbcType?
|
jdbcType: JdbcType?,
|
||||||
) {
|
) {
|
||||||
if (parameter == null) {
|
if (parameter == null) {
|
||||||
ps.setNull(index, TIMESTAMP)
|
ps.setNull(index, TIMESTAMP)
|
||||||
|
|||||||
+2
-2
@@ -58,7 +58,7 @@ class MysqlUUIDBinaryTypeHandler : QuickBaseTypeHandler<UUID>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getValue(
|
override fun getValue(
|
||||||
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?
|
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?,
|
||||||
): UUID? {
|
): UUID? {
|
||||||
|
|
||||||
val data: ByteArray? = when (ct) {
|
val data: ByteArray? = when (ct) {
|
||||||
@@ -76,7 +76,7 @@ class MysqlUUIDBinaryTypeHandler : QuickBaseTypeHandler<UUID>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun setValue(
|
override fun setValue(
|
||||||
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?
|
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?,
|
||||||
) {
|
) {
|
||||||
if (parameter == null) {
|
if (parameter == null) {
|
||||||
ps.setNull(index, java.sql.Types.BINARY)
|
ps.setNull(index, java.sql.Types.BINARY)
|
||||||
|
|||||||
+2
-2
@@ -33,7 +33,7 @@ import java.sql.PreparedStatement
|
|||||||
@MappedTypes(UUID::class)
|
@MappedTypes(UUID::class)
|
||||||
class PgsqlUUIDTypeHandler : QuickBaseTypeHandler<UUID>() {
|
class PgsqlUUIDTypeHandler : QuickBaseTypeHandler<UUID>() {
|
||||||
override fun getValue(
|
override fun getValue(
|
||||||
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?
|
vg: QuickBaseTypeHandlerValueGetter, ct: CallType, ci: Int?, cn: String?,
|
||||||
): UUID? {
|
): UUID? {
|
||||||
val data: String? = when (ct) {
|
val data: String? = when (ct) {
|
||||||
CallType.RESULTSET_INDEX ->
|
CallType.RESULTSET_INDEX ->
|
||||||
@@ -52,7 +52,7 @@ class PgsqlUUIDTypeHandler : QuickBaseTypeHandler<UUID>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun setValue(
|
override fun setValue(
|
||||||
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?
|
ps: PreparedStatement, index: Int, parameter: UUID?, jdbcType: JdbcType?,
|
||||||
) {
|
) {
|
||||||
if (parameter == null) {
|
if (parameter == null) {
|
||||||
ps.setNull(index, jdbcType?.TYPE_CODE ?: JdbcType.JAVA_OBJECT.TYPE_CODE)
|
ps.setNull(index, jdbcType?.TYPE_CODE ?: JdbcType.JAVA_OBJECT.TYPE_CODE)
|
||||||
|
|||||||
@@ -1,156 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 BaseMapperQuery.kt
|
|
||||||
* LastUpdate 2026-02-06 13:15:11
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.mingliqiye.utils.mybatisplus
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper
|
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper
|
|
||||||
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
|
|
||||||
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
"rename to FastBaseMapper",
|
|
||||||
replaceWith = ReplaceWith(
|
|
||||||
expression = "FastBaseMapper<*>",
|
|
||||||
imports = ["com.mingliqiye.utils.mybatisplus.FastBaseMapper"]
|
|
||||||
),
|
|
||||||
level = DeprecationLevel.WARNING
|
|
||||||
)
|
|
||||||
/**
|
|
||||||
* @since 4.6.1
|
|
||||||
* 已经重命名 [FastBaseMapper]
|
|
||||||
*/
|
|
||||||
interface BaseMapperQuery<T> : FastBaseMapper<T>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 扩展[BaseMapper]接口,提供便捷的查询和更新包装器创建方法
|
|
||||||
* 包含普通包装器、`Lambda`包装器和`Kotlin`专用包装器的创建方法
|
|
||||||
*/
|
|
||||||
interface FastBaseMapper<T> : BaseMapper<T> {
|
|
||||||
/**
|
|
||||||
* 创建[QueryWrapper]实例
|
|
||||||
* @return QueryWrapper<T> 查询包装器实例
|
|
||||||
*/
|
|
||||||
fun queryWrapper() = QueryWrapper<T>()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建[UpdateWrapper]实例
|
|
||||||
* @return UpdateWrapper<T> 更新包装器实例
|
|
||||||
*/
|
|
||||||
fun updateWrapper() = UpdateWrapper<T>()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建[LambdaQueryWrapper]实例
|
|
||||||
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
|
||||||
*/
|
|
||||||
fun lambdaQueryWrapper() = LambdaQueryWrapper<T>()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建[LambdaUpdateWrapper]实例
|
|
||||||
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
|
||||||
*/
|
|
||||||
fun lambdaUpdateWrapper() = LambdaUpdateWrapper<T>()
|
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[QueryWrapper]实例
|
|
||||||
* @param T 范型类型,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return QueryWrapper<T> 查询包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T> BaseMapper<T>.queryWrapper(): QueryWrapper<T> = QueryWrapper<T>(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[UpdateWrapper]实例
|
|
||||||
* @param T 范型类型,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return UpdateWrapper<T> 更新包装器实例
|
|
||||||
*/
|
|
||||||
inline fun <reified T> BaseMapper<T>.updateWrapper(): UpdateWrapper<T> = UpdateWrapper<T>()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[LambdaQueryWrapper]实例
|
|
||||||
* @param T 范型类型,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return LambdaQueryWrapper<T> Lambda查询包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T> BaseMapper<T>.lambdaQueryWrapper(): LambdaQueryWrapper<T> =
|
|
||||||
LambdaQueryWrapper<T>(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[LambdaUpdateWrapper]实例
|
|
||||||
* @param T 范型类型,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T> BaseMapper<T>.lambdaUpdateWrapper(): LambdaUpdateWrapper<T> =
|
|
||||||
LambdaUpdateWrapper<T>(T::class.java)
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[KtQueryWrapper]实例
|
|
||||||
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T : Any> BaseMapper<T>.ktQueryWrapper(): KtQueryWrapper<T> = KtQueryWrapper(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[KtQueryChainWrapper]实例
|
|
||||||
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T : Any> BaseMapper<T>.ktQueryChainWrapper(): KtQueryChainWrapper<T> =
|
|
||||||
KtQueryChainWrapper(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[KtUpdateWrapper]实例
|
|
||||||
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T : Any> BaseMapper<T>.ktUpdateWrapper(): KtUpdateWrapper<T> =
|
|
||||||
KtUpdateWrapper(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 为[BaseMapper]创建[KtUpdateChainWrapper]实例
|
|
||||||
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
|
||||||
* @receiver BaseMapper<T> 基础映射器实例
|
|
||||||
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
|
||||||
*/
|
|
||||||
inline fun <reified T : Any> BaseMapper<T>.ktUpdateChainWrapper(): KtUpdateChainWrapper<T> =
|
|
||||||
KtUpdateChainWrapper(T::class.java)
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,822 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseIService.kt
|
||||||
|
* LastUpdate 2026-10-31 09:51:52
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.mybatisplus
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIService.Companion.ktQueryChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIService.Companion.ktQueryWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIService.Companion.ktUpdateChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIService.Companion.ktUpdateWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.delete
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.deleteByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.exists
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.existsByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryChain
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateChain
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectCount
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectList
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectListByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectListByColumns
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectMapByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectOne
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectOneByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectPage
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.update
|
||||||
|
import kotlin.reflect.KProperty1
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MyBatis-Plus[IService]的便捷基类,在服务层暴露与[FastBaseMapper]一致的常用查询、更新、删除方法
|
||||||
|
* 内部统一委托给[mapper]执行,避免重复书写包装器构建代码
|
||||||
|
* @param T 实体类型
|
||||||
|
* @param D 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
*/
|
||||||
|
open class FastBaseIService<T, D : FastBaseMapper<T>> : ServiceImpl<D, T>(), IService<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前服务绑定的基础映射器实例,等价于[com.baomidou.mybatisplus.extension.repository.CrudRepository.getBaseMapper]
|
||||||
|
* @return D 继承自[FastBaseMapper]的映射器实例
|
||||||
|
*/
|
||||||
|
open val mapper: D
|
||||||
|
get() = getBaseMapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.QueryWrapper]实例
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun queryWrapper() = mapper.queryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper]实例
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun updateWrapper() = mapper.updateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper]实例
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun lambdaQueryWrapper() = mapper.lambdaQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper]实例
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun lambdaUpdateWrapper() = mapper.lambdaUpdateWrapper()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [queryWrapper]的简写形式
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun qw() = queryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [updateWrapper]的简写形式
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun uw() = updateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaQueryWrapper]的简写形式
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun lqw() = lambdaQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaUpdateWrapper]的简写形式
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun luw() = lambdaUpdateWrapper()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[com.baomidou.mybatisplus.core.conditions.query.QueryWrapper]上追加字符串列名等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T>,
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper]上追加方法引用列等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个字符串列名等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 列名与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
vararg columns: FastBaseMapper.StringAnyTuple2,
|
||||||
|
) = mapper.selectListByColumns(columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个方法引用列等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
vararg columns: FastBaseMapper.SFunctionAnyTuple2<T, *>,
|
||||||
|
) = mapper.selectListByColumns(lqw(), columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加多个方法引用列等值条件并查询记录列表,各列条件以AND连接
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
vararg columns: FastBaseMapper.SFunctionAnyTuple2<T, *>,
|
||||||
|
) = mapper.selectListByColumns(wrapper = wrapper, columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectOneByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = selectOneByColumn(
|
||||||
|
column, value, lqw()
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.existsByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = mapper.existsByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
open fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectMapByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
open fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.selectMapByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.deleteByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.deleteByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.deleteByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.deleteByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIService]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktQueryWrapper(): KtQueryWrapper<T> =
|
||||||
|
KtQueryWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIService]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktQueryChainWrapper(): KtQueryChainWrapper<T> =
|
||||||
|
KtQueryChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIService]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktUpdateWrapper(): KtUpdateWrapper<T> =
|
||||||
|
KtUpdateWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIService]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktUpdateChainWrapper(): KtUpdateChainWrapper<T> =
|
||||||
|
KtUpdateChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[KtQueryChainWrapper]实例
|
||||||
|
* 与[ktQueryChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`list`/`one`/`page`等方法执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> 可执行的Kotlin查询链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktQueryChain(): KtQueryChainWrapper<T> =
|
||||||
|
this.mapper.ktQueryChain()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[KtUpdateChainWrapper]实例
|
||||||
|
* 与[ktUpdateChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`update`/`remove`等方法执行操作
|
||||||
|
* @param T 实体类型,必须继承自Any
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> 可执行的Kotlin更新链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.ktUpdateChain(): KtUpdateChainWrapper<T> =
|
||||||
|
this.mapper.ktUpdateChain()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.kqw(): KtQueryWrapper<T> =
|
||||||
|
this.mapper.ktQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.kuw(): KtUpdateWrapper<T> =
|
||||||
|
this.mapper.ktUpdateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryChainWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.kqcw(): KtQueryChainWrapper<T> =
|
||||||
|
this.mapper.ktQueryChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateChainWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.kucw(): KtUpdateChainWrapper<T> =
|
||||||
|
this.mapper.ktUpdateChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtUpdateWrapper]并执行更新
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.update(funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>) =
|
||||||
|
mapper.update(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtUpdateWrapper]并携带实体执行更新
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param entity 承载待更新字段值的实体对象
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.update(
|
||||||
|
entity: T,
|
||||||
|
funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>,
|
||||||
|
) = mapper.update(entity, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectList(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectList(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行分页查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param page 分页参数对象
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectPage(
|
||||||
|
page: Page<T>,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectPage(page, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过页码与每页大小直接执行分页查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param current 当前页码,从1开始
|
||||||
|
* @param size 每页记录数
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectPage(
|
||||||
|
current: Long,
|
||||||
|
size: Long,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectPage(
|
||||||
|
current, size, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并查询单条记录
|
||||||
|
* 结果数量不为1时返回null,不抛出异常
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectOne(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectOne(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并统计记录数
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Long 符合条件的记录总数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectCount(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectCount(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并判断记录是否存在
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.exists(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.exists(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行删除
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.delete(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.delete(
|
||||||
|
funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询记录列表
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多列等值条件查询记录列表
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param columns 列属性引用与列值的键值对,逐项执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在各列等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIService<T, MP>.selectListByColumns(
|
||||||
|
vararg columns: Pair<KProperty1<T, *>, Any?>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumns(columns = columns, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.selectOneByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件判断记录是否存在
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.existsByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.selectMapByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectMapByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件删除记录
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.deleteByColumn(column, value, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIService<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIService<T, MP>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.deleteByColumn(column, value, funs)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,823 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseIService3.kt
|
||||||
|
* LastUpdate 2026-10-31 09:49:08
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.mybatisplus
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
|
import com.baomidou.mybatisplus.spring.service.IService
|
||||||
|
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIServiceNew3518.Companion.ktQueryChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIServiceNew3518.Companion.ktQueryWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIServiceNew3518.Companion.ktUpdateChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseIServiceNew3518.Companion.ktUpdateWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.delete
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.deleteByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.exists
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.existsByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryChain
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateChain
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectCount
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectList
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectListByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectListByColumns
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectMapByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectOne
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectOneByColumn
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectPage
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.update
|
||||||
|
import kotlin.reflect.KProperty1
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MyBatis-Plus[IService]的便捷基类,在服务层暴露与[FastBaseMapper]一致的常用查询、更新、删除方法
|
||||||
|
* 内部统一委托给[mapper]执行,避免重复书写包装器构建代码
|
||||||
|
* 3.5.18 新版本更新
|
||||||
|
* @param T 实体类型
|
||||||
|
* @param D 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
*/
|
||||||
|
open class FastBaseIServiceNew3518<T, D : FastBaseMapper<T>> : ServiceImpl<D, T>(), IService<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前服务绑定的基础映射器实例,等价于[com.baomidou.mybatisplus.extension.repository.CrudRepository.getBaseMapper]
|
||||||
|
* @return D 继承自[FastBaseMapper]的映射器实例
|
||||||
|
*/
|
||||||
|
open val mapper: D
|
||||||
|
get() = getBaseMapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.QueryWrapper]实例
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun queryWrapper() = mapper.queryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper]实例
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun updateWrapper() = mapper.updateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper]实例
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun lambdaQueryWrapper() = mapper.lambdaQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper]实例
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun lambdaUpdateWrapper() = mapper.lambdaUpdateWrapper()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [queryWrapper]的简写形式
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun qw() = queryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [updateWrapper]的简写形式
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun uw() = updateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaQueryWrapper]的简写形式
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
open fun lqw() = lambdaQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaUpdateWrapper]的简写形式
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
open fun luw() = lambdaUpdateWrapper()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[com.baomidou.mybatisplus.core.conditions.query.QueryWrapper]上追加字符串列名等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T>,
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper]上追加方法引用列等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.selectListByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个字符串列名等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 列名与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
vararg columns: FastBaseMapper.StringAnyTuple2,
|
||||||
|
) = mapper.selectListByColumns(columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个方法引用列等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
vararg columns: FastBaseMapper.SFunctionAnyTuple2<T, *>,
|
||||||
|
) = mapper.selectListByColumns(lqw(), columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加多个方法引用列等值条件并查询记录列表,各列条件以AND连接
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
open fun selectListByColumns(
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
vararg columns: FastBaseMapper.SFunctionAnyTuple2<T, *>,
|
||||||
|
) = mapper.selectListByColumns(wrapper = wrapper, columns = columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.selectOneByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = selectOneByColumn(
|
||||||
|
column, value, lqw()
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
open fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
) = mapper.existsByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
) = mapper.existsByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
open fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, wrapper
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
open fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.selectMapByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
open fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
) = mapper.selectMapByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.deleteByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
) = mapper.deleteByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
) = mapper.deleteByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
open fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
) = mapper.deleteByColumn(column, value, wrapper)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIServiceNew3518]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktQueryWrapper(): KtQueryWrapper<T> =
|
||||||
|
KtQueryWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIServiceNew3518]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktQueryChainWrapper(): KtQueryChainWrapper<T> =
|
||||||
|
KtQueryChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIServiceNew3518]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktUpdateWrapper(): KtUpdateWrapper<T> =
|
||||||
|
KtUpdateWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[FastBaseIServiceNew3518]绑定的实体类型创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper]实例
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktUpdateChainWrapper(): KtUpdateChainWrapper<T> =
|
||||||
|
KtUpdateChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[KtQueryChainWrapper]实例
|
||||||
|
* 与[ktQueryChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`list`/`one`/`page`等方法执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> 可执行的Kotlin查询链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktQueryChain(): KtQueryChainWrapper<T> =
|
||||||
|
this.mapper.ktQueryChain()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[KtUpdateChainWrapper]实例
|
||||||
|
* 与[ktUpdateChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`update`/`remove`等方法执行操作
|
||||||
|
* @param T 实体类型,必须继承自Any
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> 可执行的Kotlin更新链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.ktUpdateChain(): KtUpdateChainWrapper<T> =
|
||||||
|
this.mapper.ktUpdateChain()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.kqw(): KtQueryWrapper<T> =
|
||||||
|
this.mapper.ktQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.kuw(): KtUpdateWrapper<T> =
|
||||||
|
this.mapper.ktUpdateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryChainWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.kqcw(): KtQueryChainWrapper<T> =
|
||||||
|
this.mapper.ktQueryChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateChainWrapper]的简写形式
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.kucw(): KtUpdateChainWrapper<T> =
|
||||||
|
this.mapper.ktUpdateChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtUpdateWrapper]并执行更新
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.update(funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>) =
|
||||||
|
mapper.update(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtUpdateWrapper]并携带实体执行更新
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param entity 承载待更新字段值的实体对象
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.update(
|
||||||
|
entity: T,
|
||||||
|
funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>,
|
||||||
|
) = mapper.update(entity, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectList(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectList(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行分页查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param page 分页参数对象
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectPage(
|
||||||
|
page: Page<T>,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectPage(page, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过页码与每页大小直接执行分页查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param current 当前页码,从1开始
|
||||||
|
* @param size 每页记录数
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectPage(
|
||||||
|
current: Long,
|
||||||
|
size: Long,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectPage(
|
||||||
|
current, size, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并查询单条记录
|
||||||
|
* 结果数量不为1时返回null,不抛出异常
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectOne(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectOne(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并统计记录数
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Long 符合条件的记录总数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectCount(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.selectCount(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并判断记录是否存在
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.exists(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.exists(funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[KtQueryWrapper]并执行删除
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.delete(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>) =
|
||||||
|
mapper.delete(
|
||||||
|
funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询记录列表
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多列等值条件查询记录列表
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param columns 列属性引用与列值的键值对,逐项执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在各列等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>> FastBaseIServiceNew3518<T, MP>.selectListByColumns(
|
||||||
|
vararg columns: Pair<KProperty1<T, *>, Any?>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectListByColumns(columns = columns, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.selectOneByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectOneByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件判断记录是否存在
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.existsByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.existsByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.selectMapByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.selectMapByColumn(
|
||||||
|
column, value, funs
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件删除记录
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.deleteByColumn(column, value, funs)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param T 实体类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param MP 基础映射器类型,必须继承自[FastBaseMapper]<T>
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseIServiceNew3518<T, MP> 快速服务实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, MP : FastBaseMapper<T>, R> FastBaseIServiceNew3518<T, MP>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
) = mapper.deleteByColumn(column, value, funs)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,867 @@
|
|||||||
|
/*
|
||||||
|
* 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 FastBaseMapper.kt
|
||||||
|
* LastUpdate 2026-10-28 22:23:47
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.mybatisplus
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktQueryWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateChainWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.ktUpdateWrapper
|
||||||
|
import com.mingliqiye.utils.mybatisplus.FastBaseMapper.Companion.selectListByColumns
|
||||||
|
import com.mingliqiye.utils.tuple.Tuple
|
||||||
|
import kotlin.reflect.KProperty1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展[com.baomidou.mybatisplus.core.mapper.BaseMapper]接口,提供便捷的查询和更新包装器创建方法
|
||||||
|
* 包含普通包装器、`Lambda`包装器和`Kotlin`专用包装器的创建方法
|
||||||
|
*/
|
||||||
|
interface FastBaseMapper<T> : BaseMapper<T> {
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.QueryWrapper]实例
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
fun queryWrapper() = QueryWrapper<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper]实例
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
fun updateWrapper() = UpdateWrapper<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper]实例
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
fun lambdaQueryWrapper() = LambdaQueryWrapper<T>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建[com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper]实例
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
fun lambdaUpdateWrapper() = LambdaUpdateWrapper<T>()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [queryWrapper]的简写形式
|
||||||
|
* @return QueryWrapper<T> 查询包装器实例
|
||||||
|
*/
|
||||||
|
fun qw() = queryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [updateWrapper]的简写形式
|
||||||
|
* @return UpdateWrapper<T> 更新包装器实例
|
||||||
|
*/
|
||||||
|
fun uw() = updateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaQueryWrapper]的简写形式
|
||||||
|
* @return LambdaQueryWrapper<T> Lambda查询包装器实例
|
||||||
|
*/
|
||||||
|
fun lqw() = lambdaQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [lambdaUpdateWrapper]的简写形式
|
||||||
|
* @return LambdaUpdateWrapper<T> Lambda更新包装器实例
|
||||||
|
*/
|
||||||
|
fun luw() = lambdaUpdateWrapper()
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
): List<T> = selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T>,
|
||||||
|
): List<T> = selectList(wrapper.eq(column, value))
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
): List<T> = selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并查询记录列表
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
): List<T> = selectList(wrapper.eq(column, value))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
): List<T> = selectListByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
): List<T> {
|
||||||
|
if (value.isEmpty()) return emptyList()
|
||||||
|
return selectList(wrapper.`in`(column, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
): List<T> = selectListByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名IN条件并查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun <R> selectListByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
): List<T> {
|
||||||
|
if (value.isEmpty()) return emptyList()
|
||||||
|
return selectList(wrapper.`in`(column, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符串列名与列值构成的二元组,用于[selectListByColumns]的可变参数传参
|
||||||
|
* @property t1 列名(数据库字段名)
|
||||||
|
* @property t2 列值
|
||||||
|
*/
|
||||||
|
class StringAnyTuple2(
|
||||||
|
val t1: String,
|
||||||
|
val t2: Any,
|
||||||
|
) : Tuple(2) {
|
||||||
|
operator fun component1() = t1
|
||||||
|
operator fun component2() = t2
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 方法引用列与列值构成的二元组,用于[selectListByColumns]的可变参数传参
|
||||||
|
* @param T 实体类型
|
||||||
|
* @param D 列值类型
|
||||||
|
* @property t1 实体列的方法引用
|
||||||
|
* @property t2 列值
|
||||||
|
*/
|
||||||
|
class SFunctionAnyTuple2<T, D>(
|
||||||
|
val t1: SFunction<T, D>,
|
||||||
|
val t2: D,
|
||||||
|
) : Tuple(2) {
|
||||||
|
operator fun component1() = t1
|
||||||
|
operator fun component2() = t2
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个字符串列名等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 列名与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun selectListByColumns(
|
||||||
|
vararg columns: StringAnyTuple2,
|
||||||
|
): List<T> = selectList(qw().let { wrapper ->
|
||||||
|
columns.forEach { (column, value) ->
|
||||||
|
wrapper.eq(column, value)
|
||||||
|
}
|
||||||
|
wrapper
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多个方法引用列等值条件查询记录列表,各列条件以AND连接
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun selectListByColumns(
|
||||||
|
vararg columns: SFunctionAnyTuple2<T, *>,
|
||||||
|
): List<T> = selectListByColumns(lqw(), *columns)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加多个方法引用列等值条件并查询记录列表,各列条件以AND连接
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @param columns 方法引用列与列值构成的二元组,逐项执行等值匹配
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
fun selectListByColumns(
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
vararg columns: SFunctionAnyTuple2<T, *>,
|
||||||
|
): List<T> = selectList(wrapper.let {
|
||||||
|
columns.forEach { (column, value) ->
|
||||||
|
it.eq(column, value)
|
||||||
|
}
|
||||||
|
it
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
): T? = selectOneByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
fun <R> selectOneByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
): T? = selectOne(
|
||||||
|
wrapper.eq(column, value),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
): T? = selectOneByColumn(
|
||||||
|
column, value, lqw()
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
fun <R> selectOneByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
): T? = selectOne(
|
||||||
|
wrapper.eq(column, value),
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
): Boolean = existsByColumn(column, value, qw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上追加字符串列名等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
fun <R> existsByColumn(
|
||||||
|
column: String,
|
||||||
|
value: R,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
): Boolean = exists(
|
||||||
|
wrapper.eq(column, value),
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列等值条件判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
): Boolean = existsByColumn(column, value, lqw())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上追加方法引用列等值条件并判断记录是否存在
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
fun <R> existsByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: R,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
): Boolean = exists(
|
||||||
|
wrapper.eq(column, value),
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
): LinkedHashMap<R, T?> {
|
||||||
|
return selectMapByColumn(column, value, lqw())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
fun <R> selectMapByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T> = lqw(),
|
||||||
|
): LinkedHashMap<R, T?> {
|
||||||
|
if (value.isEmpty()) return linkedMapOf()
|
||||||
|
|
||||||
|
val data = selectList(wrapper.`in`(column, value))
|
||||||
|
|
||||||
|
val mapdata: Map<R, T> = data.associateBy { column.apply(it) }
|
||||||
|
|
||||||
|
val map = LinkedHashMap<R, T?>(value.size)
|
||||||
|
value.forEach { key ->
|
||||||
|
map[key] = mapdata[key]
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
): Int {
|
||||||
|
return deleteByColumn(column, value, qw())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[QueryWrapper]上按字符串列名IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 列名(数据库字段名)
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
fun <R> deleteByColumn(
|
||||||
|
column: String,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: QueryWrapper<T> = qw(),
|
||||||
|
): Int {
|
||||||
|
if (value.isEmpty()) return 0
|
||||||
|
return delete(
|
||||||
|
wrapper.`in`(
|
||||||
|
column, value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
): Int {
|
||||||
|
return deleteByColumn(
|
||||||
|
column, value, lqw()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在给定[LambdaQueryWrapper]上按方法引用列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param R 列值类型
|
||||||
|
* @param column 实体列的方法引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param wrapper 承载查询条件的Lambda查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
fun <R> deleteByColumn(
|
||||||
|
column: SFunction<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
wrapper: LambdaQueryWrapper<T>,
|
||||||
|
): Int {
|
||||||
|
if (value.isEmpty()) return 0
|
||||||
|
return delete(
|
||||||
|
wrapper.`in`(
|
||||||
|
column, value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[BaseMapper]创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]实例
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver BaseMapper<T> 基础映射器实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> BaseMapper<T>.ktQueryWrapper(): KtQueryWrapper<T> = KtQueryWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[BaseMapper]创建[com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper]实例
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver BaseMapper<T> 基础映射器实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> BaseMapper<T>.ktQueryChainWrapper(): KtQueryChainWrapper<T> =
|
||||||
|
KtQueryChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[BaseMapper]创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper]实例
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver BaseMapper<T> 基础映射器实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> BaseMapper<T>.ktUpdateWrapper(): KtUpdateWrapper<T> =
|
||||||
|
KtUpdateWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为[BaseMapper]创建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper]实例
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver BaseMapper<T> 基础映射器实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> BaseMapper<T>.ktUpdateChainWrapper(): KtUpdateChainWrapper<T> =
|
||||||
|
KtUpdateChainWrapper(T::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper]实例
|
||||||
|
* 与[ktQueryChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`list`/`one`/`page`等方法执行查询
|
||||||
|
* @param T 范型类型,必须继承自Any
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtQueryChainWrapper<T> 可执行的Kotlin查询链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any> FastBaseMapper<T>.ktQueryChain(): KtQueryChainWrapper<T> =
|
||||||
|
KtQueryChainWrapper<T>(this as BaseMapper<T>)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建绑定当前映射器的[com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper]实例
|
||||||
|
* 与[ktUpdateChainWrapper]不同,该链式包装器持有映射器引用,可直接调用`update`/`remove`等方法执行操作
|
||||||
|
* @param T 范型类型,必须继承自Any
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtUpdateChainWrapper<T> 可执行的Kotlin更新链式包装器实例
|
||||||
|
*/
|
||||||
|
fun <T : Any> FastBaseMapper<T>.ktUpdateChain(): KtUpdateChainWrapper<T> =
|
||||||
|
KtUpdateChainWrapper<T>(this as BaseMapper<T>)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryWrapper]的简写形式
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtQueryWrapper<T> Kotlin查询包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.kqw(): KtQueryWrapper<T> = ktQueryWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateWrapper]的简写形式
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtUpdateWrapper<T> Kotlin更新包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.kuw(): KtUpdateWrapper<T> = ktUpdateWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktQueryChainWrapper]的简写形式
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtQueryChainWrapper<T> Kotlin查询链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.kqcw(): KtQueryChainWrapper<T> = ktQueryChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ktUpdateChainWrapper]的简写形式
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @return KtUpdateChainWrapper<T> Kotlin更新链式包装器实例,使用T类作为参数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.kucw(): KtUpdateChainWrapper<T> = ktUpdateChainWrapper()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper]并执行更新
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.update(funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>): Int =
|
||||||
|
this.update(funs(kuw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper]并携带实体执行更新
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param entity 承载待更新字段值的实体对象
|
||||||
|
* @param funs 更新包装器构建函数,返回构建完成的更新包装器
|
||||||
|
* @return Int 受影响的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.update(
|
||||||
|
entity: T,
|
||||||
|
funs: (wrapper: KtUpdateWrapper<T>) -> KtUpdateWrapper<T>,
|
||||||
|
): Int = this.update(entity, funs(kuw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并执行查询
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectList(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>): List<T> =
|
||||||
|
this.selectList(funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并执行分页查询
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param page 分页参数对象
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectPage(
|
||||||
|
page: Page<T>,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): Page<T> = this.selectPage(page, funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过页码与每页大小直接执行分页查询
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param current 当前页码,从1开始
|
||||||
|
* @param size 每页记录数
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Page<T> 分页查询结果
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectPage(
|
||||||
|
current: Long,
|
||||||
|
size: Long,
|
||||||
|
funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): Page<T> = this.selectPage(Page(current, size), funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并查询单条记录
|
||||||
|
* 结果数量不为1时返回null,不抛出异常
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectOne(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>): T? =
|
||||||
|
this.selectList(funs).let {
|
||||||
|
if (it.size != 1) null
|
||||||
|
else it[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并统计记录数
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Long 符合条件的记录总数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectCount(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>): Long =
|
||||||
|
this.selectCount(funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并判断记录是否存在
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.exists(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>): Boolean =
|
||||||
|
this.exists(funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Lambda构建[com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper]并执行删除
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param funs 查询包装器构建函数,返回构建完成的查询包装器
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.delete(funs: (wrapper: KtQueryWrapper<T>) -> KtQueryWrapper<T>): Int =
|
||||||
|
this.delete(funs(kqw()))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询记录列表
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): List<T> = selectList { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).eq(column, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录列表,集合为空时直接返回空列表而不执行查询
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.selectListByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): List<T> {
|
||||||
|
if (value.isEmpty()) return emptyList()
|
||||||
|
return selectList { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).`in`(column, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按多列等值条件查询记录列表
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param columns 列属性引用与列值的键值对,逐项执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在各列等值条件之前应用
|
||||||
|
* @return List<T> 查询结果列表
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any> FastBaseMapper<T>.selectListByColumns(
|
||||||
|
vararg columns: Pair<KProperty1<T, *>, Any?>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): List<T> = selectList { wrapper: KtQueryWrapper<T> ->
|
||||||
|
val w = funs(wrapper)
|
||||||
|
columns.forEach { (column, value) -> w.eq(column, value) }
|
||||||
|
w
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件查询单条记录,结果数量不为1时返回null
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return T? 唯一的查询结果,不存在或多于一条时返回null
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.selectOneByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): T? = selectOne { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).eq(column, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件判断记录是否存在
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @return Boolean 存在符合条件的记录返回true,否则返回false
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.existsByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): Boolean = exists { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).eq(column, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件查询记录并构建有序映射,集合为空时直接返回空映射而不执行查询
|
||||||
|
* 返回的[LinkedHashMap]保持入参集合的迭代顺序,缺失的键对应的值为null
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return LinkedHashMap<R, T?> 以列值为键、实体为值的有序映射
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.selectMapByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): LinkedHashMap<R, T?> {
|
||||||
|
if (value.isEmpty()) return linkedMapOf()
|
||||||
|
|
||||||
|
val data = selectList { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).`in`(column, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
val mapdata: Map<R, T> = data.associateBy { column(it) }
|
||||||
|
|
||||||
|
val map = LinkedHashMap<R, T?>(value.size)
|
||||||
|
value.forEach { key ->
|
||||||
|
map[key] = mapdata[key]
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列等值条件删除记录
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值,执行等值匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在等值条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: R,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): Int = delete { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).eq(column, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按单列IN条件删除记录,集合为空时直接返回0而不执行删除
|
||||||
|
* 注意:MyBatis-Plus对空集合的IN条件会整体跳过,若不拦截将导致全表删除
|
||||||
|
* @param T 范型类型,必须继承自Any,通过reified获取实际类型
|
||||||
|
* @param R 列值类型
|
||||||
|
* @receiver FastBaseMapper<T> 快速映射器实例
|
||||||
|
* @param column 实体列属性引用
|
||||||
|
* @param value 列值集合,执行IN匹配
|
||||||
|
* @param funs 附加查询包装器构建函数,在IN条件之前应用
|
||||||
|
* @return Int 被删除的行数
|
||||||
|
*/
|
||||||
|
inline fun <reified T : Any, R> FastBaseMapper<T>.deleteByColumn(
|
||||||
|
column: KProperty1<T, R>,
|
||||||
|
value: Collection<R>,
|
||||||
|
funs: (w: KtQueryWrapper<T>) -> KtQueryWrapper<T> = { it },
|
||||||
|
): Int {
|
||||||
|
if (value.isEmpty()) return 0
|
||||||
|
return delete { wrapper: KtQueryWrapper<T> ->
|
||||||
|
funs(wrapper).`in`(column, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile NettyUtils.kt
|
* CurrentFile NettyUtils.kt
|
||||||
* LastUpdate 2026-06-27 21:40:00
|
* LastUpdate 2026-10-20 20:05:37
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -323,7 +323,7 @@ fun ByteBuf.releaseSafely(): Boolean {
|
|||||||
*/
|
*/
|
||||||
fun ByteBuf.tryRelease(): Boolean {
|
fun ByteBuf.tryRelease(): Boolean {
|
||||||
return try {
|
return try {
|
||||||
if (refCnt() > 0) release() else false
|
refCnt() > 0 && release()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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 BooleanUtils.kt
|
||||||
|
* LastUpdate 2026-10-22 19:57:04
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.objects
|
||||||
|
|
||||||
|
class BooleanIfFunction(private val boolean: Boolean) {
|
||||||
|
|
||||||
|
fun elses(function: () -> Unit): Boolean {
|
||||||
|
if (boolean) {
|
||||||
|
function()
|
||||||
|
}
|
||||||
|
return !boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun Boolean.ifs(function: () -> Unit): BooleanIfFunction {
|
||||||
|
return if (this) {
|
||||||
|
function()
|
||||||
|
BooleanIfFunction(false)
|
||||||
|
} else BooleanIfFunction(true)
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 mingliqiye
|
* Copyright 2026 mingliqiye
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile OsPath.kt
|
* CurrentFile OsPath.kt
|
||||||
* LastUpdate 2025-09-18 09:47:43
|
* LastUpdate 2026-10-20 20:05:37
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -26,8 +26,7 @@ package com.mingliqiye.utils.path
|
|||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.nio.file.Path
|
import java.nio.file.*
|
||||||
import java.nio.file.Paths
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.function.Consumer
|
import java.util.function.Consumer
|
||||||
|
|
||||||
@@ -40,6 +39,41 @@ class OsPath private constructor(private val path: Path) : Path by path {
|
|||||||
return path.spliterator()
|
return path.spliterator()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun startsWith(other: String): Boolean {
|
||||||
|
return path.startsWith(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun endsWith(other: String): Boolean {
|
||||||
|
return path.endsWith(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resolve(other: String): Path {
|
||||||
|
return path.resolve(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resolveSibling(other: Path): Path {
|
||||||
|
return path.resolveSibling(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resolveSibling(other: String): Path {
|
||||||
|
return path.resolveSibling(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toFile(): File {
|
||||||
|
return path.toFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun register(
|
||||||
|
watcher: WatchService,
|
||||||
|
vararg events: WatchEvent.Kind<*>,
|
||||||
|
): WatchKey {
|
||||||
|
return path.register(watcher, *events)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun iterator(): MutableIterator<Path> {
|
||||||
|
return path.iterator()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun of(path: String): OsPath {
|
fun of(path: String): OsPath {
|
||||||
|
|||||||
@@ -13,132 +13,194 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* ProjectName mingli-utils
|
* ProjectName FastBaseMapper.kt
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile ObjectPool.kt
|
* CurrentFile ObjectPool.kt
|
||||||
* LastUpdate 2026-10-02 16:19:05
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.mingliqiye.utils.pool
|
package com.mingliqiye.utils.pool
|
||||||
|
|
||||||
import com.mingliqiye.utils.require.Require.require
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue
|
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
|
||||||
import java.util.concurrent.locks.Condition
|
import java.util.concurrent.locks.Condition
|
||||||
import java.util.concurrent.locks.ReentrantLock
|
import java.util.concurrent.locks.ReentrantLock
|
||||||
import kotlin.concurrent.withLock
|
import kotlin.concurrent.withLock
|
||||||
|
|
||||||
@Deprecated("未实现")
|
/**
|
||||||
|
* 通用对象池抽象实现。
|
||||||
|
*
|
||||||
|
* 支持最小空闲数量、最大容量、超时借用与自动回收。
|
||||||
|
* 子类只需实现 [init] 与 [destroy] 即可完成对象的创建与销毁。
|
||||||
|
*
|
||||||
|
* 线程安全:内部通过 [ReentrantLock] + [Condition] 协调借出/归还的等待与唤醒。
|
||||||
|
*
|
||||||
|
* @param T 池中管理的对象类型
|
||||||
|
* @property maxSize 池允许存在的对象总数上限(含已借出),必须大于 0
|
||||||
|
* @property minSize 池需要保持的最小空闲对象数,[borrow] 归还会在低于该值时补充
|
||||||
|
* @property initialSize 初始化时预热的对象数量,必须 >= [minSize] 且 <= [maxSize]
|
||||||
|
*/
|
||||||
abstract class ObjectPool<T>(
|
abstract class ObjectPool<T>(
|
||||||
private val maxSize: Int = Int.MAX_VALUE,
|
private val maxSize: Int = Int.MAX_VALUE,
|
||||||
private val minSize: Int = 0,
|
private val minSize: Int = 0,
|
||||||
private val initialSize: Int = 1,
|
private val initialSize: Int = 1,
|
||||||
) : Pool<T> {
|
) : Pool<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 已创建(含空闲与借出)对象的总数,用于容量控制。
|
||||||
|
* 所有读写均在 [lock] 内完成,保证与 [borrowedCount]、[pool] 一致。
|
||||||
|
*/
|
||||||
|
private var createdCount: Int = 0
|
||||||
|
|
||||||
private val initialCount = AtomicInteger(0)
|
/** 当前已借出、尚未归还的对象数量([lock] 保护) */
|
||||||
private val pool = ConcurrentLinkedQueue<ObjectPoolData<T>>()
|
private var borrowedCount: Int = 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 空闲对象队列。全部访问均在 [lock] 内完成,
|
||||||
|
* 因此 [ArrayDeque.size] 即为精确空闲数。
|
||||||
|
*/
|
||||||
|
private val pool = ArrayDeque<ObjectPoolData<T>>()
|
||||||
|
|
||||||
|
/** 池是否已关闭 */
|
||||||
private val isClosed = AtomicBoolean(false)
|
private val isClosed = AtomicBoolean(false)
|
||||||
|
|
||||||
private val lock = ReentrantLock()
|
private val lock = ReentrantLock()
|
||||||
private val notEmpty: Condition = lock.newCondition()
|
private val notEmpty: Condition = lock.newCondition()
|
||||||
|
private val notFull: Condition = lock.newCondition()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
require(maxSize >= minSize)
|
require(maxSize >= minSize)
|
||||||
require(initialSize >= minSize)
|
require(initialSize >= minSize)
|
||||||
|
require(initialSize <= maxSize)
|
||||||
require(maxSize > 0)
|
require(maxSize > 0)
|
||||||
repeat(initialSize) {
|
repeat(initialSize) {
|
||||||
pool.add(init(initialCount.getAndIncrement()))
|
pool.addLast(init(createdCount++))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭对象池:销毁所有空闲对象,唤醒所有等待者。
|
||||||
|
* 已借出的对象在归还时会被直接销毁。
|
||||||
|
*/
|
||||||
override fun close() {
|
override fun close() {
|
||||||
for (poolData in pool) {
|
lock.withLock {
|
||||||
destroy(poolData)
|
if (!isClosed.compareAndSet(false, true)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
while (pool.isNotEmpty()) {
|
||||||
|
val data = pool.removeFirst()
|
||||||
|
runCatching { destroy(data) }
|
||||||
|
createdCount--
|
||||||
|
}
|
||||||
|
notEmpty.signalAll()
|
||||||
|
notFull.signalAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从池中借出一个对象。
|
||||||
|
*
|
||||||
|
* 若池中有空闲对象则直接返回;否则在未达 [maxSize] 时新建;
|
||||||
|
* 达到上限时根据 [timeoutMillis] 阻塞等待归还,超时抛出 [IllegalStateException]。
|
||||||
|
*
|
||||||
|
* @param timeoutMillis 等待超时(毫秒)。<= 0 表示无限等待。
|
||||||
|
*/
|
||||||
override fun borrow(timeoutMillis: Long): ObjectPoolData<T> {
|
override fun borrow(timeoutMillis: Long): ObjectPoolData<T> {
|
||||||
lock.withLock {
|
lock.withLock {
|
||||||
|
var remainingNanos = if (timeoutMillis > 0) TimeUnit.MILLISECONDS.toNanos(timeoutMillis) else -1L
|
||||||
|
|
||||||
|
while (true) {
|
||||||
if (isClosed.get()) {
|
if (isClosed.get()) {
|
||||||
throw IllegalStateException("Object pool closed")
|
throw IllegalStateException("Object pool closed")
|
||||||
}
|
}
|
||||||
pool.poll()?.let { return it }
|
|
||||||
if (initialCount.get() < maxSize) {
|
// 1. 优先取空闲对象
|
||||||
return init(initialCount.getAndIncrement())
|
if (pool.isNotEmpty()) {
|
||||||
|
val data = pool.removeFirst()
|
||||||
|
borrowedCount++
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 池未满则创建新对象
|
||||||
|
if (createdCount < maxSize) {
|
||||||
|
val id = createdCount++
|
||||||
|
val data = try {
|
||||||
|
init(id)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
createdCount--
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
borrowedCount++
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 池已满且无空闲,等待归还
|
// 3. 池已满且无空闲,等待归还
|
||||||
if (timeoutMillis <= 0) {
|
if (remainingNanos < 0) {
|
||||||
// 无限等待
|
notEmpty.await()
|
||||||
while (pool.isEmpty()) {
|
} else if (remainingNanos == 0L) {
|
||||||
notEmpty.await() // 释放锁并等待
|
throw IllegalStateException("Timeout waiting for object from pool")
|
||||||
}
|
} else {
|
||||||
return pool.poll()
|
remainingNanos = notEmpty.awaitNanos(remainingNanos)
|
||||||
}
|
if (remainingNanos <= 0L) {
|
||||||
|
|
||||||
// 有限时间等待
|
|
||||||
var remainingNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMillis)
|
|
||||||
while (pool.isEmpty()) {
|
|
||||||
if (remainingNanos <= 0) {
|
|
||||||
throw IllegalStateException("Timeout waiting for object from pool")
|
throw IllegalStateException("Timeout waiting for object from pool")
|
||||||
}
|
}
|
||||||
remainingNanos = notEmpty.awaitNanos(remainingNanos)
|
|
||||||
}
|
}
|
||||||
return pool.poll()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 归还对象到池中。
|
||||||
|
* 若池已关闭或对象不属于当前池,则直接销毁;否则回填到空闲队列并唤醒等待者。
|
||||||
|
*/
|
||||||
override fun returnObject(obj: ObjectPoolData<T>) {
|
override fun returnObject(obj: ObjectPoolData<T>) {
|
||||||
lock.withLock {
|
lock.withLock {
|
||||||
if (isClosed.get()) {
|
borrowedCount--
|
||||||
throw InterruptedException("Object pool closed")
|
|
||||||
|
if (isClosed.get() || obj.objectPool !== this) {
|
||||||
|
runCatching { destroy(obj) }
|
||||||
|
createdCount--
|
||||||
|
notFull.signal()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
pool.add(obj)
|
|
||||||
|
pool.addLast(obj)
|
||||||
notEmpty.signal()
|
notEmpty.signal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前空闲对象数量(精确值)。
|
||||||
|
* 通过短暂持锁读取,与 [activeSize]/[totalSize] 各自独立但均为调用瞬间的真实值。
|
||||||
|
*/
|
||||||
|
fun idleSize(): Int = lock.withLock { pool.size }
|
||||||
|
|
||||||
|
/** 当前已借出、尚未归还的对象数量(精确值) */
|
||||||
|
fun activeSize(): Int = lock.withLock { borrowedCount }
|
||||||
|
|
||||||
|
/** 已创建对象总数,含空闲与借出(精确值) */
|
||||||
|
fun totalSize(): Int = lock.withLock { createdCount }
|
||||||
|
|
||||||
|
/** 池是否已关闭 */
|
||||||
|
fun closed(): Boolean = isClosed.get()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一次性读取空闲 / 借出 / 总数 的一致快照(同一把锁内)。
|
||||||
|
*
|
||||||
|
* 相比分别调用三个方法,可避免三者之间因并发导致的不自洽
|
||||||
|
* (例如 `idle + active != total`)。
|
||||||
|
*/
|
||||||
|
fun snapshot(): PoolSnapshot = lock.withLock {
|
||||||
|
PoolSnapshot(idle = pool.size, active = borrowedCount, total = createdCount, closed = isClosed.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
interface Pool<T> {
|
* 池状态快照。
|
||||||
fun close()
|
* @property idle 空闲对象数
|
||||||
fun borrow(timeoutMillis: Long): ObjectPoolData<T>
|
* @property active 已借出对象数
|
||||||
fun borrow(): ObjectPoolData<T> = borrow(0)
|
* @property total 已创建对象总数(= idle + active)
|
||||||
fun returnObject(obj: ObjectPoolData<T>)
|
* @property closed 池是否已关闭
|
||||||
fun use(funs: (ObjectPoolData<T>) -> Unit) = use(0, funs)
|
*/
|
||||||
fun init(id: Int): ObjectPoolData<T>
|
data class PoolSnapshot(val idle: Int, val active: Int, val total: Int, val closed: Boolean)
|
||||||
fun destroy(data: ObjectPoolData<T>)
|
|
||||||
fun use(timeoutMillis: Long, funs: (ObjectPoolData<T>) -> Unit) {
|
|
||||||
val i = borrow(timeoutMillis)
|
|
||||||
funs(i)
|
|
||||||
returnObject(i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class ObjectPoolData<T>(
|
|
||||||
val obj: T,
|
|
||||||
val objectPool: ObjectPool<T>,
|
|
||||||
val id: Int = obj.hashCode(),
|
|
||||||
val name: String = "",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TheardPool : ObjectPool<Thread>() {
|
|
||||||
override fun init(id: Int): ObjectPoolData<Thread> {
|
|
||||||
return ObjectPoolData(
|
|
||||||
Thread().apply {
|
|
||||||
name = "TheardPool $id"
|
|
||||||
},
|
|
||||||
this
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun destroy(data: ObjectPoolData<Thread>) {
|
|
||||||
data.obj.interrupt()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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 ObjectPoolData.kt
|
||||||
|
* LastUpdate 2026-10-12 13:43:53
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.pool
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 池对象的元数据包装。
|
||||||
|
*
|
||||||
|
* @property obj 实际对象
|
||||||
|
* @property objectPool 归属的对象池
|
||||||
|
* @property id 池内编号,默认取 [obj] 的 hashCode
|
||||||
|
* @property name 可选的名称,用于日志/调试
|
||||||
|
*/
|
||||||
|
data class ObjectPoolData<T>(
|
||||||
|
val obj: T,
|
||||||
|
val objectPool: ObjectPool<T>,
|
||||||
|
val id: Int = obj.hashCode(),
|
||||||
|
val name: String = "",
|
||||||
|
) {
|
||||||
|
/** 语法糖:归还到所属池 */
|
||||||
|
fun release() = objectPool.returnObject(this)
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* 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 Pool.kt
|
||||||
|
* LastUpdate 2026-10-12 13:43:53
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.pool
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对象池通用接口。
|
||||||
|
*/
|
||||||
|
interface Pool<T> {
|
||||||
|
/** 关闭池并释放全部资源 */
|
||||||
|
fun close()
|
||||||
|
|
||||||
|
/** 借出一个对象,[timeoutMillis] <= 0 表示无限等待 */
|
||||||
|
fun borrow(timeoutMillis: Long): ObjectPoolData<T>
|
||||||
|
|
||||||
|
/** 无限等待地借出一个对象 */
|
||||||
|
fun borrow(): ObjectPoolData<T> = borrow(0)
|
||||||
|
|
||||||
|
/** 归还对象 */
|
||||||
|
fun returnObject(obj: ObjectPoolData<T>)
|
||||||
|
|
||||||
|
/** 使用默认超时执行 [funs] */
|
||||||
|
fun use(funs: (ObjectPoolData<T>) -> Unit) = use(0, funs)
|
||||||
|
|
||||||
|
/** 创建一个新的池对象 */
|
||||||
|
fun init(id: Int): ObjectPoolData<T>
|
||||||
|
|
||||||
|
/** 销毁池对象 */
|
||||||
|
fun destroy(data: ObjectPoolData<T>)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 借出对象执行 [funs],无论是否抛出异常都会自动归还。
|
||||||
|
*/
|
||||||
|
fun use(timeoutMillis: Long, funs: (ObjectPoolData<T>) -> Unit) {
|
||||||
|
val data = borrow(timeoutMillis)
|
||||||
|
try {
|
||||||
|
funs(data)
|
||||||
|
} finally {
|
||||||
|
runCatching { returnObject(data) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 借出对象执行 [funs] 并返回其结果,无论是否抛出异常都会自动归还。
|
||||||
|
*/
|
||||||
|
fun <R> useResult(timeoutMillis: Long = 0, funs: (ObjectPoolData<T>) -> R): R {
|
||||||
|
val data = borrow(timeoutMillis)
|
||||||
|
try {
|
||||||
|
return funs(data)
|
||||||
|
} finally {
|
||||||
|
runCatching { returnObject(data) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,308 @@
|
|||||||
|
/*
|
||||||
|
* 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 Random.kt
|
||||||
|
* LastUpdate 2026-10-20 20:05:37
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.random
|
||||||
|
|
||||||
|
import java.security.SecureRandom
|
||||||
|
import kotlin.random.Random
|
||||||
|
import kotlin.random.nextInt
|
||||||
|
import kotlin.random.nextLong
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Random对象 - 提供各种随机数据生成方法
|
||||||
|
* 使用SecureRandom确保密码学安全,同时提供便捷的随机数生成接口
|
||||||
|
*/
|
||||||
|
object Random {
|
||||||
|
/**
|
||||||
|
* 安全随机数生成器 - 使用系统强随机算法
|
||||||
|
* 通过lazy委托延迟初始化,提高性能
|
||||||
|
*/
|
||||||
|
private val secureRandom: SecureRandom by lazy {
|
||||||
|
SecureRandom.getInstanceStrong()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标准随机数生成器 - Kotlin标准库提供的随机数生成器
|
||||||
|
* 通过lazy委托延迟初始化
|
||||||
|
*/
|
||||||
|
private val random: Random by lazy {
|
||||||
|
Random.Default
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定长度的随机字节数组
|
||||||
|
* @param size 要生成的字节数组长度
|
||||||
|
* @return 包含随机字节的ByteArray
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextBytes(size: Int): ByteArray = ByteArray(size).also { secureRandom.nextBytes(it) }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成单个随机字节
|
||||||
|
* @return 随机字节值(0-255)
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(): Byte = nextInt(0, 256).toByte()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成无符号随机字节
|
||||||
|
* @return 无符号随机字节值(0-255)
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextUByte(): UByte = nextInt(0, 256).toUByte()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成小于指定最大值的随机字节
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在Byte.MIN_VALUE到max范围内的随机字节
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(max: Byte): Byte = nextInt(Byte.MIN_VALUE.toInt(), max.toInt()).toByte()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的随机字节
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在min到max范围内的随机字节
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(min: Byte, max: Byte): Byte = nextInt(min.toInt(), max.toInt() + 1).toByte()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的无符号随机字节
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在min到max范围内的无符号随机字节
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(min: UByte, max: UByte): UByte = nextInt(min.toInt(), max.toInt() + 1).toUByte()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定IntRange范围内的随机字节
|
||||||
|
* @param range 指定的整数范围
|
||||||
|
* @return 在范围内转换为字节的随机值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(range: IntRange): Byte = nextByte(range.first.toByte(), range.last.toByte())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定UIntRange范围内的无符号随机字节
|
||||||
|
* @param range 指定的无符号整数范围
|
||||||
|
* @return 在范围内转换为无符号字节的随机值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextByte(range: UIntRange): UByte = nextByte(range.first.toUByte(), range.last.toUByte())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机整数
|
||||||
|
* @return 随机整数值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextInt(): Int = secureRandom.nextInt()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成小于指定最大值的随机整数
|
||||||
|
* @param max 最大值(不包含)
|
||||||
|
* @return 0到max-1范围内的随机整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextInt(max: Int): Int = secureRandom.nextInt(max)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的随机整数
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在min到max范围内的随机整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextInt(min: Int, max: Int): Int = random.nextInt(min..max)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定IntRange范围内的随机整数
|
||||||
|
* @param range 指定的整数范围
|
||||||
|
* @return 在范围内的随机整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextInt(range: IntRange): Int = random.nextInt(range)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机短整数
|
||||||
|
* @return 在Short范围内的随机短整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextShort(): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成小于指定最大值的随机短整数
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在Short.MIN_VALUE到max范围内的随机短整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextShort(max: Short): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), max.toInt()).toShort()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的随机短整数
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在min到max范围内的随机短整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextShort(min: Short, max: Short): Short = secureRandom.nextInt(min.toInt(), max.toInt()).toShort()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定IntRange范围内的随机短整数
|
||||||
|
* @param range 指定的整数范围
|
||||||
|
* @return 在范围内转换为短整数的随机值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextShort(range: IntRange): Short = secureRandom.nextInt(range.first, range.last).toShort()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机长整数
|
||||||
|
* @return 随机长整数值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextLong(): Long = secureRandom.nextLong()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成小于指定最大值的随机长整数
|
||||||
|
* @param max 最大值(不包含)
|
||||||
|
* @return 0到max-1范围内的随机长整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextLong(max: Long): Long = secureRandom.nextLong(max)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定范围内的随机长整数
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 在min到max范围内的随机长整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextLong(min: Long, max: Long): Long = random.nextLong(min..max)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成指定LongRange范围内的随机长整数
|
||||||
|
* @param range 指定的长整数范围
|
||||||
|
* @return 在范围内的随机长整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextLong(range: LongRange): Long = random.nextLong(range)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机布尔值
|
||||||
|
* @return 随机true或false
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun nextBoolean(): Boolean = secureRandom.nextBoolean()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String类型的扩展函数 - 从字符串中随机选择字符生成指定长度的新字符串
|
||||||
|
* @param length 要生成的字符串长度
|
||||||
|
* @return 由原字符串中随机字符组成的新字符串
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun String.randomString(length: Int): String = String(CharArray(length) {
|
||||||
|
this[nextInt(indices)]
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成包含数字和字母的随机字符串
|
||||||
|
* @param length 要生成的字符串长度
|
||||||
|
* @return 包含0-9、a-z、A-Z字符的随机字符串
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun randomString(length: Int): String =
|
||||||
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".randomString(length)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array类型的扩展函数 - 从数组中随机选择一个元素
|
||||||
|
* @param T 数组元素类型
|
||||||
|
* @return 数组中的随机元素
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun <T> Array<T>.randomItem(): T = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ByteArray类型的扩展函数 - 从字节数组中随机选择一个元素
|
||||||
|
* @return 字节数组中的随机字节
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun ByteArray.randomItem(): Byte = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IntArray类型的扩展函数 - 从整数数组中随机选择一个元素
|
||||||
|
* @return 整数数组中的随机整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun IntArray.randomItem(): Int = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LongArray类型的扩展函数 - 从长整数数组中随机选择一个元素
|
||||||
|
* @return 长整数数组中的随机长整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun LongArray.randomItem(): Long = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FloatArray类型的扩展函数 - 从浮点数数组中随机选择一个元素
|
||||||
|
* @return 浮点数数组中的随机浮点数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun FloatArray.randomItem(): Float = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DoubleArray类型的扩展函数 - 从双精度浮点数数组中随机选择一个元素
|
||||||
|
* @return 双精度浮点数数组中的随机双精度浮点数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun DoubleArray.randomItem(): Double = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BooleanArray类型的扩展函数 - 从布尔数组中随机选择一个元素
|
||||||
|
* @return 布尔数组中的随机布尔值
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun BooleanArray.randomItem(): Boolean = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CharArray类型的扩展函数 - 从字符数组中随机选择一个元素
|
||||||
|
* @return 字符数组中的随机字符
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun CharArray.randomItem(): Char = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ShortArray类型的扩展函数 - 从短整数数组中随机选择一个元素
|
||||||
|
* @return 短整数数组中的随机短整数
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun ShortArray.randomItem(): Short = this[nextInt(indices)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection类型的扩展函数 - 从集合中随机选择一个元素
|
||||||
|
* @param T 集合元素类型
|
||||||
|
* @return 集合中的随机元素
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun <T> Collection<T>.randomItem(): T = this.elementAt(nextInt(indices))
|
||||||
|
}
|
||||||
@@ -1,307 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 RandomBytes.kt
|
|
||||||
* LastUpdate 2026-10-11 17:11:11
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
package com.mingliqiye.utils.random
|
|
||||||
|
|
||||||
import java.security.SecureRandom
|
|
||||||
import kotlin.random.Random
|
|
||||||
import kotlin.random.nextInt
|
|
||||||
import kotlin.random.nextLong
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Random对象 - 提供各种随机数据生成方法
|
|
||||||
* 使用SecureRandom确保密码学安全,同时提供便捷的随机数生成接口
|
|
||||||
*/
|
|
||||||
object Random {
|
|
||||||
/**
|
|
||||||
* 安全随机数生成器 - 使用系统强随机算法
|
|
||||||
* 通过lazy委托延迟初始化,提高性能
|
|
||||||
*/
|
|
||||||
private val secureRandom: SecureRandom by lazy {
|
|
||||||
SecureRandom.getInstanceStrong()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 标准随机数生成器 - Kotlin标准库提供的随机数生成器
|
|
||||||
* 通过lazy委托延迟初始化
|
|
||||||
*/
|
|
||||||
private val random: Random by lazy {
|
|
||||||
Random
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定长度的随机字节数组
|
|
||||||
* @param size 要生成的字节数组长度
|
|
||||||
* @return 包含随机字节的ByteArray
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextBytes(size: Int): ByteArray = ByteArray(size).also { secureRandom.nextBytes(it) }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成单个随机字节
|
|
||||||
* @return 随机字节值(0-255)
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(): Byte = nextInt(0, 256).toByte()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成无符号随机字节
|
|
||||||
* @return 无符号随机字节值(0-255)
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextUByte(): UByte = nextInt(0, 256).toUByte()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成小于指定最大值的随机字节
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在Byte.MIN_VALUE到max范围内的随机字节
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(max: Byte): Byte = nextInt(Byte.MIN_VALUE.toInt(), max.toInt()).toByte()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定范围内的随机字节
|
|
||||||
* @param min 最小值(包含)
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在min到max范围内的随机字节
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(min: Byte, max: Byte): Byte = nextInt(min.toInt(), max.toInt() + 1).toByte()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定范围内的无符号随机字节
|
|
||||||
* @param min 最小值(包含)
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在min到max范围内的无符号随机字节
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(min: UByte, max: UByte): UByte = nextInt(min.toInt(), max.toInt() + 1).toUByte()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定IntRange范围内的随机字节
|
|
||||||
* @param range 指定的整数范围
|
|
||||||
* @return 在范围内转换为字节的随机值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(range: IntRange): Byte = nextByte(range.first.toByte(), range.last.toByte())
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定UIntRange范围内的无符号随机字节
|
|
||||||
* @param range 指定的无符号整数范围
|
|
||||||
* @return 在范围内转换为无符号字节的随机值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextByte(range: UIntRange): UByte = nextByte(range.first.toUByte(), range.last.toUByte())
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成随机整数
|
|
||||||
* @return 随机整数值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextInt(): Int = secureRandom.nextInt()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成小于指定最大值的随机整数
|
|
||||||
* @param max 最大值(不包含)
|
|
||||||
* @return 0到max-1范围内的随机整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextInt(max: Int): Int = secureRandom.nextInt(max)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定范围内的随机整数
|
|
||||||
* @param min 最小值(包含)
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在min到max范围内的随机整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextInt(min: Int, max: Int): Int = random.nextInt(min..max)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定IntRange范围内的随机整数
|
|
||||||
* @param range 指定的整数范围
|
|
||||||
* @return 在范围内的随机整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextInt(range: IntRange): Int = random.nextInt(range)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成随机短整数
|
|
||||||
* @return 在Short范围内的随机短整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextShort(): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成小于指定最大值的随机短整数
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在Short.MIN_VALUE到max范围内的随机短整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextShort(max: Short): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), max.toInt()).toShort()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定范围内的随机短整数
|
|
||||||
* @param min 最小值(包含)
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在min到max范围内的随机短整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextShort(min: Short, max: Short): Short = secureRandom.nextInt(min.toInt(), max.toInt()).toShort()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定IntRange范围内的随机短整数
|
|
||||||
* @param range 指定的整数范围
|
|
||||||
* @return 在范围内转换为短整数的随机值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextShort(range: IntRange): Short = secureRandom.nextInt(range.first, range.last).toShort()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成随机长整数
|
|
||||||
* @return 随机长整数值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextLong(): Long = secureRandom.nextLong()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成小于指定最大值的随机长整数
|
|
||||||
* @param max 最大值(不包含)
|
|
||||||
* @return 0到max-1范围内的随机长整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextLong(max: Long): Long = secureRandom.nextLong(max)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定范围内的随机长整数
|
|
||||||
* @param min 最小值(包含)
|
|
||||||
* @param max 最大值(包含)
|
|
||||||
* @return 在min到max范围内的随机长整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextLong(min: Long, max: Long): Long = random.nextLong(min..max)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成指定LongRange范围内的随机长整数
|
|
||||||
* @param range 指定的长整数范围
|
|
||||||
* @return 在范围内的随机长整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextLong(range: LongRange): Long = random.nextLong(range)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成随机布尔值
|
|
||||||
* @return 随机true或false
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun nextBoolean(): Boolean = secureRandom.nextBoolean()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* String类型的扩展函数 - 从字符串中随机选择字符生成指定长度的新字符串
|
|
||||||
* @param length 要生成的字符串长度
|
|
||||||
* @return 由原字符串中随机字符组成的新字符串
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun String.randomString(length: Int): String = String(CharArray(length) {
|
|
||||||
this[nextInt(indices)]
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成包含数字和字母的随机字符串
|
|
||||||
* @param length 要生成的字符串长度
|
|
||||||
* @return 包含0-9、a-z、A-Z字符的随机字符串
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun randomString(length: Int): String =
|
|
||||||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".randomString(length)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array类型的扩展函数 - 从数组中随机选择一个元素
|
|
||||||
* @param T 数组元素类型
|
|
||||||
* @return 数组中的随机元素
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun <T> Array<T>.randomItem(): T = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ByteArray类型的扩展函数 - 从字节数组中随机选择一个元素
|
|
||||||
* @return 字节数组中的随机字节
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun ByteArray.randomItem(): Byte = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IntArray类型的扩展函数 - 从整数数组中随机选择一个元素
|
|
||||||
* @return 整数数组中的随机整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun IntArray.randomItem(): Int = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* LongArray类型的扩展函数 - 从长整数数组中随机选择一个元素
|
|
||||||
* @return 长整数数组中的随机长整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun LongArray.randomItem(): Long = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FloatArray类型的扩展函数 - 从浮点数数组中随机选择一个元素
|
|
||||||
* @return 浮点数数组中的随机浮点数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun FloatArray.randomItem(): Float = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* DoubleArray类型的扩展函数 - 从双精度浮点数数组中随机选择一个元素
|
|
||||||
* @return 双精度浮点数数组中的随机双精度浮点数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun DoubleArray.randomItem(): Double = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BooleanArray类型的扩展函数 - 从布尔数组中随机选择一个元素
|
|
||||||
* @return 布尔数组中的随机布尔值
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun BooleanArray.randomItem(): Boolean = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* CharArray类型的扩展函数 - 从字符数组中随机选择一个元素
|
|
||||||
* @return 字符数组中的随机字符
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun CharArray.randomItem(): Char = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ShortArray类型的扩展函数 - 从短整数数组中随机选择一个元素
|
|
||||||
* @return 短整数数组中的随机短整数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun ShortArray.randomItem(): Short = this[nextInt(indices)]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Collection类型的扩展函数 - 从集合中随机选择一个元素
|
|
||||||
* @param T 集合元素类型
|
|
||||||
* @return 集合中的随机元素
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun <T> Collection<T>.randomItem(): T = this.elementAt(nextInt(indices))
|
|
||||||
}
|
|
||||||
@@ -1,229 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 Require.kt
|
|
||||||
* LastUpdate 2026-02-24 09:03:42
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.mingliqiye.utils.require
|
|
||||||
|
|
||||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import java.lang.reflect.Constructor
|
|
||||||
import kotlin.contracts.ExperimentalContracts
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件检查工具对象,提供多种重载的require方法用于参数验证和异常抛出
|
|
||||||
* 支持自定义异常类型、延迟消息构造和日志记录功能
|
|
||||||
*/
|
|
||||||
object Require {
|
|
||||||
@JvmStatic
|
|
||||||
var logger: Logger? = MingLiLoggerFactory.getLogger<Require>()
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
var isLogError = true
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查条件是否满足,如果不满足则抛出指定类型的异常
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param throwable 异常类型Class对象
|
|
||||||
* @param layzMessage 延迟消息构造器
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
fun require(value: Boolean, throwable: Class<out Throwable>, layzMessage: RequireLayzMessageConstructor) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, layzMessage.call(), throwable)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查条件是否满足,如果不满足则抛出指定类型的异常
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param throwable 异常类型KClass对象
|
|
||||||
* @param layzMessage 延迟消息构造器
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
fun require(value: Boolean, throwable: KClass<out Throwable>, layzMessage: RequireLayzMessageConstructor) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, throwable.java, layzMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查条件是否满足,如果不满足则抛出延迟构造的异常
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param layzThrowable 延迟异常构造器
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
fun require(value: Boolean, layzThrowable: RequireLayzExceptionConstructor) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
if (!value) throwThrowable(layzThrowable.call())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查条件是否满足,如果不满足则抛出指定消息和类型的异常
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param message 异常消息
|
|
||||||
* @param throwable 异常类型Class对象
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
fun require(value: Boolean, message: String, throwable: Class<out Throwable>) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
if (!value) throwThrowable(getExceptionConstructor(throwable).newInstance(message))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查条件是否满足,如果不满足则抛出指定消息和类型的异常
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param message 异常消息
|
|
||||||
* @param throwable 异常类型KClass对象
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
fun require(value: Boolean, message: String, throwable: KClass<out Throwable>) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, message, throwable.java)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 抛出异常并在启用日志时记录错误信息
|
|
||||||
* @param throwable 要抛出的异常对象
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
fun throwThrowable(throwable: Throwable) {
|
|
||||||
if (isLogError && logger != null) logger!!.error(throwable.message, throwable)
|
|
||||||
throw throwable
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内联函数版本的条件检查,支持泛型异常类型
|
|
||||||
* @param T 异常类型
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param message 异常消息
|
|
||||||
*/
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
@JvmName("__inline_Require")
|
|
||||||
@JvmStatic
|
|
||||||
inline fun <reified T : Throwable> require(value: Boolean, message: String) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, message, T::class.java)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内联函数版本的条件检查,支持泛型异常类型和延迟消息构造
|
|
||||||
* @param T 异常类型
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param layzMessage 延迟消息构造器
|
|
||||||
*/
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
@JvmName("__inline_Require")
|
|
||||||
@JvmStatic
|
|
||||||
inline fun <reified T : Throwable> require(value: Boolean, layzMessage: RequireLayzMessageConstructor) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
if (!value) throwThrowable(getExceptionConstructor<T>().newInstance(layzMessage.call()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定异常类型的构造函数(内联泛型版本)
|
|
||||||
* @param T 异常类型
|
|
||||||
* @return 异常类型的构造函数
|
|
||||||
*/
|
|
||||||
@JvmName("__inline_GetExceptionConstructor")
|
|
||||||
@JvmStatic
|
|
||||||
inline fun <reified T : Throwable> getExceptionConstructor(): Constructor<out T> =
|
|
||||||
getExceptionConstructor(T::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定异常类型的构造函数
|
|
||||||
* @param throwable 异常类型Class对象
|
|
||||||
* @return 异常类型的构造函数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@JvmName("__GetExceptionConstructor")
|
|
||||||
fun <T : Throwable> getExceptionConstructor(throwable: Class<out T>): Constructor<out T> =
|
|
||||||
throwable.getConstructor(String::class.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定异常类型的构造函数
|
|
||||||
* @param throwable 异常类型KClass对象
|
|
||||||
* @return 异常类型的构造函数
|
|
||||||
*/
|
|
||||||
@JvmStatic
|
|
||||||
@JvmName("__GetExceptionConstructor")
|
|
||||||
fun <T : Throwable> getExceptionConstructor(throwable: KClass<out T>): Constructor<out T> =
|
|
||||||
getExceptionConstructor(throwable.java)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 基础条件检查方法,不满足条件时抛出IllegalArgumentException
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
*/
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
@JvmStatic
|
|
||||||
fun require(value: Boolean) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, "the require conditions are not met.")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用延迟消息构造器进行条件检查,不满足时抛出IllegalArgumentException
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param layzMessage 延迟消息构造器
|
|
||||||
*/
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
@JvmStatic
|
|
||||||
fun requireLayzMessage(value: Boolean, layzMessage: RequireLayzMessageConstructor) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
require(value, IllegalArgumentException::class.java, layzMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 带消息的条件检查方法,不满足条件时抛出IllegalArgumentException
|
|
||||||
* @param value 要检查的布尔条件
|
|
||||||
* @param message 异常消息
|
|
||||||
*/
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
@JvmStatic
|
|
||||||
fun require(value: Boolean, message: String) {
|
|
||||||
contract {
|
|
||||||
returns() implies value
|
|
||||||
}
|
|
||||||
if (!value) throwThrowable(getExceptionConstructor(IllegalArgumentException::class.java).newInstance(message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,225 @@
|
|||||||
|
/*
|
||||||
|
* 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 RequireTrue.kt
|
||||||
|
* LastUpdate 2026-10-20 21:07:55
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.require
|
||||||
|
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
const val DEFAULT_MESSAGE = "required fail"
|
||||||
|
|
||||||
|
class Required<T : Throwable>(private val newException: (message: String) -> T) {
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean, message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value) { message }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value) { DEFAULT_MESSAGE }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean, lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
if (!value) {
|
||||||
|
throw newException(lazyMessage())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Throwable> newRequired(clazz: Class<T>): Required<T> {
|
||||||
|
return Required {
|
||||||
|
newException(clazz, it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun newRequired(): Required<IllegalArgumentException> {
|
||||||
|
return newRequired<IllegalArgumentException>()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmName("newRequiredInline")
|
||||||
|
inline fun <reified T : Throwable> newRequired(): Required<T> {
|
||||||
|
return newRequired(T::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Throwable> newException(clazz: Class<T>, msg: String): T = try {
|
||||||
|
val constructor = clazz.getConstructor(String::class.java).also {
|
||||||
|
check(!java.lang.reflect.Modifier.isAbstract(clazz.modifiers)) {
|
||||||
|
"${clazz.name} 是抽象类,无法实例化"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
constructor.newInstance(msg)
|
||||||
|
} catch (e: ReflectiveOperationException) {
|
||||||
|
throw IllegalStateException("无法实例化 ${clazz.name}", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueDef")
|
||||||
|
fun <T : Throwable> requireTrue(value: Boolean, clazz: Class<T>, lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
if (!value) {
|
||||||
|
throw newException(clazz, lazyMessage())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueInline")
|
||||||
|
inline fun <reified T : Throwable> requireTrue(value: Boolean, noinline lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, T::class.java, lazyMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
fun <T : Throwable> Boolean.requireTrue(clazz: Class<T>, lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, clazz, lazyMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
fun <T : Throwable> Boolean.requireTrue(clazz: Class<T>, message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, clazz) {
|
||||||
|
message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun <T : Throwable> requireTrue(value: Boolean, clazz: Class<T>, message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, clazz) { message }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueInline")
|
||||||
|
inline fun <reified T : Throwable> requireTrue(value: Boolean, message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, T::class.java) { message }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
inline fun <reified T : Throwable> Boolean.requireTrue(noinline lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, T::class.java, lazyMessage)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
inline fun <reified T : Throwable> Boolean.requireTrue(message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, T::class.java, message)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun <T : Throwable> requireTrue(value: Boolean, clazz: Class<T>) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, clazz) { DEFAULT_MESSAGE }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueInline")
|
||||||
|
inline fun <reified T : Throwable> requireTrue(value: Boolean) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, T::class.java) { DEFAULT_MESSAGE }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, IllegalArgumentException::class.java) { DEFAULT_MESSAGE }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean, message: String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, IllegalArgumentException::class.java) { message }
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
fun requireTrue(value: Boolean, lazyMessage: () -> String) {
|
||||||
|
contract {
|
||||||
|
returns() implies value
|
||||||
|
}
|
||||||
|
requireTrue(value, IllegalArgumentException::class.java, lazyMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
fun <T : Throwable> Boolean.requireTrue(clazz: Class<T>) {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, clazz) { DEFAULT_MESSAGE }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
@JvmName("requireTrueOnBoolean")
|
||||||
|
inline fun <reified T : Throwable> Boolean.requireTrue() {
|
||||||
|
contract {
|
||||||
|
returns() implies this@requireTrue
|
||||||
|
}
|
||||||
|
requireTrue(this, T::class.java) { DEFAULT_MESSAGE }
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* 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 Required.kt
|
||||||
|
* LastUpdate 2026-10-20 19:58:06
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.require
|
||||||
@@ -50,5 +50,3 @@ fun encryptAesGcmNoPadding(src: ByteArray, key: String, iv: ByteArray): ByteArra
|
|||||||
fun encryptAesGcmNoPadding(src: String, key: String, iv: ByteArray): ByteArray {
|
fun encryptAesGcmNoPadding(src: String, key: String, iv: ByteArray): ByteArray {
|
||||||
return encryptAesGcmNoPadding(src.toByteArray(), key.toByteArray(), iv)
|
return encryptAesGcmNoPadding(src.toByteArray(), key.toByteArray(), iv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -24,5 +24,5 @@ package com.mingliqiye.utils.springboot.annotation
|
|||||||
|
|
||||||
@Target(AnnotationTarget.FUNCTION)
|
@Target(AnnotationTarget.FUNCTION)
|
||||||
annotation class ApplicationDestroyedRunner(
|
annotation class ApplicationDestroyedRunner(
|
||||||
val order: Int = Int.MIN_VALUE
|
val order: Int = Int.MIN_VALUE,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -28,5 +28,5 @@ import org.springframework.stereotype.Component
|
|||||||
@Component
|
@Component
|
||||||
annotation class Listener(
|
annotation class Listener(
|
||||||
@get:AliasFor(annotation = Component::class)
|
@get:AliasFor(annotation = Component::class)
|
||||||
val value: String = ""
|
val value: String = "",
|
||||||
)
|
)
|
||||||
|
|||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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 JSONAAutoConfiguration.kt
|
||||||
|
* LastUpdate 2026-11-09 14:59:49
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure
|
||||||
|
|
||||||
|
import com.mingliqiye.utils.json.api.JSONA
|
||||||
|
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||||
|
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
|
||||||
|
|
||||||
|
|
||||||
|
@AutoConfiguration
|
||||||
|
@AutoConfigureAfter(name = ["com.mingliqiye.utils.springboot.autoconfigure.JacksonJsonApiAutoConfiguration", "com.mingliqiye.utils.springboot.autoconfigure.Jackson3JsonApiAutoConfiguration"])
|
||||||
|
@ConditionalOnBean(JsonApi::class)
|
||||||
|
open class JSONAAutoConfiguration(
|
||||||
|
jsonApi: JsonApi,
|
||||||
|
) {
|
||||||
|
val log = MingLiLoggerFactory.getLogger<JSONAAutoConfiguration>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
JSONA.setJsonApi(jsonApi)
|
||||||
|
log.info(
|
||||||
|
"JSONA use ${
|
||||||
|
jsonApi.javaClass
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
-13
@@ -16,16 +16,17 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Jackson3AutoConfiguration.kt
|
* CurrentFile Jackson3AutoConfiguration.kt
|
||||||
* LastUpdate 2026-10-02 16:11:12
|
* LastUpdate 2026-11-09 14:18:45
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||||
|
|
||||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
|
|
||||||
import com.mingliqiye.utils.json.converters.UUIDJsonConverter
|
|
||||||
import com.mingliqiye.utils.json.converters.base.getJackson3Module
|
import com.mingliqiye.utils.json.converters.base.getJackson3Module
|
||||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.JacksonBuilder
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.JacksonJsonBuilderImpl
|
||||||
|
import org.springframework.beans.factory.ObjectProvider
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
@@ -36,17 +37,27 @@ import org.springframework.context.annotation.Bean
|
|||||||
@AutoConfigureAfter(name = ["org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration"])
|
@AutoConfigureAfter(name = ["org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration"])
|
||||||
open class Jackson3AutoConfiguration {
|
open class Jackson3AutoConfiguration {
|
||||||
|
|
||||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3AutoConfiguration>()
|
private val log = MingLiLoggerFactory.getLogger<Jackson3AutoConfiguration>()
|
||||||
|
|
||||||
@Bean
|
@Bean("UUIDJsonConverterJsonMapperCustomizer")
|
||||||
@ConditionalOnMissingBean(JsonMapperBuilderCustomizer::class)
|
@ConditionalOnMissingBean(name = ["UUIDJsonConverterJsonMapperCustomizer"])
|
||||||
open fun myJsonMapperCustomizer(): JsonMapperBuilderCustomizer {
|
fun jsonMapperBuilderCustomizer(builders: ObjectProvider<JacksonBuilder>): JsonMapperBuilderCustomizer {
|
||||||
return JsonMapperBuilderCustomizer { builder ->
|
val builderList = builders.orderedStream().toList()
|
||||||
builder
|
var jacksonJsonBuilderImpl = JacksonJsonBuilderImpl()
|
||||||
.addModule(UUIDJsonConverter.getJsonConverter().getJackson3Module())
|
log.info("scan ${builderList.size} JacksonBuilders")
|
||||||
.addModule(DateTimeJsonConverter.getJsonConverter().getJackson3Module())
|
builderList.forEach {
|
||||||
}.also {
|
jacksonJsonBuilderImpl = it.customize(jacksonJsonBuilderImpl) as JacksonJsonBuilderImpl
|
||||||
logger.info("MingliUtils Jackson3 Serializers created")
|
|
||||||
}
|
}
|
||||||
|
return JsonMapperBuilderCustomizer {
|
||||||
|
it.addModules(
|
||||||
|
*jacksonJsonBuilderImpl.getData().map { converter ->
|
||||||
|
converter.getJackson3Module().also { module ->
|
||||||
|
log.info("Jackson3 Module Install ${module.moduleName}")
|
||||||
}
|
}
|
||||||
|
}.toTypedArray()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-16
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Jackson3JsonApiAutoConfiguration.kt
|
* CurrentFile Jackson3JsonApiAutoConfiguration.kt
|
||||||
* LastUpdate 2026-10-02 16:11:02
|
* LastUpdate 2026-11-09 13:45:29
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -26,33 +26,51 @@ import com.mingliqiye.utils.exception.JsonException
|
|||||||
import com.mingliqiye.utils.json.api.JSONA
|
import com.mingliqiye.utils.json.api.JSONA
|
||||||
import com.mingliqiye.utils.json.api.Jackson3JsonApi
|
import com.mingliqiye.utils.json.api.Jackson3JsonApi
|
||||||
import com.mingliqiye.utils.json.api.base.JsonApi
|
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Primary
|
||||||
import tools.jackson.databind.json.JsonMapper
|
import tools.jackson.databind.json.JsonMapper
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于 Jackson 3 的 [JsonApi] 自动配置。
|
||||||
|
*
|
||||||
|
* 在类路径存在 Jackson 3(`tools.jackson.databind.json.JsonMapper`)
|
||||||
|
* 及 Spring Boot 的 Jackson 自动配置时生效,将容器中的 [JsonMapper]
|
||||||
|
* 包装为 [Jackson3JsonApi] 并以 [JsonApi] 类型注册为 Bean,
|
||||||
|
* 同时尝试初始化全局单例 [JSONA]。
|
||||||
|
*
|
||||||
|
* ## 生效顺序
|
||||||
|
*
|
||||||
|
* 本类依赖 [Jackson3AutoConfiguration] 注册的 `JsonMapper` 构建定制器,
|
||||||
|
* 通过 `@AutoConfigureAfter` 保证在其之后处理,确保 `JsonMapper` 已按
|
||||||
|
* 自定义模块完成配置。
|
||||||
|
*
|
||||||
|
* ## 覆盖方式
|
||||||
|
*
|
||||||
|
* 若调用方需要自定义 [JsonApi] 实现,只需自行声明一个 [JsonApi] Bean,
|
||||||
|
* 本配置的 `@ConditionalOnMissingBean(JsonApi::class)` 将自动跳过创建。
|
||||||
|
*
|
||||||
|
* ## 全局单例
|
||||||
|
*
|
||||||
|
* [JSONA] 是进程级全局 JSON 访问入口。本配置在创建 Bean 后尝试读取
|
||||||
|
* [JSONA] 当前实例;若尚未设置(读取抛 [NullPointerException]),
|
||||||
|
* 则将本次创建的 [JsonApi] 设置为其全局实现。若调用方已在更早阶段
|
||||||
|
* 设置过 [JSONA],则保留既有实现,不覆盖。
|
||||||
|
*
|
||||||
|
* @see Jackson3AutoConfiguration
|
||||||
|
* @see Jackson3JsonApi
|
||||||
|
*/
|
||||||
@ConditionalOnClass(name = ["tools.jackson.databind.json.JsonMapper", "org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration"])
|
@ConditionalOnClass(name = ["tools.jackson.databind.json.JsonMapper", "org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration"])
|
||||||
@AutoConfigureAfter(name = ["com.mingliqiye.utils.springboot.autoconfigure.Jackson3AutoConfiguration"])
|
@AutoConfigureAfter(name = ["com.mingliqiye.utils.springboot.autoconfigure.Jackson3AutoConfiguration"])
|
||||||
open class Jackson3JsonApiAutoConfiguration {
|
open class Jackson3JsonApiAutoConfiguration {
|
||||||
private val logger = MingLiLoggerFactory.getLogger<Jackson3JsonApiAutoConfiguration>()
|
@Bean("jackson3JsonApi")
|
||||||
|
@Primary
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(JsonApi::class)
|
@ConditionalOnMissingBean(JsonApi::class)
|
||||||
open fun jsonApi(jsonMapper: JsonMapper): JsonApi {
|
open fun jsonApi(jsonMapper: JsonMapper): JsonApi {
|
||||||
return Jackson3JsonApi(
|
return Jackson3JsonApi(
|
||||||
get = { jsonMapper },
|
get = { jsonMapper },
|
||||||
set = { throw JsonException("JSON API set method is not allowed.") }).also {
|
set = { throw JsonException("JSON API set method is not allowed.") })
|
||||||
logger.info("JsonApi Bean Auto Configuration Success")
|
|
||||||
try {
|
|
||||||
JSONA.getJsonApi()
|
|
||||||
} catch (_: NullPointerException) {
|
|
||||||
JSONA.setJsonApi(it)
|
|
||||||
logger.info(
|
|
||||||
"JSONA Using {}", it.javaClass.name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-108
@@ -1,108 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 JacksonAutoConfiguration.kt
|
|
||||||
* LastUpdate 2026-10-02 16:10:53
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.mingliqiye.utils.springboot.autoconfigure
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
|
||||||
import com.mingliqiye.utils.json.api.JSONA
|
|
||||||
import com.mingliqiye.utils.json.api.JacksonJsonApi
|
|
||||||
import com.mingliqiye.utils.json.api.base.JsonApi
|
|
||||||
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
|
|
||||||
import com.mingliqiye.utils.json.converters.UUIDJsonConverter
|
|
||||||
import com.mingliqiye.utils.json.converters.base.addJsonConverter
|
|
||||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
|
||||||
import org.springframework.context.annotation.Bean
|
|
||||||
import org.springframework.context.annotation.Primary
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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 JacksonAutoConfiguration.kt
|
|
||||||
* LastUpdate 2026-02-05 10:39:54
|
|
||||||
* UpdateUser MingLiPro
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* JacksonAutoConfiguration 是一个 Spring Boot 自动配置类,用于配置 Jackson 相关的序列化和反序列化功能。
|
|
||||||
* 该类在检测到 ObjectMapper 和 Spring Boot 的 JacksonAutoConfiguration 存在时自动生效,
|
|
||||||
* 并注册自定义的 JSON 转换器模块(如 UUID 和 DateTime 转换器)。
|
|
||||||
*
|
|
||||||
* @param objectMapper 用于 JSON 序列化和反序列化的 ObjectMapper 实例。
|
|
||||||
*/
|
|
||||||
@ConditionalOnClass(name = ["com.fasterxml.jackson.databind.ObjectMapper", "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
|
||||||
@AutoConfigureAfter(name = ["org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
|
||||||
open class JacksonAutoConfiguration(objectMapper: ObjectMapper) {
|
|
||||||
private val log: Logger = MingLiLoggerFactory.getLogger("MingliUtils-JacksonAutoConfiguration")
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 初始化块:在类实例化时执行。
|
|
||||||
* 注册自定义的 UUID 和 DateTime JSON 转换器模块到 ObjectMapper 中。
|
|
||||||
*/
|
|
||||||
init {
|
|
||||||
objectMapper
|
|
||||||
.addJsonConverter<UUIDJsonConverter>()
|
|
||||||
.addJsonConverter<DateTimeJsonConverter>()
|
|
||||||
log.info("MingliUtils Jackson Serializers created")
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 创建并返回一个 JsonApi Bean 实例。
|
|
||||||
* 该方法会在没有其他 JsonApi Bean 存在时被调用,并将创建的 JacksonJsonApi 实例设置为全局默认的 JSON API。
|
|
||||||
*
|
|
||||||
* @param objectMapper 用于 JSON 操作的 ObjectMapper 实例。
|
|
||||||
* @return 返回配置好的 JsonApi 实例。
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@ConditionalOnMissingBean(JsonApi::class)
|
|
||||||
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
|
|
||||||
return JacksonJsonApi(objectMapper).also {
|
|
||||||
log.info("JsonApi Bean Auto Configuration Success")
|
|
||||||
try {
|
|
||||||
JSONA.getJsonApi()
|
|
||||||
} catch (_: NullPointerException) {
|
|
||||||
JSONA.setJsonApi(it)
|
|
||||||
log.info(
|
|
||||||
"JSONA Using {}", it.javaClass.name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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 JacksonJsonApiAutoConfiguration.kt
|
||||||
|
* LastUpdate 2026-11-09 13:45:41
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.mingliqiye.utils.json.api.JacksonJsonApi
|
||||||
|
import com.mingliqiye.utils.json.api.base.JsonApi
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
|
||||||
|
/*
|
||||||
|
* JacksonAutoConfiguration 是一个 Spring Boot 自动配置类,用于配置 Jackson 相关的序列化和反序列化功能。
|
||||||
|
* 该类在检测到 ObjectMapper 和 Spring Boot 的 JacksonAutoConfiguration 存在时自动生效,
|
||||||
|
* 并注册自定义的 JSON 转换器模块(如 UUID 和 DateTime 转换器)。
|
||||||
|
*
|
||||||
|
* @param objectMapper 用于 JSON 序列化和反序列化的 ObjectMapper 实例。
|
||||||
|
*/
|
||||||
|
@ConditionalOnClass(name = ["com.fasterxml.jackson.databind.ObjectMapper", "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
||||||
|
@AutoConfigureAfter(name = ["com.mingliqiye.utils.springboot.autoconfigure.JacksonJsonAutoConfiguration"])
|
||||||
|
open class JacksonJsonApiAutoConfiguration {
|
||||||
|
@Bean("jacksonJsonApi")
|
||||||
|
@ConditionalOnMissingBean(JsonApi::class)
|
||||||
|
open fun jsonApi(objectMapper: ObjectMapper): JsonApi {
|
||||||
|
return JacksonJsonApi(objectMapper)
|
||||||
|
}
|
||||||
|
}
|
||||||
+89
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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 JacksonJsonAutoConfiguration.kt
|
||||||
|
* LastUpdate 2026-11-09 14:19:05
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure
|
||||||
|
|
||||||
|
import com.mingliqiye.utils.json.converters.base.getJacksonModule
|
||||||
|
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.JacksonBuilder
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.JacksonJsonBuilderImpl
|
||||||
|
import org.springframework.beans.factory.ObjectProvider
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||||
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson 2 的序列化模块自动配置(过渡兼容)。
|
||||||
|
*
|
||||||
|
* 在类路径存在 Jackson 2(`com.fasterxml.jackson.databind.ObjectMapper`)
|
||||||
|
* 及对应 Spring Boot 自动配置时生效,向容器注册用于追加 UUID / 日期时间
|
||||||
|
* 模块的 [Jackson2ObjectMapperBuilderCustomizer]。
|
||||||
|
*
|
||||||
|
* ## 定位说明
|
||||||
|
*
|
||||||
|
* Spring Boot 4 起默认 JSON 库为 Jackson 3,Jackson 2 的自动配置已被弃用,
|
||||||
|
* 仅用于迁移过渡,并计划在未来版本移除。本类只服务于仍在使用 Jackson 2 的场景,
|
||||||
|
* 新项目应优先使用 Jackson 3 的对应配置。
|
||||||
|
*
|
||||||
|
* ## 类名注意
|
||||||
|
*
|
||||||
|
* 条件与排序中引用的自动配置类名需与目标 Spring Boot 版本一致。
|
||||||
|
* Spring Boot 4 中 Jackson 2 的自动配置类位于 `spring-boot-jackson2` 模块
|
||||||
|
* (包路径形如 `org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration`)。
|
||||||
|
* 若项目中引用的类名不存在,本配置会被整体跳过而不报错,需自行核对。
|
||||||
|
*
|
||||||
|
* ## Bean 命名
|
||||||
|
*
|
||||||
|
* 与 Jackson 3 配置中的定制器 Bean 名称刻意区分(后缀 `2`),
|
||||||
|
* 避免两套配置同时生效时发生名称冲突。
|
||||||
|
*
|
||||||
|
* @see Jackson2ObjectMapperBuilderCustomizer
|
||||||
|
*/
|
||||||
|
@ConditionalOnClass(name = ["com.fasterxml.jackson.databind.ObjectMapper", "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
||||||
|
@AutoConfigureAfter(name = ["org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"])
|
||||||
|
open class JacksonJsonAutoConfiguration {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val log = MingLiLoggerFactory.getLogger<JacksonJsonAutoConfiguration>()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("mingliUtilsJacksonObjectMapperBuilderCustomizer")
|
||||||
|
fun jackson2ObjectMapperBuilderCustomizer(builders: ObjectProvider<JacksonBuilder>): Jackson2ObjectMapperBuilderCustomizer {
|
||||||
|
val builderList = builders.orderedStream().toList()
|
||||||
|
var jacksonJsonBuilderImpl = JacksonJsonBuilderImpl()
|
||||||
|
builderList.forEach {
|
||||||
|
jacksonJsonBuilderImpl = it.customize(jacksonJsonBuilderImpl) as JacksonJsonBuilderImpl
|
||||||
|
}
|
||||||
|
return Jackson2ObjectMapperBuilderCustomizer { builder ->
|
||||||
|
builder.modulesToInstall(
|
||||||
|
*jacksonJsonBuilderImpl.getData().map { converter ->
|
||||||
|
converter.getJacksonModule().also { module ->
|
||||||
|
log.info("Jackson Module Install ${module.moduleName}")
|
||||||
|
}
|
||||||
|
}.toTypedArray()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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 JsonAutoConfiguration.kt
|
||||||
|
* LastUpdate 2026-11-09 14:44:55
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure
|
||||||
|
|
||||||
|
import com.mingliqiye.utils.json.converters.DateTimeJsonConverter
|
||||||
|
import com.mingliqiye.utils.json.converters.NetworkEndpointJsonConverter
|
||||||
|
import com.mingliqiye.utils.json.converters.UUIDJsonConverter
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.JacksonBuilder
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
|
||||||
|
@AutoConfiguration
|
||||||
|
open class JsonAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean("mingliUtilsJacksonBuilder")
|
||||||
|
fun jacksonBuilder(): JacksonBuilder = JacksonBuilder {
|
||||||
|
it.apply {
|
||||||
|
addJsonConverters(
|
||||||
|
UUIDJsonConverter.getJsonConverter(),
|
||||||
|
DateTimeJsonConverter.getJsonConverter(),
|
||||||
|
NetworkEndpointJsonConverter.getJsonConverter(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+74
-25
@@ -13,18 +13,19 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* ProjectName mingli-utils
|
* ProjectName FastBaseMapper.kt
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile Okhttp3AutoConfiguration.kt
|
* CurrentFile Okhttp3AutoConfiguration.kt
|
||||||
* LastUpdate 2026-10-02 16:10:15
|
* LastUpdate 2026-10-26 23:22:18
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.mingliqiye.utils.springboot.autoconfigure
|
package com.mingliqiye.utils.springboot.autoconfigure
|
||||||
|
|
||||||
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
import com.mingliqiye.utils.logger.MingLiLoggerFactory
|
||||||
|
import com.mingliqiye.utils.springboot.autoconfigure.builder.OkhttpBuilder
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.ObjectProvider
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration
|
import org.springframework.boot.autoconfigure.AutoConfiguration
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
@@ -32,43 +33,91 @@ import org.springframework.context.annotation.Bean
|
|||||||
import org.springframework.context.event.ContextClosedEvent
|
import org.springframework.context.event.ContextClosedEvent
|
||||||
import org.springframework.context.event.EventListener
|
import org.springframework.context.event.EventListener
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OkHttp 3 的 Spring Boot 自动配置。
|
||||||
|
*
|
||||||
|
* 在类路径存在 `okhttp3.OkHttpClient` 时生效,向容器注册一个默认的
|
||||||
|
* [okhttp3.OkHttpClient] Bean。构建过程中会依次应用容器中所有
|
||||||
|
* [OkhttpBuilder] 定制器(按 `@Order` / [org.springframework.core.Ordered] 排序),
|
||||||
|
* 便于调用方追加拦截器、超时、代理、连接池等配置,而无需自行创建客户端。
|
||||||
|
*
|
||||||
|
* ## 覆盖方式
|
||||||
|
*
|
||||||
|
* 若调用方需要完全自定义客户端,只需自行声明一个 [okhttp3.OkHttpClient] Bean,
|
||||||
|
* 本配置的 `@ConditionalOnMissingBean` 将自动跳过创建,不会产生多余实例。
|
||||||
|
*
|
||||||
|
* 若只想在默认客户端上做增量调整,实现 [OkhttpBuilder] 并注册为 Bean 即可,
|
||||||
|
* 无需替换整个客户端。
|
||||||
|
*
|
||||||
|
* ## 生命周期
|
||||||
|
*
|
||||||
|
* 由本配置创建的客户端会在 Spring 上下文关闭时([ContextClosedEvent])
|
||||||
|
* 主动清理连接池并关闭分发器线程池,避免线程与连接泄漏。
|
||||||
|
* 调用方自行声明的客户端不会被本配置清理,其生命周期由调用方负责。
|
||||||
|
*
|
||||||
|
* @see OkhttpBuilder
|
||||||
|
*/
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
@ConditionalOnClass(name = ["okhttp3.OkHttpClient"])
|
@ConditionalOnClass(name = ["okhttp3.OkHttpClient"])
|
||||||
open class Okhttp3AutoConfiguration {
|
open class Okhttp3AutoConfiguration {
|
||||||
|
|
||||||
private var okHttpClient: okhttp3.OkHttpClient? = null
|
companion object {
|
||||||
|
|
||||||
val log: Logger = MingLiLoggerFactory.getLogger<Okhttp3AutoConfiguration>()
|
val log: Logger = MingLiLoggerFactory.getLogger<Okhttp3AutoConfiguration>()
|
||||||
|
|
||||||
@Autowired(required = false)
|
|
||||||
open var userDefinedBuilder: okhttp3.OkHttpClient.Builder? = null
|
|
||||||
|
|
||||||
open fun initOkHttpClient() {
|
|
||||||
okHttpClient = if (userDefinedBuilder == null) {
|
|
||||||
okhttp3.OkHttpClient()
|
|
||||||
} else {
|
|
||||||
userDefinedBuilder!!.build()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 由本配置创建并注册的 [okhttp3.OkHttpClient] 实例,供上下文关闭时清理使用。
|
||||||
|
*
|
||||||
|
* 当调用方自行声明了 `OkHttpClient` Bean 时,本配置的 Bean 方法不会执行,
|
||||||
|
* 该字段保持 `null`,关闭时的清理逻辑也会相应跳过。
|
||||||
|
*/
|
||||||
|
var okhttp: okhttp3.OkHttpClient? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建默认的 [okhttp3.OkHttpClient] Bean。
|
||||||
|
*
|
||||||
|
* 依次应用容器中所有 [OkhttpBuilder] 定制器后构建客户端。没有任何定制器时
|
||||||
|
* 也能正常构建([ObjectProvider] 允许零个元素)。
|
||||||
|
*
|
||||||
|
* 该 Bean 仅在容器中不存在其他 `OkHttpClient` 时创建;若调用方已自行声明,
|
||||||
|
* 本方法不会被调用。
|
||||||
|
*
|
||||||
|
* @param builders 容器中的 OkHttp 构建定制器,按 `@Order` 排序后依次应用
|
||||||
|
* @return 构建完成的 OkHttp 客户端
|
||||||
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
open fun okHttpClient(): okhttp3.OkHttpClient {
|
open fun okHttpClient(builders: ObjectProvider<OkhttpBuilder>): okhttp3.OkHttpClient {
|
||||||
if (okHttpClient == null) {
|
var connt = 0
|
||||||
initOkHttpClient()
|
return okhttp3.OkHttpClient.Builder().let { builder ->
|
||||||
|
var b = builder
|
||||||
|
builders.orderedStream().forEach {
|
||||||
|
b = it.customize(b)
|
||||||
|
connt++
|
||||||
|
}
|
||||||
|
b.build()
|
||||||
|
}.also {
|
||||||
|
okhttp = it
|
||||||
|
log.info("load $connt OkhttpBuilder initialized OkHttpClient successfully")
|
||||||
}
|
}
|
||||||
log.info("OkHttpClient initialized (using ${if (userDefinedBuilder != null) "user-defined" else "default"} builder)")
|
|
||||||
return okHttpClient!!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 Spring 上下文关闭时清理本配置创建的 OkHttp 客户端。
|
||||||
|
*
|
||||||
|
* 执行两步清理:
|
||||||
|
* 1. [okhttp3.ConnectionPool.evictAll] 立即关闭连接池中的空闲连接;
|
||||||
|
* 2. 关闭 [okhttp3.Dispatcher] 的线程池,中断未完成的任务。
|
||||||
|
*
|
||||||
|
* 仅当 [okhttp] 字段非空(即客户端由本配置创建)时执行;
|
||||||
|
* 调用方自行声明的客户端不会被清理。
|
||||||
|
*/
|
||||||
@EventListener(ContextClosedEvent::class)
|
@EventListener(ContextClosedEvent::class)
|
||||||
open fun handleContextClosed() {
|
open fun handleContextClosed() {
|
||||||
if (okHttpClient != null) {
|
okhttp?.let {
|
||||||
log.info("Context closed, cleaning up OkHttpClient...")
|
log.info("Context closed, cleaning up OkHttpClient...")
|
||||||
okHttpClient?.dispatcher?.executorService?.shutdown()
|
it.connectionPool.evictAll()
|
||||||
okHttpClient?.connectionPool?.evictAll()
|
it.dispatcher.executorService.shutdownNow()
|
||||||
okHttpClient?.dispatcher?.executorService?.shutdownNow()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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 JacksonBuilder.kt
|
||||||
|
* LastUpdate 2026-11-09 14:39:28
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure.builder
|
||||||
|
|
||||||
|
import com.mingliqiye.utils.json.converters.base.JsonConverter
|
||||||
|
|
||||||
|
sealed class JacksonJsonBuilder {
|
||||||
|
abstract fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilder
|
||||||
|
abstract fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder
|
||||||
|
}
|
||||||
|
|
||||||
|
open class JacksonJsonBuilderImpl : JacksonJsonBuilder() {
|
||||||
|
override fun addJsonConverter(jsonConverter: JsonConverter<*, *>): JacksonJsonBuilderImpl {
|
||||||
|
data.add(jsonConverter)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addJsonConverters(vararg jsonConverters: JsonConverter<*, *>): JacksonJsonBuilder {
|
||||||
|
data.addAll(jsonConverters)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
private val data = mutableListOf<JsonConverter<*, *>>()
|
||||||
|
|
||||||
|
fun getData(): List<JsonConverter<*, *>> {
|
||||||
|
return data.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
fun interface JacksonBuilder {
|
||||||
|
fun customize(builder: JacksonJsonBuilder): JacksonJsonBuilder
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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 OkhttpBuilder.kt
|
||||||
|
* LastUpdate 2026-11-09 13:32:25
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.springboot.autoconfigure.builder
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
fun interface OkhttpBuilder {
|
||||||
|
fun customize(builder: OkHttpClient.Builder): OkHttpClient.Builder
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile SpringBeanUtils.kt
|
* CurrentFile SpringBeanUtils.kt
|
||||||
* LastUpdate 2026-10-02 16:18:14
|
* LastUpdate 2026-10-27 12:51:22
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -44,6 +44,12 @@ class SpringBeanUtils : ApplicationContextAware {
|
|||||||
|
|
||||||
private var applicationContextBase: ApplicationContext? = null
|
private var applicationContextBase: ApplicationContext? = null
|
||||||
|
|
||||||
|
inline fun <reified T : Any> lazyBean(): Lazy<T> = lazy(::getBean)
|
||||||
|
|
||||||
|
inline fun <reified T : Any> lazyBean(name: String): Lazy<T> = lazy {
|
||||||
|
getBean(name, T::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Throws(BeansException::class)
|
@Throws(BeansException::class)
|
||||||
inline fun <reified T : Any> getBean() = getBean(T::class.java)
|
inline fun <reified T : Any> getBean() = getBean(T::class.java)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ package com.mingliqiye.utils.springboot.listener
|
|||||||
import java.lang.reflect.Method
|
import java.lang.reflect.Method
|
||||||
|
|
||||||
data class ApplicationRunnerData(
|
data class ApplicationRunnerData(
|
||||||
val beanName: String, val order: Int, val bean: Any, val method: Method
|
val beanName: String, val order: Int, val bean: Any, val method: Method,
|
||||||
) {
|
) {
|
||||||
fun run() {
|
fun run() {
|
||||||
method.invoke(bean)
|
method.invoke(bean)
|
||||||
|
|||||||
@@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* 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 SuprtStreamUtils.kt
|
||||||
|
* LastUpdate 2026-10-11 17:32:17
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.stream
|
||||||
|
|
||||||
|
import java.util.stream.Stream
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [SuperStream] 的工厂工具类,提供把各类常见数据源(Java [Stream]、数组、集合、映射)
|
||||||
|
* 转换为 [SuperStream] 的扩展函数。
|
||||||
|
*
|
||||||
|
* 所有扩展函数均以 `toSuperStream` 命名,遵循 Kotlin 的 `toXxx` 转换命名惯例。
|
||||||
|
*
|
||||||
|
* 使用示例:
|
||||||
|
* ```kotlin
|
||||||
|
* intArrayOf(1, 2, 3).toSuperStream()
|
||||||
|
* listOf("a", "b").toSuperStream(parallel = true)
|
||||||
|
* mapOf(1 to "a").toSuperStream()
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
object SuperStreamUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 Java [Stream] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param T 流中元素的类型,必须是不可空类型。
|
||||||
|
* @return 包装当前 [Stream] 的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun <T : Any> Stream<T>.toSuperStream() = SuperStream.of(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [ByteArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun ByteArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [ShortArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun ShortArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [IntArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun IntArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [LongArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun LongArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [FloatArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun FloatArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [DoubleArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun DoubleArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [BooleanArray] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun BooleanArray.toSuperStream(parallel: Boolean = false) = SuperStream.of(this.toList(), parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象数组 [Array] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param T 数组元素类型,必须是不可空类型。
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前数组元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun <T : Any> Array<T>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [Collection] 转换为 [SuperStream]。
|
||||||
|
*
|
||||||
|
* @param T 集合元素类型,必须是不可空类型。
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前集合元素构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun <T : Any> Collection<T>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 [Map] 转换为 [SuperStream],流中元素为键值对 [Map.Entry]。
|
||||||
|
*
|
||||||
|
* @param K 键的类型。
|
||||||
|
* @param V 值的类型。
|
||||||
|
* @param parallel 是否创建并行流,默认为 `false`(串行)。
|
||||||
|
* @return 由当前映射的条目构成的 [SuperStream]。
|
||||||
|
*/
|
||||||
|
fun <K, V> Map<K, V>.toSuperStream(parallel: Boolean = false) = SuperStream.of(this, parallel)
|
||||||
|
}
|
||||||
@@ -16,444 +16,258 @@
|
|||||||
* ProjectName mingli-utils
|
* ProjectName mingli-utils
|
||||||
* ModuleName mingli-utils.main
|
* ModuleName mingli-utils.main
|
||||||
* CurrentFile SystemUtil.kt
|
* CurrentFile SystemUtil.kt
|
||||||
* LastUpdate 2026-03-27 09:35:40
|
* LastUpdate 2026-10-28 22:43:54
|
||||||
* UpdateUser MingLiPro
|
* UpdateUser MingLiPro
|
||||||
*/
|
*/
|
||||||
@file:JvmName("SystemUtils")
|
@file:JvmName("SystemUtils")
|
||||||
|
|
||||||
package com.mingliqiye.utils.system
|
package com.mingliqiye.utils.system
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory
|
|
||||||
import java.net.*
|
import java.net.*
|
||||||
|
import java.util.*
|
||||||
import com.mingliqiye.utils.random.Random.nextBytes as randomByte
|
import com.mingliqiye.utils.random.Random.nextBytes as randomByte
|
||||||
|
|
||||||
|
private val UNIX_PREFIXES = arrayOf("Linux", "AIX", "SunOS", "Mac OS X", "FreeBSD")
|
||||||
|
private val HEX_UPPER: Array<String> = Array(256) { "%02X".format(it) }
|
||||||
|
|
||||||
|
private fun <T> Enumeration<T>?.toList(): List<T> = if (this == null) emptyList() else Collections.list(this)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 操作系统名称属性,延迟初始化
|
* 操作系统名称属性,延迟初始化
|
||||||
*/
|
*/
|
||||||
val osName: String? by lazy {
|
val osName: String? by lazy {
|
||||||
System.getProperties().getProperty("os.name")
|
runCatching { System.getProperty("os.name") }.getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前操作系统是否为Windows系统
|
* 判断当前操作系统是否为 Windows
|
||||||
*
|
|
||||||
* @return 如果是Windows系统返回true,否则返回false
|
|
||||||
*/
|
*/
|
||||||
val isWindows: Boolean by lazy {
|
val isWindows: Boolean by lazy {
|
||||||
osName != null && osName!!.startsWith("Windows")
|
osName?.startsWith("Windows") == true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前操作系统是否为Mac系统
|
* 判断当前操作系统是否为 Mac
|
||||||
*
|
|
||||||
* @return 如果是Mac系统返回true,否则返回false
|
|
||||||
*/
|
*/
|
||||||
val isMac: Boolean by lazy {
|
val isMac: Boolean by lazy {
|
||||||
osName != null && osName!!.startsWith("Mac")
|
osName?.startsWith("Mac") == true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前操作系统是否为Unix/Linux系统
|
* 判断当前操作系统是否为 Unix/Linux(含 macOS)
|
||||||
*
|
|
||||||
* @return 如果是Unix/Linux系统返回true,否则返回false
|
|
||||||
*/
|
*/
|
||||||
val isUnix: Boolean by lazy {
|
val isUnix: Boolean by lazy {
|
||||||
if (osName == null) {
|
val name = osName ?: return@lazy false
|
||||||
false
|
UNIX_PREFIXES.any { name.startsWith(it) }
|
||||||
} else {
|
|
||||||
(osName!!.startsWith("Linux") || osName!!.startsWith("AIX") || osName!!.startsWith("SunOS") || osName!!.startsWith(
|
|
||||||
"Mac OS X"
|
|
||||||
) || osName!!.startsWith(
|
|
||||||
"FreeBSD"
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JDK 版本号属性,延迟初始化
|
* JDK 版本号属性,延迟初始化
|
||||||
*/
|
*/
|
||||||
val jdkVersion: String? by lazy {
|
val jdkVersion: String? by lazy {
|
||||||
System.getProperty("java.specification.version")
|
runCatching { System.getProperty("java.specification.version") }.getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Java版本号的整数形式属性,延迟初始化
|
* Java 版本号的主版本整数,延迟初始化
|
||||||
|
*
|
||||||
|
* 同时兼容 `1.8` 旧式版本号与 `9` / `17` / `100` 等新式版本号。
|
||||||
|
*
|
||||||
|
* @throws IllegalStateException 无法解析出整数版本时抛出
|
||||||
*/
|
*/
|
||||||
val javaVersionAsInteger: Int by lazy {
|
val javaVersionAsInteger: Int by lazy {
|
||||||
val version = jdkVersion
|
val version = jdkVersion
|
||||||
if (version == null || version.isEmpty()) {
|
if (version.isNullOrBlank()) {
|
||||||
throw IllegalStateException(
|
throw IllegalStateException(
|
||||||
"Unable to determine Java version from property 'java.specification.version'"
|
"Unable to determine Java version from property 'java.specification.version'"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
val normalized = if (version.startsWith("1.")) version.substring(2) else version
|
||||||
val uversion: String = if (version.startsWith("1.")) {
|
val major = normalized.substringBefore('.').substringBefore('-')
|
||||||
if (version.length < 3) {
|
major.toIntOrNull() ?: throw IllegalStateException("Invalid Java version format: $version")
|
||||||
throw IllegalStateException(
|
|
||||||
"Invalid Java version format: $version"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
version[2] + ""
|
|
||||||
} else {
|
|
||||||
if (version.length < 2) {
|
|
||||||
throw IllegalStateException(
|
|
||||||
"Invalid Java version format: $version"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
version.take(2)
|
|
||||||
}
|
|
||||||
uversion.toInt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前 JDK 版本是否大于 8
|
* 判断当前 JDK 版本是否大于 8
|
||||||
*
|
|
||||||
* @return 如果JDK版本大于8返回true,否则返回false
|
|
||||||
*/
|
*/
|
||||||
val isJdk8Plus: Boolean by lazy {
|
val isJdk8Plus: Boolean by lazy {
|
||||||
javaVersionAsInteger > 8
|
javaVersionAsInteger > 8
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地IP地址数组,延迟初始化
|
* 本地 IPv4 地址数组,跳过回环、虚拟与未启用网卡
|
||||||
*
|
*
|
||||||
* @throws RuntimeException 当获取网络接口信息失败时抛出
|
* @throws RuntimeException 网络接口枚举失败时抛出
|
||||||
*/
|
*/
|
||||||
val localIps: Array<String> by lazy {
|
val localIps: Array<String> by lazy {
|
||||||
try {
|
try {
|
||||||
val ipList: MutableList<String> = ArrayList()
|
activeNetworkInterfaces().flatMap { it.inetAddresses.toList() }.filterIsInstance<Inet4Address>()
|
||||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
.mapNotNull { it.hostAddress }.toList().toTypedArray()
|
||||||
|
|
||||||
while (interfaces.hasMoreElements()) {
|
|
||||||
val networkInterface = interfaces.nextElement()
|
|
||||||
// 跳过回环接口和虚拟接口
|
|
||||||
if (networkInterface.isLoopback || networkInterface.isVirtual || !networkInterface.isUp) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val addresses = networkInterface.inetAddresses
|
|
||||||
while (addresses.hasMoreElements()) {
|
|
||||||
val address = addresses.nextElement()
|
|
||||||
// 只获取IPv4地址
|
|
||||||
if (address is Inet4Address) {
|
|
||||||
ipList.add(address.hostAddress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ipList.toTypedArray()
|
|
||||||
} catch (e: SocketException) {
|
} catch (e: SocketException) {
|
||||||
throw RuntimeException("Failed to get local IP addresses", e)
|
throw RuntimeException("Failed to get local IP addresses", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地IP地址列表,延迟初始化
|
* 本地 IPv4 地址列表
|
||||||
*/
|
*/
|
||||||
val localIpsByList: List<String> by lazy {
|
val localIpsByList: List<String> by lazy { localIps.toList() }
|
||||||
localIps.toList()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地回环地址数组,延迟初始化
|
* 本地回环地址数组;枚举失败时至少包含 `127.0.0.1`
|
||||||
*/
|
*/
|
||||||
val loopbackIps: Array<String> by lazy {
|
val loopbackIps: Array<String> by lazy {
|
||||||
val strings: MutableList<String> = ArrayList(3)
|
try {
|
||||||
return@lazy try {
|
NetworkInterface.getNetworkInterfaces().toList().asSequence().filter { it.isLoopback && it.isUp }
|
||||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
.flatMap { it.inetAddresses.toList().asSequence() }.mapNotNull { it.hostAddress }.toList().toTypedArray()
|
||||||
|
} catch (_: SocketException) {
|
||||||
while (interfaces.hasMoreElements()) {
|
|
||||||
val networkInterface = interfaces.nextElement()
|
|
||||||
|
|
||||||
// 只处理回环接口
|
|
||||||
if (networkInterface.isLoopback && networkInterface.isUp) {
|
|
||||||
val addresses = networkInterface.inetAddresses
|
|
||||||
|
|
||||||
while (addresses.hasMoreElements()) {
|
|
||||||
val address = addresses.nextElement()
|
|
||||||
strings.add(address.hostAddress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
strings.toTypedArray()
|
|
||||||
} catch (e: SocketException) {
|
|
||||||
arrayOf("127.0.0.1")
|
arrayOf("127.0.0.1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地回环地址IP列表,延迟初始化
|
* 本地回环地址列表
|
||||||
*/
|
*/
|
||||||
val loopbackIpsByList: List<String> by lazy {
|
val loopbackIpsByList: List<String> by lazy { loopbackIps.toList() }
|
||||||
loopbackIps.toList()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前进程的PID
|
* 当前进程 PID;获取失败返回 -1
|
||||||
*
|
|
||||||
* @return 进程ID,如果无法获取则返回-1
|
|
||||||
*/
|
*/
|
||||||
val getPid: Long by lazy {
|
val getPid: Long by lazy {
|
||||||
try {
|
runCatching { ProcessHandle.current().pid() }.getOrElse { -1L }
|
||||||
val name = ManagementFactory.getRuntimeMXBean().name
|
|
||||||
val index = name.indexOf('@')
|
|
||||||
if (index > 0) {
|
|
||||||
name.take(index).toLong()
|
|
||||||
} else {
|
|
||||||
-1L
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
-1L
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前进程的PID字符串形式
|
* 当前进程 PID 字符串;获取失败返回 "-1"
|
||||||
*
|
|
||||||
* @return 进程ID字符串,如果无法获取则返回"-1"
|
|
||||||
*/
|
*/
|
||||||
val pidAsString: String by lazy {
|
val pidAsString: String by lazy {
|
||||||
try {
|
val pid = getPid
|
||||||
val name = ManagementFactory.getRuntimeMXBean().name
|
if (pid == -1L) "-1" else pid.toString()
|
||||||
val index = name.indexOf('@')
|
|
||||||
if (index > 0) {
|
|
||||||
name.take(index)
|
|
||||||
} else {
|
|
||||||
"-1"
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
"-1"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取计算机名
|
* 计算机名;获取失败返回 "unknown"
|
||||||
*
|
|
||||||
* @return 计算机名,如果无法获取则返回"unknown"
|
|
||||||
*/
|
*/
|
||||||
val computerName: String by lazy {
|
val computerName: String by lazy {
|
||||||
try {
|
getEnvVar("COMPUTERNAME") ?: getEnvVar("HOSTNAME")
|
||||||
var name = System.getenv("COMPUTERNAME")
|
?: runCatching { InetAddress.getLocalHost().hostName }.getOrNull()?.takeIf { it.isNotBlank() } ?: "unknown"
|
||||||
if (name.isNullOrBlank()) {
|
|
||||||
name = System.getenv("HOSTNAME")
|
|
||||||
}
|
|
||||||
if (name.isNullOrBlank()) {
|
|
||||||
name = InetAddress.getLocalHost().hostName
|
|
||||||
}
|
|
||||||
name ?: "unknown"
|
|
||||||
} catch (e: Exception) {
|
|
||||||
"unknown"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前用户名
|
* 当前用户名;获取失败返回 "unknown"
|
||||||
*
|
|
||||||
* @return 当前用户名,如果无法获取则返回"unknown"
|
|
||||||
*/
|
*/
|
||||||
val userName: String by lazy {
|
val userName: String by lazy {
|
||||||
try {
|
getEnvVar("USERNAME") ?: getEnvVar("USER") ?: runCatching { System.getProperty("user.name") }.getOrNull()
|
||||||
getEnvVar("USERNAME") ?: getEnvVar("USER") ?: System.getProperty("user.name") ?: "unknown"
|
?.takeIf { it.isNotBlank() } ?: "unknown"
|
||||||
} catch (e: SecurityException) {
|
|
||||||
"unknown"
|
|
||||||
} catch (e: Exception) {
|
|
||||||
"unknown"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEnvVar(name: String): String? {
|
private fun getEnvVar(name: String): String? = runCatching {
|
||||||
return try {
|
|
||||||
System.getenv(name)?.takeIf { it.isNotBlank() }
|
System.getenv(name)?.takeIf { it.isNotBlank() }
|
||||||
} catch (e: SecurityException) {
|
}.getOrNull()
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本机MAC地址的字节数组形式
|
* 本机 MAC 地址字节数组
|
||||||
*
|
*
|
||||||
* @return MAC地址字节数组,如果无法获取则返回空数组
|
* 未找到可用网卡时回退为 6 字节随机数,避免下游拿到空数组导致错误。
|
||||||
*/
|
*/
|
||||||
val macAddressBytes: ByteArray by lazy {
|
val macAddressBytes: ByteArray by lazy {
|
||||||
try {
|
val found = try {
|
||||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
activeNetworkInterfaces().mapNotNull { runCatching { it.hardwareAddress }.getOrNull() }
|
||||||
|
.firstOrNull { it.isNotEmpty() }
|
||||||
while (interfaces.hasMoreElements()) {
|
} catch (_: SocketException) {
|
||||||
val networkInterface = interfaces.nextElement()
|
null
|
||||||
|
|
||||||
// 跳过回环接口和虚拟接口
|
|
||||||
if (networkInterface.isLoopback || networkInterface.isVirtual || !networkInterface.isUp) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val mac = networkInterface.hardwareAddress
|
|
||||||
if (mac != null && mac.isNotEmpty()) {
|
|
||||||
return@lazy mac
|
|
||||||
}
|
|
||||||
}
|
|
||||||
randomByte(6)
|
|
||||||
} catch (e: SocketException) {
|
|
||||||
randomByte(6)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
randomByte(6)
|
|
||||||
}
|
}
|
||||||
|
found ?: randomByte(6)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本机MAC地址的十六进制字符串列表形式
|
* 本机 MAC 地址的十六进制字符串列表(大写)
|
||||||
*
|
|
||||||
* @return MAC地址字符串列表,每个元素表示一个字节的十六进制值(大写),如果无法获取则返回空列表
|
|
||||||
*/
|
*/
|
||||||
val macAddressStringList: List<String> by lazy {
|
val macAddressStringList: List<String> by lazy {
|
||||||
val macBytes = macAddressBytes
|
macAddressBytes.map { HEX_UPPER[it.toInt() and 0xFF] }
|
||||||
if (macBytes.isEmpty()) {
|
|
||||||
return@lazy emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
macBytes.map { String.format("%02X", it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本机MAC地址的格式化字符串形式(如 "00:11:22:33:44:55")
|
* 本机 MAC 地址的格式化字符串,如 `00:11:22:33:44:55`
|
||||||
*
|
|
||||||
* @return 格式化的MAC地址字符串,如果无法获取则返回空字符串
|
|
||||||
*/
|
*/
|
||||||
val macAddressFormattedString: String by lazy {
|
val macAddressFormattedString: String by lazy {
|
||||||
val macBytes = macAddressBytes
|
macAddressBytes.joinToString(":") { HEX_UPPER[it.toInt() and 0xFF] }
|
||||||
if (macBytes.isEmpty()) {
|
|
||||||
return@lazy ""
|
|
||||||
}
|
|
||||||
|
|
||||||
macBytes.joinToString(":") { String.format("%02X", it) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有网络接口的MAC地址映射
|
* 所有非回环、非虚拟网卡的 MAC 地址映射,key 为网卡名
|
||||||
*
|
|
||||||
* @return Map结构,key为网络接口名称,value为对应的MAC地址字节数组
|
|
||||||
*/
|
*/
|
||||||
val allMacAddresses: Map<String, ByteArray> by lazy {
|
val allMacAddresses: Map<String, ByteArray> by lazy {
|
||||||
try {
|
try {
|
||||||
val result = mutableMapOf<String, ByteArray>()
|
NetworkInterface.getNetworkInterfaces().toList().asSequence().filter { !it.isLoopback && !it.isVirtual }
|
||||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
.mapNotNull { ni ->
|
||||||
|
val mac = runCatching { ni.hardwareAddress }.getOrNull()
|
||||||
while (interfaces.hasMoreElements()) {
|
if (mac != null && mac.isNotEmpty()) ni.name to mac else null
|
||||||
val networkInterface = interfaces.nextElement()
|
}.toMap()
|
||||||
|
} catch (_: SocketException) {
|
||||||
// 跳过回环接口和虚拟接口
|
|
||||||
if (networkInterface.isLoopback || networkInterface.isVirtual) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val mac = networkInterface.hardwareAddress
|
|
||||||
if (mac != null && mac.isNotEmpty()) {
|
|
||||||
result[networkInterface.name] = mac
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
} catch (e: SocketException) {
|
|
||||||
emptyMap()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
emptyMap()
|
emptyMap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有网络接口的MAC地址字符串列表映射
|
* 所有网卡 MAC 地址的十六进制字符串列表映射
|
||||||
*
|
|
||||||
* @return Map结构,key为网络接口名称,value为对应的MAC地址字符串列表
|
|
||||||
*/
|
*/
|
||||||
val allMacAddressesStringList: Map<String, List<String>> by lazy {
|
val allMacAddressesStringList: Map<String, List<String>> by lazy {
|
||||||
allMacAddresses.mapValues { entry ->
|
allMacAddresses.mapValues { (_, bytes) -> bytes.map { HEX_UPPER[it.toInt() and 0xFF] } }
|
||||||
entry.value.map { String.format("%02X", it) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取可用处理器数量的懒加载属性
|
* 可用处理器数量
|
||||||
*/
|
*/
|
||||||
val availableProcessors: Int by lazy {
|
val availableProcessors: Int by lazy {
|
||||||
Runtime.getRuntime().availableProcessors()
|
Runtime.getRuntime().availableProcessors()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun activeNetworkInterfaces(): Sequence<NetworkInterface> =
|
||||||
private var isLoadprotocol = false
|
NetworkInterface.getNetworkInterfaces().toList().asSequence().filter { !it.isLoopback && !it.isVirtual && it.isUp }
|
||||||
private var protocol = ""
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
private val protocol: String by lazy {
|
||||||
* 判断是否为开发模式(file协议)
|
val target = ProtocolAnchor::class.java
|
||||||
*/
|
val resourceName = target.simpleName + ".class"
|
||||||
val isDevMode: Boolean by lazy {
|
val resource: URL = target.getResource(resourceName) ?: throw IllegalStateException(
|
||||||
protocol == "file"
|
"Unable to locate resource '$resourceName' for class ${target.name}"
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否不为开发模式
|
|
||||||
*/
|
|
||||||
val isNotDevMode: Boolean by lazy {
|
|
||||||
protocol != "file"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否为JAR模式
|
|
||||||
*/
|
|
||||||
val isJarMode: Boolean by lazy {
|
|
||||||
protocol == "jar"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否不为JAR模式
|
|
||||||
*/
|
|
||||||
val isNotJarMode: Boolean by lazy {
|
|
||||||
protocol != "jar"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否为WAR模式
|
|
||||||
*/
|
|
||||||
val isWarMode: Boolean by lazy {
|
|
||||||
protocol == "war"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否不为WAR模式
|
|
||||||
*/
|
|
||||||
val isNotMode: Boolean by lazy {
|
|
||||||
protocol != "war"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否为JSWAR模式
|
|
||||||
*/
|
|
||||||
val isJswarMode: Boolean by lazy {
|
|
||||||
protocol == "jswar"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否不为JSWAR模式
|
|
||||||
*/
|
|
||||||
val isNotJswarMode: Boolean by lazy {
|
|
||||||
protocol != "jswar"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 加载并获取资源协议
|
|
||||||
* @param clazz 要获取协议的类对象,默认为null
|
|
||||||
* @return 返回资源的协议字符串
|
|
||||||
*/
|
|
||||||
fun loadProtocol(clazz: Class<*>? = null): String {
|
|
||||||
// 如果已经加载过协议且clazz为null,则直接返回已缓存的协议
|
|
||||||
if (isLoadprotocol && clazz == null) {
|
|
||||||
return protocol
|
|
||||||
}
|
|
||||||
val resource: URL? = clazz!!.getResource(
|
|
||||||
clazz.getSimpleName() + ".class"
|
|
||||||
)
|
)
|
||||||
protocol = resource!!.protocol
|
resource.protocol
|
||||||
return protocol.let {
|
|
||||||
isLoadprotocol = true
|
|
||||||
it
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** 是否为开发模式(file 协议) */
|
||||||
|
val isDevMode: Boolean get() = protocol == "file"
|
||||||
|
|
||||||
|
/** 是否不为开发模式 */
|
||||||
|
val isNotDevMode: Boolean get() = protocol != "file"
|
||||||
|
|
||||||
|
/** 是否为 JAR 模式 */
|
||||||
|
val isJarMode: Boolean get() = protocol == "jar"
|
||||||
|
|
||||||
|
/** 是否不为 JAR 模式 */
|
||||||
|
val isNotJarMode: Boolean get() = protocol != "jar"
|
||||||
|
|
||||||
|
/** 是否为 WAR 模式 */
|
||||||
|
val isWarMode: Boolean get() = protocol == "war"
|
||||||
|
|
||||||
|
/** 是否不为 WAR 模式 */
|
||||||
|
val isNotWarMode: Boolean get() = protocol != "war"
|
||||||
|
|
||||||
|
/** 是否为 JSWAR 模式 */
|
||||||
|
val isJswarMode: Boolean get() = protocol == "jswar"
|
||||||
|
|
||||||
|
/** 是否不为 JSWAR 模式 */
|
||||||
|
val isNotJswarMode: Boolean get() = protocol != "jswar"
|
||||||
|
|
||||||
|
val lineSeparator
|
||||||
|
get() = System.lineSeparator()
|
||||||
|
|
||||||
|
const val lineSeparatorLF = "\n"
|
||||||
|
|
||||||
|
const val lineSeparatorCR = "\r"
|
||||||
|
|
||||||
|
const val lineSeparatorCRLF = lineSeparatorCR + lineSeparatorLF
|
||||||
|
|
||||||
|
private object ProtocolAnchor
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* 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 AsyncTasks.kt
|
||||||
|
* LastUpdate 2026-10-19 20:51:35
|
||||||
|
* UpdateUser MingLiPro
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mingliqiye.utils.thread
|
||||||
|
|
||||||
|
import java.util.concurrent.*
|
||||||
|
|
||||||
|
// ---- 并行执行 ----
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 并行执行全部任务,阻塞等待所有完成,按输入顺序返回结果。
|
||||||
|
* 任一任务失败立即抛出异常。
|
||||||
|
*
|
||||||
|
* ```kotlin
|
||||||
|
* val results = runParallel(
|
||||||
|
* { fetchUser() },
|
||||||
|
* { fetchOrder() },
|
||||||
|
* { fetchCart() },
|
||||||
|
* )
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
fun <T> runParallel(
|
||||||
|
vararg tasks: () -> T,
|
||||||
|
executor: ExecutorService = GlobalThreadPool.io,
|
||||||
|
): List<T> = executor.invokeAllAndGet(tasks.map { Callable(it) })
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 并行执行全部任务,阻塞等待所有完成,按输入顺序返回结果
|
||||||
|
*/
|
||||||
|
fun <T> runParallel(
|
||||||
|
tasks: Collection<() -> T>,
|
||||||
|
executor: ExecutorService = GlobalThreadPool.io,
|
||||||
|
): List<T> = executor.invokeAllAndGet(tasks.map { Callable(it) })
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 并行执行全部任务,任一任务在超时时间内未完成则抛出 [java.util.concurrent.TimeoutException]
|
||||||
|
*/
|
||||||
|
fun <T> runParallelWithin(
|
||||||
|
timeout: Long,
|
||||||
|
unit: TimeUnit = TimeUnit.MILLISECONDS,
|
||||||
|
executor: ExecutorService = GlobalThreadPool.io,
|
||||||
|
tasks: Collection<() -> T>,
|
||||||
|
): List<T> {
|
||||||
|
val futures = tasks.map { executor.submit(Callable(it)) }
|
||||||
|
val deadline = System.nanoTime() + unit.toNanos(timeout)
|
||||||
|
return futures.map { future ->
|
||||||
|
val remaining = deadline - System.nanoTime()
|
||||||
|
if (remaining <= 0) throw java.util.concurrent.TimeoutException("runParallel timeout $timeout $unit")
|
||||||
|
future.get(remaining, TimeUnit.NANOSECONDS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行全部任务,返回第一个成功完成的任务结果,其余任务取消。
|
||||||
|
*/
|
||||||
|
fun <T> firstCompleted(
|
||||||
|
vararg tasks: () -> T,
|
||||||
|
executor: ExecutorService = GlobalThreadPool.io,
|
||||||
|
): T = executor.invokeAny(tasks.map { Callable(it) })
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行全部任务,返回第一个成功完成的任务结果,其余任务取消
|
||||||
|
*/
|
||||||
|
fun <T> firstCompleted(
|
||||||
|
tasks: Collection<() -> T>,
|
||||||
|
executor: ExecutorService = GlobalThreadPool.io,
|
||||||
|
): T = executor.invokeAny(tasks.map { Callable(it) })
|
||||||
|
|
||||||
|
// ---- 集合工具 ----
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待所有 Future 完成并按顺序收集结果,任一失败抛出原始异常
|
||||||
|
*/
|
||||||
|
fun <T> awaitAll(futures: Collection<Future<T>>): List<T> = futures.map { it.await() }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交全部 Callable 并阻塞等待所有完成,任一失败抛出原始异常
|
||||||
|
*/
|
||||||
|
fun <T> ExecutorService.invokeAllAndGet(tasks: Collection<Callable<T>>): List<T> =
|
||||||
|
invokeAll(tasks).map { it.await() }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 等待所有 Future 完成,失败的任务返回 null(不中断整体流程)
|
||||||
|
*/
|
||||||
|
fun <T> awaitAllOrNull(futures: Collection<Future<T>>): List<T?> =
|
||||||
|
futures.map { it.getOrNull() }
|
||||||
|
|
||||||
|
// ---- 重试 ----
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带重试的调用:任务抛出异常时重试,全部失败抛出最后一次异常。
|
||||||
|
*
|
||||||
|
* ```kotlin
|
||||||
|
* val data = callWithRetry(3, delayMillis = 500) { httpGet(url) }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @param times 总尝试次数(含首次),必须 >= 1
|
||||||
|
* @param delayMillis 每次重试前的等待毫秒数
|
||||||
|
*/
|
||||||
|
fun <T> callWithRetry(
|
||||||
|
times: Int,
|
||||||
|
delayMillis: Long = 0,
|
||||||
|
task: () -> T,
|
||||||
|
): T {
|
||||||
|
require(times >= 1) { "times must be >= 1: $times" }
|
||||||
|
var last: Throwable? = null
|
||||||
|
repeat(times) { attempt ->
|
||||||
|
try {
|
||||||
|
return task()
|
||||||
|
} catch (t: Throwable) {
|
||||||
|
last = t
|
||||||
|
// 最后一次失败不再等待
|
||||||
|
if (attempt < times - 1 && delayMillis > 0) {
|
||||||
|
Thread.sleep(delayMillis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw last ?: IllegalStateException("unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在全局 IO 池中带重试执行任务
|
||||||
|
*/
|
||||||
|
fun <T> callAsyncWithRetry(
|
||||||
|
times: Int,
|
||||||
|
delayMillis: Long = 0,
|
||||||
|
task: () -> T,
|
||||||
|
): Future<T> = GlobalThreadPool.io.submit(Callable { callWithRetry(times, delayMillis, task) })
|
||||||
|
|
||||||
|
// ---- 定时任务 ----
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 延迟执行任务(全局定时池)
|
||||||
|
*
|
||||||
|
* ```kotlin
|
||||||
|
* delayRun(5, TimeUnit.SECONDS) { log.info("5s later") }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
fun delayRun(
|
||||||
|
delay: Long,
|
||||||
|
unit: TimeUnit = TimeUnit.MILLISECONDS,
|
||||||
|
action: () -> Unit,
|
||||||
|
): ScheduledFuture<*> = GlobalThreadPool.scheduled.schedule(action, delay, unit)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以固定频率执行任务(周期 = 任务耗时 + [period]),全局定时池
|
||||||
|
*/
|
||||||
|
fun scheduleWithFixedDelay(
|
||||||
|
period: Long,
|
||||||
|
unit: TimeUnit = TimeUnit.MILLISECONDS,
|
||||||
|
initialDelay: Long = 0,
|
||||||
|
action: () -> Unit,
|
||||||
|
): ScheduledFuture<*> =
|
||||||
|
GlobalThreadPool.scheduled.scheduleWithFixedDelay(action, initialDelay, period, unit)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以固定频率执行任务(周期恒定为 [period]),全局定时池
|
||||||
|
*/
|
||||||
|
fun scheduleAtFixedRate(
|
||||||
|
period: Long,
|
||||||
|
unit: TimeUnit = TimeUnit.MILLISECONDS,
|
||||||
|
initialDelay: Long = 0,
|
||||||
|
action: () -> Unit,
|
||||||
|
): ScheduledFuture<*> =
|
||||||
|
GlobalThreadPool.scheduled.scheduleAtFixedRate(action, initialDelay, period, unit)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user