From 8069de37f2cd8ffe2692c8f7ab95d4a94cd127f3 Mon Sep 17 00:00:00 2001 From: minglipro Date: Tue, 17 Nov 2026 21:17:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(tuple):=20=E5=AE=9E=E7=8E=B0=20Tuple.compa?= =?UTF-8?q?reTo=20=E5=AD=97=E5=85=B8=E5=BA=8F=E6=AF=94=E8=BE=83=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E5=B8=83=205.5.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 替换原 TODO 占位实现:同类型可比较元素按自然顺序逐位比较,其余按 字符串形式回退,公共前缀相等时短元组在前;新增 TestTuple 6 个用例 全部通过;CHANGELOG 增加 5.5.2 条目,VERSIONS 5.5.1 -> 5.5.2。 --- CHANGELOG.md | 9 ++- gradle.properties | 2 +- .../com/mingliqiye/utils/tuple/Tuples.kt | 28 ++++++- .../kotlin/com/mingliqiye/utils/TestTuple.kt | 76 +++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/com/mingliqiye/utils/TestTuple.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 70aa52f..30ede42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ 本文件记录 mingli-utils 各版本的主要变更。 +## [5.5.2] - 2026-11-17 + +### 新增 +- **tuple**:实现 `Tuple.compareTo` 字典序比较——同类型可比较元素按自然顺序, + 其余按字符串形式比较;公共前缀相等时元素个数较少的元组在前。 + 新增 `TestTuple` 测试覆盖 6 种比较场景。 + ## [5.5.1] - 2026-11-17 ### 新增 @@ -17,7 +24,7 @@ 等 10 个转换器及 `JsonConverter` 接口,统一由 `BaseJsonConverter` 与 `JsonConverterUtils` 处理; 简化 `DateTimeJsonConverter`、`UUIDJsonConverter`、`NetworkEndpointJsonConverter` 与 Jackson2/Jackson3 序列化/反序列化桥接实现;精简 `JSONA` 与 `JsonApi` 门面。 -- **tuple**:`Tuple` 实现 `Comparable`,迭代器改为基于 `toMutableList()` 的简化实现。 +- **tuple**:`Tuple` 声明实现 `Comparable`;迭代器改为基于 `toMutableList()` 的简化实现。 - **springboot**:`JsonAutoConfiguration` 与 `JacksonJsonBuilder` / `JacksonJsonBuilderImpl` 同步适配新的 JSON 转换器体系。 diff --git a/gradle.properties b/gradle.properties index 641fac0..2aaa804 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,4 +22,4 @@ JDKVERSIONS=17 GROUPSID=com.mingliqiye.utils ARTIFACTID=mingli-utils -VERSIONS=5.5.1 +VERSIONS=5.5.2 diff --git a/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt b/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt index 08a124f..1424cc9 100644 --- a/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt +++ b/src/main/kotlin/com/mingliqiye/utils/tuple/Tuples.kt @@ -16,7 +16,7 @@ * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile Tuples.kt - * LastUpdate 2026-11-17 20:29:54 + * LastUpdate 2026-11-17 21:11:31 * UpdateUser MingLiPro */ @@ -34,6 +34,32 @@ import java.io.Serializable */ abstract class Tuple(val size: Int = 1) : Serializable, Iterable, Comparable { + /** + * 按字典序比较当前元组与另一个元组。 + * + * 从第 1 个元素开始逐位比较:两个元素类型相同且都实现 [Comparable] 时按自然顺序比较, + * 否则按元素的字符串形式([toString])比较。 + * 公共位置元素全部相等时,元素个数较少的元组视为较小。 + * + * @param other 要比较的目标元组 + * @return 负数、零或正数,分别表示当前元组小于、等于或大于 [other] + */ + override fun compareTo(other: Tuple): Int { + val minSize = minOf(size, other.size) + for (i in 0.. && b is Comparable<*> && a.javaClass == b.javaClass) { + @Suppress("UNCHECKED_CAST") + (a as Comparable).compareTo(b) + } else { + a.toString().compareTo(b.toString()) + } + if (result != 0) return result + } + return size.compareTo(other.size) + } + override fun iterator(): Iterator { return toMutableList().iterator() } diff --git a/src/test/kotlin/com/mingliqiye/utils/TestTuple.kt b/src/test/kotlin/com/mingliqiye/utils/TestTuple.kt new file mode 100644 index 0000000..1681f32 --- /dev/null +++ b/src/test/kotlin/com/mingliqiye/utils/TestTuple.kt @@ -0,0 +1,76 @@ +/* + * 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.test + * CurrentFile TestTuple.kt + * LastUpdate 2026-11-17 21:20:00 + * UpdateUser MingLiPro + */ + +package com.mingliqiye.utils + +import com.mingliqiye.utils.tuple.Tuple +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test + +/** + * [Tuple] 字典序比较(compareTo)测试。 + */ +class TestTuple { + + /** 测试首元素不同时按首元素自然顺序比较 */ + @Test + fun compareByFirstElement() { + assertTrue(Tuple.of(2, "a") > Tuple.of(1, "z")) + assertTrue(Tuple.of(1, "z") < Tuple.of(2, "a")) + } + + /** 测试首元素相同时按后续元素逐位比较 */ + @Test + fun compareByLaterElement() { + assertTrue(Tuple.of(1, "b") > Tuple.of(1, "a")) + assertTrue(Tuple.of(1, 2, 3) < Tuple.of(1, 2, 4)) + } + + /** 测试公共前缀相等时元素个数少的元组较小 */ + @Test + fun compareBySizeWhenPrefixEqual() { + assertTrue(Tuple.of(1) < Tuple.of(1, 0)) + assertTrue(Tuple.of(1, 0, 0) > Tuple.of(1, 0)) + } + + /** 测试元素完全相同的元组比较结果为 0 */ + @Test + fun compareEqualTuples() { + assertEquals(0, Tuple.of(1, "a").compareTo(Tuple.of(1, "a"))) + } + + /** 测试非 Comparable 元素按字符串形式比较且不抛异常 */ + @Test + fun compareNonComparableElements() { + val a = Any() + val b = Any() + assertEquals(a.toString().compareTo(b.toString()), Tuple.of(a).compareTo(Tuple.of(b))) + } + + /** 测试元组可直接使用 sorted 排序 */ + @Test + fun sortedTuples() { + val sorted = listOf(Tuple.of(2, "a"), Tuple.of(1, "z"), Tuple.of(1, "a")).sorted() + assertEquals(listOf(listOf(1, "a"), listOf(1, "z"), listOf(2, "a")), sorted.map { it.toList() }) + } +} \ No newline at end of file