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

org.smallibs.concurrent.promise.impl.FlatMappedPromise 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.concurrent.promise.Promise;
import org.smallibs.data.Try;

import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;

final class FlatMappedPromise extends AbstractPromise {

    private final Promise promise;
    private final Function> transform;

    FlatMappedPromise(Promise promise, Function> transform) {
        super();

        this.promise = promise;
        this.transform = transform;
    }

    @Override
    public Future getFuture() {
        return new FlatMappedFuture<>(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 ->
                value.map(transform).
                onSuccess(o -> o.onComplete(consumer)).
                onFailure(t -> consumer.accept(Try.failure(t))));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy