Opt.ofEmptyAble支持更多类型

This commit is contained in:
Looly 2023-12-15 22:00:15 +08:00
parent 9242ca262b
commit bcc5bc6d6c
2 changed files with 4 additions and 4 deletions

View File

@ -11,10 +11,10 @@
*/ */
package org.dromara.hutool.core.lang; package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.func.SerSupplier; import org.dromara.hutool.core.func.SerSupplier;
import org.dromara.hutool.core.stream.EasyStream; import org.dromara.hutool.core.stream.EasyStream;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.util.Collection; import java.util.Collection;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -93,12 +93,12 @@ public class Opt<T> {
* *
* @param <T> 包裹里元素的类型 * @param <T> 包裹里元素的类型
* @param <R> 集合值类型 * @param <R> 集合值类型
* @param value 传入需要包裹的元素 * @param value 传入需要包裹的元素支持CharSequenceMapIterableIteratorArray类型
* @return 一个包裹里元素可能为空的 {@code Opt} * @return 一个包裹里元素可能为空的 {@code Opt}
* @since 5.7.17 * @since 5.7.17
*/ */
public static <T, R extends Collection<T>> Opt<R> ofEmptyAble(final R value) { public static <T, R extends Collection<T>> Opt<R> ofEmptyAble(final R value) {
return CollUtil.isEmpty(value) || CollUtil.getFirstNoneNull(value) == null ? empty() : new Opt<>(value); return ObjUtil.isEmpty(value) ? empty() : new Opt<>(value);
} }
/** /**

View File

@ -168,7 +168,7 @@ public class OptTest {
final List<String> hutool = Opt.ofEmptyAble(Collections.<String>emptyList()).orElseGet(() -> Collections.singletonList("hutool")); final List<String> hutool = Opt.ofEmptyAble(Collections.<String>emptyList()).orElseGet(() -> Collections.singletonList("hutool"));
Assertions.assertEquals(past, hutool); Assertions.assertEquals(past, hutool);
Assertions.assertEquals(Collections.singletonList("hutool"), hutool); Assertions.assertEquals(Collections.singletonList("hutool"), hutool);
Assertions.assertTrue(Opt.ofEmptyAble(Arrays.asList(null, null, null)).isEmpty()); Assertions.assertFalse(Opt.ofEmptyAble(Arrays.asList(null, null, null)).isEmpty());
} }
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "ConstantConditions"}) @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "ConstantConditions"})