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

io.amient.affinity.core.util.JavaPromise Maven / Gradle / Ivy

Go to download

Library for building fast, scalable, fault-tolerant Data APIs based on Akka, ZooKeeper and Kafka.

There is a newer version: 0.15.2
Show newest version
package io.amient.affinity.core.util;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class JavaPromise implements Future {

    private final CountDownLatch latch = new CountDownLatch(1);
    private T value;
    private Exception exception;

    public void failure(Exception e) {
        exception = e;
        latch.countDown();
    }

    public void success(T result) {
        value = result;
        latch.countDown();
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return latch.getCount() == 0;
    }

    @Override
    public T get() throws InterruptedException {
        latch.await();
        if (exception != null) {
            throw new RuntimeException(exception);
        } else {
            return value;
        }
    }

    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
        if (latch.await(timeout, unit)) {
            return value;
        } else {
            throw new TimeoutException();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy