add CheckedUtil

This commit is contained in:
looly 2022-01-04 07:15:20 +08:00
parent d99ef79e29
commit a3b9b0ac6b
3 changed files with 8 additions and 2 deletions

View File

@ -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 】 增加SshjSftppr#493@Gitee * 【extra 】 增加SshjSftppr#493@Gitee
* 【core 】 增加CheckedUtilpr#491@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【http 】 HttpUtil重定向次数失效问题issue#I4O28Q@Gitee * 【http 】 HttpUtil重定向次数失效问题issue#I4O28Q@Gitee

View File

@ -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;
} }

View File

@ -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);
} }