Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java CompletionStage转换为等效的Scala_Java_Scala - Fatal编程技术网

将Java CompletionStage转换为等效的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

我正在尝试将下面的java代码转换为它的scala等价物

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!");
            }
        }