mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 20:27:58 +08:00
add CheckedUtil
This commit is contained in:
parent
d99ef79e29
commit
a3b9b0ac6b
@ -10,6 +10,7 @@
|
||||
* 【core 】 优化ArrayUtil.isAllEmpty性能(pr#2045@Github)
|
||||
* 【core 】 CharSequenceUtil.replace方法支持增补字符(pr#2041@Github)
|
||||
* 【extra 】 增加SshjSftp(pr#493@Gitee)
|
||||
* 【core 】 增加CheckedUtil(pr#491@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【http 】 HttpUtil重定向次数失效问题(issue#I4O28Q@Gitee)
|
||||
|
@ -38,6 +38,7 @@ import java.util.Objects;
|
||||
* </pre>
|
||||
*
|
||||
* @author conder
|
||||
* @since 5.7.19
|
||||
*/
|
||||
public class CheckedUtil {
|
||||
|
||||
@ -270,6 +271,7 @@ public class CheckedUtil {
|
||||
}
|
||||
|
||||
public interface FuncRt<P, R> extends Func<P, R> {
|
||||
@SuppressWarnings("unchecked")
|
||||
R call(P... parameters) throws RuntimeException;
|
||||
}
|
||||
|
||||
@ -282,6 +284,7 @@ public class CheckedUtil {
|
||||
}
|
||||
|
||||
public interface VoidFuncRt<P> extends VoidFunc<P> {
|
||||
@SuppressWarnings("unchecked")
|
||||
void call(P... parameters) throws RuntimeException;
|
||||
}
|
||||
|
||||
|
@ -29,18 +29,20 @@ public class CheckedUtilTest {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void supplierTest() {
|
||||
File noFile = new File("./no-file");
|
||||
try {
|
||||
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
|
||||
FileInputStream stream = CheckedUtil.uncheck(() -> new FileInputStream(noFile)).call();
|
||||
CheckedUtil.uncheck(() -> new FileInputStream(noFile)).call();
|
||||
} catch (Exception re) {
|
||||
Assert.assertTrue(re instanceof RuntimeException);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void functionTest() {
|
||||
Func1<String, String> afunc = (funcParam) -> {
|
||||
@ -55,7 +57,7 @@ public class CheckedUtilTest {
|
||||
|
||||
try {
|
||||
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
|
||||
String reslut = CheckedUtil.uncheck(afunc).call("hello world");
|
||||
CheckedUtil.uncheck(afunc).call("hello world");
|
||||
} catch (Exception re) {
|
||||
Assert.assertTrue(re instanceof RuntimeException);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user