From 3f666d652dd07f1bda8301dafc22912fc1452854 Mon Sep 17 00:00:00 2001 From: minglipro Date: Sat, 10 Jan 2026 08:54:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=20=E6=B7=BB=E5=8A=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3=E5=92=8C=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 RFunction 函数式接口用于无参数函数调用 - 更新文件头版权年份从 2025 到 2026 - 更新文件最后修改时间戳 - 新增 Require 类提供条件验证功能 - 支持多种构造方式包括布尔值、函数式参数等 - 提供多种异常抛出机制包括自定义异常类型 - 添加扩展函数和静态工厂方法简化使用 - 支持 Java 互操作性的 JvmStatic 注解 --- .../mingliqiye/utils/functions/Functions.kt | 9 +- .../com/mingliqiye/utils/request/Require.kt | 126 ++++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/com/mingliqiye/utils/request/Require.kt diff --git a/src/main/kotlin/com/mingliqiye/utils/functions/Functions.kt b/src/main/kotlin/com/mingliqiye/utils/functions/Functions.kt index e1560a1..5a5cdd2 100644 --- a/src/main/kotlin/com/mingliqiye/utils/functions/Functions.kt +++ b/src/main/kotlin/com/mingliqiye/utils/functions/Functions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2025 mingliqiye + * 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. @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile Functions.kt - * LastUpdate 2025-09-15 09:56:54 + * LastUpdate 2026-01-09 08:12:01 * UpdateUser MingLiPro */ @@ -85,6 +85,11 @@ fun interface P1Function

{ fun call(p: P) } +@FunctionalInterface +fun interface RFunction { + fun call(): R +} + @FunctionalInterface fun interface P1RFunction { fun call(p: P): R diff --git a/src/main/kotlin/com/mingliqiye/utils/request/Require.kt b/src/main/kotlin/com/mingliqiye/utils/request/Require.kt new file mode 100644 index 0000000..bcc76a6 --- /dev/null +++ b/src/main/kotlin/com/mingliqiye/utils/request/Require.kt @@ -0,0 +1,126 @@ +/* + * 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-01-10 08:53:26 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils.request + +import com.mingliqiye.utils.functions.P1RFunction +import com.mingliqiye.utils.functions.RFunction + +class Require(private val must: Boolean) { + + constructor(funs: RFunction) : this(funs.call()) + + constructor(must: RFunction, message: String) : this(must) { + throws(message) + } + + constructor( + must: RFunction, + message: String, + exception: Class = IllegalArgumentException::class.java + ) : this(must) { + throws(message, exception) + } + + constructor(must: Boolean, message: String) : this(must) { + throws(message) + } + + constructor( + must: Boolean, message: String, exception: Class = IllegalArgumentException::class.java + ) : this(must) { + throws(message, exception) + } + + constructor( + must: Boolean, message: String, exception: P1RFunction + ) : this(must) { + throws(message, exception) + } + + companion object { + fun Boolean.require(message: String, exception: P1RFunction): Require { + return Require(this, message, exception) + } + + fun Boolean.require( + message: String, + exception: Class = IllegalArgumentException::class.java + ): Require { + return Require(this, message, exception) + } + + @JvmStatic + fun require( + must: Boolean, message: String, exception: Class = IllegalArgumentException::class.java + ): Require { + return Require(must, message, exception) + } + + @JvmStatic + fun require(must: Boolean, message: String): Require { + return Require(must, message) + } + + @JvmStatic + fun require(must: Boolean): Require { + return Require(must) + } + + @JvmStatic + fun require( + must: RFunction, + message: String, + exception: Class = IllegalArgumentException::class.java + ): Require { + return Require(must, message, exception) + } + + @JvmStatic + fun require(must: RFunction, message: String): Require { + return Require(must, message) + } + + @JvmStatic + fun require(must: RFunction): Require { + return Require(must) + } + } + + fun throws(message: String) { + if (!must) { + throw IllegalArgumentException(message) + } + } + + fun throws(string: String, exception: Class) { + if (!must) { + throw exception.getConstructor(String::class.java).newInstance(string) + } + } + + fun throws(string: String, exception: P1RFunction) { + if (!must) { + throw exception.call(string) + } + } +}