
org.smallibs.concurrent.promise.impl.MappedPromise 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.concurrent.promise.Promise;
import org.smallibs.data.Try;
import org.smallibs.util.FunctionWithError;
import org.smallibs.util.FunctionsWithError;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
final class MappedPromise extends AbstractPromise {
private final Promise promise;
private final Function super T, Try> transform;
MappedPromise(Promise promise, FunctionWithError super T, R> transform) {
this.promise = promise;
this.transform = FunctionsWithError.toFunction(transform);
}
@Override
public Future getFuture() {
return new MappedFuture<>(promise.getFuture(), transform);
}
@Override
public void onSuccess(final Consumer consumer) {
promise.onSuccess(t -> transform.apply(t).onSuccess(consumer));
}
@Override
public void onFailure(final Consumer consumer) {
promise.onFailure(consumer);
}
@Override
public void onComplete(Consumer> consumer) {
promise.onComplete(value -> consumer.accept(value.flatmap(transform).self()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy