mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
AsyncUtil下增加getOpt方法,用于执行失败时提供默认值,也能自定获取异常;拓展了Opt.ofEmptyAble方法,使其可以判断Collection下的其他集合
This commit is contained in:
parent
efeddc1469
commit
158290c388
@ -24,12 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package cn.hutool.core.lang;
|
package cn.hutool.core.lang;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
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.List;
|
import java.util.Collection;
|
||||||
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,18 +101,19 @@ public class Opt<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回一个包裹里{@code List}集合可能为空的{@code Opt},额外判断了集合内元素为空的情况
|
* 返回一个包裹里{@code Collection}集合可能为空的{@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> Opt<List<T>> ofEmptyAble(List<T> value) {
|
public static <T, R extends Collection<T>> Opt<R> ofEmptyAble(R value) {
|
||||||
return CollectionUtil.isEmpty(value) ? empty() : new Opt<>(value);
|
return CollUtil.isEmpty(value) ? empty() : new Opt<>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 传入一段操作,包裹异常
|
||||||
*
|
*
|
||||||
* @param supplier 操作
|
* @param supplier 操作
|
||||||
* @param <T> 类型
|
* @param <T> 类型
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
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;
|
||||||
@ -60,4 +62,15 @@ 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,5 +1,7 @@
|
|||||||
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;
|
||||||
@ -34,5 +36,12 @@ 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