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

org.smallibs.concurrent.promise.impl.AbstractPromise 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.concurrent.promise.PromiseHelper;
import org.smallibs.exception.FilterException;
import org.smallibs.type.HK;
import org.smallibs.util.FunctionWithError;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

abstract class AbstractPromise implements Promise {

    AbstractPromise() {
    }

    @Override
    public  R accept(Function>, R> f) {
        return f.apply(this);
    }

    @Override
    public final  Promise map(FunctionWithError function) {
        Objects.requireNonNull(function);

        return new MappedPromise<>(this, function);
    }

    @Override
    public final  Promise flatmap(Function> function) {
        Objects.requireNonNull(function);

        return new FlatMappedPromise<>(this, function);
    }

    @Override
    public HK> filter(Predicate predicate) {
        return this.flatmap(t -> {
            if (predicate.test(t)) {
                return PromiseHelper.success(t);
            } else {
                return PromiseHelper.failure(new FilterException());
            }
        });
    }

    @Override
    final public Promise self() {
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy