Using local Vavr immutable collections inside a Java 8 lambda

578 Views Asked by At

Given the code fragment below, how would I use Vavr immutable lists or streams? I can't as a local variable since there is no way to mark it final. I don't want to promote List<CurvePoint> points to a class member.

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) {...}
1

There are 1 best solutions below

0
On BEST ANSWER

This is the problem with the void doStuff(Callback<Whatever> callback) syntax: hard to unit test, and tightly couples the emitter (RemoteFileTemplate) with the consumer (Callback code).

If instead the signature was CompletableFuture<Whatever> doStuff() then you could freely write immutable code (with Vavr or anything else). Then you could write something in the spirit of return List.ofAll(template.get(path + "/" + file.getName()).get()) for synchronous code, or return template.get(path + "/" + file.getName()).thenApply(List::ofAll) for asynchronous code.