java-8 - 如何将方法签名从 void 更改为 CompletionStage?
问题描述
class AnotherClass {
Option<Integer> validateAndGetInt(Xyz xyz, Mno mno, String strValue) {
if (strValue != null) {
int intValue = 1;
try{
intValue = Integer.parseInt(strValue);
} catch (Exception e) {
throw new Exception("err");
}
Printer.getInstance().validateInt(xyz, mno, intValue);
return Optional.of(intValue);
}
}
} // class ends
class Printer {
void validateInt(Xyz xyz, Mno mno, int x) {
int m = xyz.getTeamVal(mno, x);
if(m < 0) {
throw new CustomException("invalid team value");
}
}
}
我计划从void
搬到CompletionStage<Void>
for validate(int x)
。
validate
将更改如下,但我不确定如何在调用方使用。
CompletionStage<Void> validateInt(Xyz xyz, Mno mno, int x) {
CompletableFuture<Void> f = new CompletableFuture<Void>();
int m = xyz.getTeamVal(mno, x);
if(m < 0) {
f.completeExceptionally(new CustomException("invalid team value"));
} else {
f.complete(null);
}
return f;
}
有人可以帮助解释我如何使用它AnotherClass
吗?
我不想尽可能多地使用它:
Printer.getInstance().validateInt(xyz, mno, intValue).toCompletableFuture().get()
解决方案
推荐阅读
- php - 遍历从 PHP 序列化格式解码的地图
- crystal-reports - 字符串类型字段上的多个范围过滤器会破坏日期范围过滤器
- c# - 在excel c#中解析日期时遇到问题
- ionic-framework - IONIC 对位于自定义组件中的图像使用另一个文件目录
- javascript - 返回javascript中动态范围之间的偶数和奇数
- yii - Yii1 - 在限制和偏移之前计数,查询全部
- php - 为什么这个三元运算符不起作用?
- java - 如何忽略 SQL 更新中的列
- jenkins - 如何在 Jenkins 中使用 Artifactory 插件向 Artifactory 中的现有工件添加递归属性
- excel - 熊猫为每个 Excel 工作表创建单独的数据框