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

io.github.yawenok.apns.http2.impl.model.ResponseFuture Maven / Gradle / Ivy

package io.github.yawenok.apns.http2.impl.model;

import io.github.yawenok.apns.http2.NotificationResponse;

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

public class ResponseFuture implements Future {
    private CountDownLatch latch = new CountDownLatch(1);

    private NotificationResponse response = null;

    private Exception exception = null;

    public ResponseFuture() {
    }

    public ResponseFuture(Exception exception) {
        this.exception = exception;
    }

    public void setResponse(NotificationResponse response) {
        this.response = response;
        this.latch.countDown();
    }

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

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

    @Override
    public boolean isDone() {
        if (response != null) {
            return true;
        }
        return false;
    }

    @Override
    public NotificationResponse get() throws InterruptedException {
        if (exception != null) {
            throw new InterruptedException("Interrupt by error!" + exception.getMessage());
        }
        if (response == null) {
            latch.await();
        }
        return response;
    }

    @Override
    public NotificationResponse get(long timeout, TimeUnit unit) throws InterruptedException {
        if (exception != null) {
            throw new InterruptedException("Interrupt by error!" + exception.getMessage());
        }
        if (response == null) {
            latch.await(timeout, unit);
        }
        return response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy