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

org.smallibs.concurrent.promise.impl.SolvedPromise Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
/*
 * HPAS
 * https://github.com/d-plaindoux/hpas
 *
 * Copyright (c) 2016-2017 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */

package org.smallibs.concurrent.promise.impl;

import org.smallibs.data.Try;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

public final class SolvedPromise extends AbstractPromise {

    private final Try value;

    public SolvedPromise(Try value) {
        this.value = value;
    }

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

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

            @Override
            public boolean isDone() {
                return true;
            }

            @Override
            public T get() throws InterruptedException, ExecutionException {
                return value.orElseThrow(t -> new ExecutionException(t));
            }

            @Override
            public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                return get();
            }
        };
    }

    @Override
    public void onSuccess(Consumer consumer) {
        value.onSuccess(consumer);
    }

    @Override
    public void onFailure(Consumer consumer) {
        value.onFailure(consumer);
    }

    @Override
    public void onComplete(Consumer> consumer) {
        consumer.accept(value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy