org.testcontainers.utility.LazyFuture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testcontainers Show documentation
Show all versions of testcontainers Show documentation
Isolated container management for Java code testing
package org.testcontainers.utility;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.Delegate;
import org.rnorth.ducttape.timeouts.Timeouts;
import java.util.concurrent.*;
/**
* Future implementation with lazy result evaluation in the same Thread as caller.
*
* @param
*/
public abstract class LazyFuture implements Future {
@Delegate(excludes = Excludes.class)
private final Future delegate = CompletableFuture.completedFuture(null);
@Getter(value = AccessLevel.MODULE, lazy = true)
private final T resolvedValue = resolve();
abstract protected T resolve();
@Override
public T get() throws InterruptedException, ExecutionException {
return getResolvedValue();
}
@Override
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
try {
return Timeouts.getWithTimeout((int) timeout, unit, this::get);
} catch (org.rnorth.ducttape.TimeoutException e) {
throw new TimeoutException(e.getMessage());
}
}
private interface Excludes {
T get();
T get(long timeout, TimeUnit unit);
}
}