
org.smallibs.concurrent.promise.impl.SolvedPromise Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hpas Show documentation
Show all versions of hpas Show documentation
Functional ADT And Asynchronous library in Java
/*
* 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