Compare commits

...

2 Commits

6 changed files with 70 additions and 14 deletions

View File

@ -1,7 +1,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.42(2025-11-04)
# 5.8.42(2025-11-12)
### 🐣新特性
* 【core 】 `ListUtil`增加`zip`方法pr#4052@Github
@ -17,6 +17,7 @@
* 【http 】 修复`HttpConnection.reflectSetMethod`反射在JDK9+权限问题issue#4109@Github
* 【http 】 修复`JsonUtil.toJsonStr`对Boolean和Number返回错误问题issue#4109@Github
* 【core 】 修复`FileUtil.listFileNames`相对路径index混乱问题issue#4121@Github
* 【core 】 修复`NumberWithFormat`没有实现Comparable接口导致的JSON排序报错问题issue#ID61QR@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.41(2025-10-12)

View File

@ -13,7 +13,7 @@ import java.util.Date;
* @author looly
* @since 5.8.13
*/
public class NumberWithFormat extends Number implements TypeConverter {
public class NumberWithFormat extends Number implements TypeConverter, Comparable<NumberWithFormat> {
private static final long serialVersionUID = 1L;
private final Number number;
@ -86,4 +86,13 @@ public class NumberWithFormat extends Number implements TypeConverter {
public String toString() {
return this.number.toString();
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public int compareTo(NumberWithFormat o) {
if(this.number instanceof Comparable && o.getNumber() instanceof Comparable) {
return ((Comparable) this.number).compareTo(o.getNumber());
}
return Double.compare(this.doubleValue(), o.doubleValue());
}
}

View File

@ -168,7 +168,7 @@ public class MutableLong extends Number implements Comparable<MutableLong>, Muta
* </ol>
*
* @param obj 比对的对象
* @return 相同返回<code>true</code>否则 {@code false}
* @return 相同返回{@code true}否则 {@code false}
*/
@Override
public boolean equals(final Object obj) {

View File

@ -72,14 +72,14 @@ public class ObjectUtil {
}
/**
* 计算对象长度如果是字符串调用其length函数集合类调用其size函数数组调用其length属性其他可遍历对象遍历计算长度<br>
* 支持的类型包括
* <p>计算对象长度支持类型包括
* <ul>
* <li>CharSequence</li>
* <li>Map</li>
* <li>Iterator</li>
* <li>Enumeration</li>
* <li>Array</li>
* <li>{@code null}默认返回{@code 0}</li>
* <li>数组返回数组长度</li>
* <li>{@link CharSequence}返回{@link CharSequence#length()}</li>
* <li>{@link Collection}返回{@link Collection#size()}</li>
* <li>{@link Iterator}{@link Iterable}可迭代的元素数量副作用{@link Iterator}只能被迭代一次</li>
* <li>{@link Enumeration}返回可迭代的元素数量副作用{@link Enumeration}只能被迭代一次</li>
* </ul>
*
* @param obj 被计算长度的对象

View File

@ -4,13 +4,12 @@ import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class ObjectUtilTest {
@ -108,4 +107,32 @@ public class ObjectUtilTest {
String a = null;
assertFalse(ObjectUtil.isNotNull(a));
}
@Test
public void testLengthConsumesIterator() {
List<String> list = Arrays.asList("a", "b", "c");
Iterator<String> iterator = list.iterator();
// 迭代器第一次调用length
int length1 = ObjectUtil.length(iterator);
assertEquals(3, length1);
// 迭代器第二次调用length - 迭代器已经被消耗返回0
int length2 = ObjectUtil.length(iterator);
assertEquals(0, length2); // 但当前实现会重新遍历但iterator已经没有元素了
// 尝试使用迭代器 - 已经无法使用
assertFalse(iterator.hasNext());
}
@Test
public void testLengthConsumesEnumeration() {
Vector<String> vector = new Vector<>(Arrays.asList("a", "b", "c"));
Enumeration<String> enumeration = vector.elements();
// 第一次调用length
int length1 = ObjectUtil.length(enumeration);
assertEquals(3, length1);
// 第二次调用length - 枚举已经被消耗
int length2 = ObjectUtil.length(enumeration);
assertEquals(0, length2);
// 枚举已经无法使用
assertFalse(enumeration.hasMoreElements());
}
}

View File

@ -0,0 +1,19 @@
package cn.hutool.json;
import cn.hutool.core.map.MapUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
public class IssueID61QRTest {
@Test
public void testName() {
JSONObject map1 = JSONUtil.createObj(new JSONConfig().setDateFormat("yyyy"));
// JSONObject map1 = JSONUtil.createObj();
map1.set("a", 3);
map1.set("b", 5);
map1.set("c", 5432);
Assertions.assertEquals("{c=5432, b=5, a=3}", MapUtil.sortByValue(JSONUtil.toBean(map1, Map.class), true).toString());
}
}