
org.smallibs.concurrent.promise.impl.AbstractPromise 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.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 super T, R> function) {
Objects.requireNonNull(function);
return new MappedPromise<>(this, function);
}
@Override
public final Promise flatmap(Function super T, Promise> function) {
Objects.requireNonNull(function);
return new FlatMappedPromise<>(this, function);
}
@Override
public HK> filter(Predicate super T> 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