在 Java 8 lambda 中使用本地 Vavr 不可变集合

Using local Vavr immutable collections inside a Java 8 lambda

鉴于下面的代码片段,我将如何使用 Vavr 不可变列表或流?我不能作为局部变量,因为无法将其标记为最终变量。我不想将 List<CurvePoint> points 提升为 class 会员。

import io.vavr.collection.List;

private RemoteFileTemplate template;

public <T> List<T> loadObjects(Class<T> type, InputStream stream) {...}

public List<CurvePoint> getCurvePoints(String path, FTPFile file) {
    final List<CurvePoint> points = List.empty();
    template.get(path + "/" + file.getName(), s -> {
        // The final local variable points cannot be assigned. It must be blank and not using a compound assignment
        points = points.appendAll(loadObjects(CurvePoint.class, s));
    });

    return points;
}

public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, InitializingBean, BeanFactoryAware {
    public boolean get(final String remotePath, final InputStreamCallback callback) {...}

这是 void doStuff(Callback<Whatever> callback) 语法的问题:难以进行单元测试,并将发射器 (RemoteFileTemplate) 与消费者 (回调代码) 紧密耦合。

如果签名改为 CompletableFuture<Whatever> doStuff(),那么您可以自由编写不可变代码(使用 Vavr 或其他任何东西)。然后,您可以本着 return List.ofAll(template.get(path + "/" + file.getName()).get()) 的精神编写同步代码,或 return template.get(path + "/" + file.getName()).thenApply(List::ofAll) 的异步代码。