将Java CompletionStage转换为等效的Scala
我正在尝试将下面的java代码转换为它的scala等价物将Java CompletionStage转换为等效的Scala,java,scala,Java,Scala,我正在尝试将下面的java代码转换为它的scala等价物 CompletionStage<GetXByKeyResponse> apiResponseF = appleClient.getAppleProperty(key); CompletionStage<XContainer> xContainerF = apiResponseF.thenCompose(resp -> resp.fold( CompletableFuture::completedFu
CompletionStage<GetXByKeyResponse> apiResponseF = appleClient.getAppleProperty(key);
CompletionStage<XContainer> xContainerF = apiResponseF.thenCompose(resp -> resp.fold(
CompletableFuture::completedFuture,
errorNotFound -> Futures.failedFuture(new Exception("some error")),
errorInternalServerError -> Futures.failedFuture(new Exception("some error"))
));
return xContainerF;
折叠的定义
方法:
import java.util.function.Function;
public <T> T fold(Function<XContainer, T> handleOk,
Function<ServiceErrorResponse, T> handleNotFound,
Function<ServiceErrorResponse, T> handleInternalServerError) {
if (this instanceof Ok) {
return handleOk.apply(((Ok) this).getValue());
} else if (this instanceof NotFound) {
return handleNotFound.apply(((NotFound) this).getValue());
} else if (this instanceof InternalServerError) {
return handleInternalServerError.apply(((InternalServerError) this).getValue());
} else {
throw new AssertionError("This is a bug!");
}
}
import java.util.function.function;
公共T形折叠(功能手柄),
函数handleNotFound,
函数handleInternalServerError){
如果(此实例为Ok){
return handleOk.apply(((Ok)this.getValue());
}else if(未找到此实例){
return handleNotFound.apply(((NotFound)this.getValue());
}else if(此实例为InternalServerError){
返回handleInternalServerError.apply(((InternalServerError)this.getValue());
}否则{
抛出新的断言错误(“这是一个bug!”);
}
}
import java.util.function.Function;
public <T> T fold(Function<XContainer, T> handleOk,
Function<ServiceErrorResponse, T> handleNotFound,
Function<ServiceErrorResponse, T> handleInternalServerError) {
if (this instanceof Ok) {
return handleOk.apply(((Ok) this).getValue());
} else if (this instanceof NotFound) {
return handleNotFound.apply(((NotFound) this).getValue());
} else if (this instanceof InternalServerError) {
return handleInternalServerError.apply(((InternalServerError) this).getValue());
} else {
throw new AssertionError("This is a bug!");
}
}