mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-07 01:28:34 +08:00
add test and fix comment
This commit is contained in:
parent
f60f20243d
commit
9b3414b397
@ -72,14 +72,14 @@ public class ObjectUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度<br>
|
* <p>计算对象长度,支持类型包括:
|
||||||
* 支持的类型包括:
|
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>CharSequence</li>
|
* <li>{@code null}:默认返回{@code 0};</li>
|
||||||
* <li>Map</li>
|
* <li>数组:返回数组长度;</li>
|
||||||
* <li>Iterator</li>
|
* <li>{@link CharSequence}:返回{@link CharSequence#length()};</li>
|
||||||
* <li>Enumeration</li>
|
* <li>{@link Collection}:返回{@link Collection#size()};</li>
|
||||||
* <li>Array</li>
|
* <li>{@link Iterator}或{@link Iterable}:可迭代的元素数量;副作用:{@link Iterator}只能被迭代一次</li>
|
||||||
|
* <li>{@link Enumeration}:返回可迭代的元素数量;副作用:{@link Enumeration}只能被迭代一次</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param obj 被计算长度的对象
|
* @param obj 被计算长度的对象
|
||||||
|
|||||||
@ -4,13 +4,12 @@ import cn.hutool.core.clone.CloneSupport;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DatePattern;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class ObjectUtilTest {
|
public class ObjectUtilTest {
|
||||||
|
|
||||||
@ -108,4 +107,32 @@ public class ObjectUtilTest {
|
|||||||
String a = null;
|
String a = null;
|
||||||
assertFalse(ObjectUtil.isNotNull(a));
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user