mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Revert "AsyncUtil下增加getOpt方法,用于执行失败时提供默认值,也能自定获取异常;拓展了Opt.ofEmptyAble方法,使其可以判断Collection下的其他集合"
This reverts commit 158290c38808044cc65c41a176db9ae9e55d9715.
This commit is contained in:
parent
7242d6992f
commit
c9ad39ab3a
@ -24,12 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package cn.hutool.core.lang;
|
package cn.hutool.core.lang;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.func.Func0;
|
import cn.hutool.core.lang.func.Func0;
|
||||||
import cn.hutool.core.lang.func.VoidFunc0;
|
import cn.hutool.core.lang.func.VoidFunc0;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -101,19 +101,18 @@ public class Opt<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回一个包裹里{@code Collection}集合可能为空的{@code Opt},额外判断了集合内元素为空的情况
|
* 返回一个包裹里{@code List}集合可能为空的{@code Opt},额外判断了集合内元素为空的情况
|
||||||
*
|
*
|
||||||
* @param value 传入需要包裹的元素
|
* @param value 传入需要包裹的元素
|
||||||
* @param <T> 包裹里元素的类型
|
* @param <T> 包裹里元素的类型
|
||||||
* @return 一个包裹里元素可能为空的 {@code Opt}
|
* @return 一个包裹里元素可能为空的 {@code Opt}
|
||||||
* @since 5.7.17
|
* @since 5.7.17
|
||||||
*/
|
*/
|
||||||
public static <T, R extends Collection<T>> Opt<R> ofEmptyAble(R value) {
|
public static <T> Opt<List<T>> ofEmptyAble(List<T> value) {
|
||||||
return CollUtil.isEmpty(value) ? empty() : new Opt<>(value);
|
return CollectionUtil.isEmpty(value) ? empty() : new Opt<>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 传入一段操作,包裹异常
|
|
||||||
*
|
*
|
||||||
* @param supplier 操作
|
* @param supplier 操作
|
||||||
* @param <T> 类型
|
* @param <T> 类型
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package cn.hutool.core.thread;
|
package cn.hutool.core.thread;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Opt;
|
|
||||||
|
|
||||||
import java.lang.reflect.UndeclaredThrowableException;
|
import java.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@ -62,15 +60,4 @@ public class AsyncUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取异步任务结果,包裹了异常,并返回 {@link Opt}
|
|
||||||
*
|
|
||||||
* @param <T> 任务返回值类型
|
|
||||||
* @param task 异步任务
|
|
||||||
* @return 任务返回值
|
|
||||||
*/
|
|
||||||
public static <T> Opt<T> getOpt(CompletableFuture<T> task) {
|
|
||||||
return Opt.ofTry(task::get);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package cn.hutool.core.thread;
|
package cn.hutool.core.thread;
|
||||||
|
|
||||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
||||||
import cn.hutool.core.lang.Opt;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -36,12 +34,5 @@ public class AsyncUtilTest {
|
|||||||
AsyncUtil.waitAll(hutool, sweater, warm);
|
AsyncUtil.waitAll(hutool, sweater, warm);
|
||||||
// 获取结果
|
// 获取结果
|
||||||
Assert.assertEquals("hutool卫衣真暖和", AsyncUtil.get(hutool) + AsyncUtil.get(sweater) + AsyncUtil.get(warm));
|
Assert.assertEquals("hutool卫衣真暖和", AsyncUtil.get(hutool) + AsyncUtil.get(sweater) + AsyncUtil.get(warm));
|
||||||
|
|
||||||
Opt<String> opt = AsyncUtil.getOpt(CompletableFuture.supplyAsync(() -> {
|
|
||||||
ExceptionUtil.wrapRuntimeAndThrow("Ops!");
|
|
||||||
return "whatever";
|
|
||||||
}));
|
|
||||||
Assert.assertTrue(opt.isFail());
|
|
||||||
Assert.assertEquals("java.lang.RuntimeException: Ops!", opt.getException().getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user