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