io.datakernel.async.AsyncPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-eventloop Show documentation
Show all versions of datakernel-eventloop Show documentation
Efficient non-blocking network and file I/O, for building Node.js-like client/server applications with
high performance requirements. It is similar to Event Loop in Node.js.
Although Eventloop runs in a single thread, multiple event loops can be
launched at the same time allowing for efficient CPU usage.
package io.datakernel.async;
import java.util.Objects;
import java.util.function.Predicate;
public interface AsyncPredicate {
Stage test(T t);
default AsyncPredicate negate() {
return t -> this.test(t).thenApply(b -> !b);
}
default AsyncPredicate and(AsyncPredicate super T> other) {
Objects.requireNonNull(other);
return t -> this.test(t).thenCompose(b -> b ? other.test(t) : Stage.of(Boolean.FALSE));
}
default AsyncPredicate and(Predicate super T> other) {
Objects.requireNonNull(other);
return t -> this.test(t).thenApply(b -> b ? other.test(t) : Boolean.FALSE);
}
default AsyncPredicate or(Predicate super T> other) {
Objects.requireNonNull(other);
return t -> this.test(t).thenApply(b -> b ? Boolean.TRUE : other.test(t));
}
default AsyncPredicate or(AsyncPredicate super T> other) {
Objects.requireNonNull(other);
return t -> this.test(t).thenCompose(b -> b ? Stage.of(Boolean.TRUE) : other.test(t));
}
static AsyncPredicate of(Predicate predicate) {
return t -> Stage.of(predicate.test(t));
}
static AsyncPredicate alwaysTrue() {
return t -> Stage.of(Boolean.TRUE);
}
static AsyncPredicate alwaysFalse() {
return t -> Stage.of(Boolean.FALSE);
}
}