All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testcontainers.utility.LazyFuture Maven / Gradle / Ivy

There is a newer version: 1.20.1
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy