io.activej.async.function.AsyncSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activej-promise Show documentation
Show all versions of activej-promise Show documentation
A convenient way to organize asynchronous code.
Promises are a faster and more efficient version of JavaScript's Promise and Java's CompletionStage's.
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.activej.async.function;
import io.activej.async.callback.Callback;
import io.activej.async.process.AsyncExecutor;
import io.activej.common.collection.Try;
import io.activej.promise.Promise;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Represents asynchronous supplier that returns {@link Promise} of some data.
*/
@FunctionalInterface
public interface AsyncSupplier {
/**
* Gets {@link Promise} of data item asynchronously.
*/
Promise get();
static AsyncSupplier of(@NotNull Supplier> supplier) {
return supplier::get;
}
static AsyncSupplier ofValue(@Nullable T value) {
return () -> Promise.of(value);
}
static AsyncSupplier ofIterator(@NotNull Iterator extends T> iterator) {
return () -> Promise.of(iterator.hasNext() ? iterator.next() : null);
}
static AsyncSupplier ofStream(@NotNull Stream extends T> stream) {
return ofIterator(stream.iterator());
}
static AsyncSupplier ofIterable(@NotNull Iterable extends T> iterable) {
return ofIterator(iterable.iterator());
}
static AsyncSupplier ofPromise(@NotNull Promise promise) {
return () -> promise;
}
static AsyncSupplier ofPromiseIterator(@NotNull Iterator extends Promise> iterator) {
return () -> iterator.hasNext() ? iterator.next() : Promise.of(null);
}
static AsyncSupplier ofPromiseIterable(@NotNull Iterable extends Promise> iterable) {
return ofPromiseIterator(iterable.iterator());
}
static AsyncSupplier ofPromiseStream(@NotNull Stream extends Promise> stream) {
return ofPromiseIterator(stream.iterator());
}
static AsyncSupplier ofAsyncSupplierIterator(@NotNull Iterator extends AsyncSupplier> iterator) {
return () -> iterator.hasNext() ? iterator.next().get() : Promise.of(null);
}
static AsyncSupplier ofAsyncSupplierIterable(@NotNull Iterable extends AsyncSupplier> iterable) {
return ofAsyncSupplierIterator(iterable.iterator());
}
static AsyncSupplier ofAsyncSupplierStream(@NotNull Stream extends AsyncSupplier> stream) {
return ofAsyncSupplierIterator(stream.iterator());
}
@Contract(pure = true)
@NotNull
default R transformWith(@NotNull Function, R> fn) {
return fn.apply(this);
}
/**
* Ensures that supplied {@code Promise} will complete asynchronously.
*
* @return {@link AsyncSupplier} of {@code Promise}s
* that will be completed asynchronously
* @see Promise#async()
*/
@Contract(pure = true)
@NotNull
default AsyncSupplier async() {
return () -> get().async();
}
@Contract(pure = true)
@NotNull
default AsyncSupplier toVoid() {
return () -> get().toVoid();
}
@Contract(pure = true)
@NotNull
default AsyncSupplier> toTry() {
return () -> get().toTry();
}
@Contract(pure = true)
@NotNull
default AsyncSupplier withExecutor(@NotNull AsyncExecutor asyncExecutor) {
return () -> asyncExecutor.execute(this);
}
@Contract(pure = true)
@NotNull
default AsyncSupplier peek(@NotNull Consumer super T> action) {
return () -> get().whenResult(action);
}
@Contract(pure = true)
@NotNull
default AsyncSupplier peekEx(@NotNull Callback action) {
return () -> get().whenComplete(action);
}
/**
* Applies function before supplying a {@code Promise}.
*
* @param fn function to be applied to the result of {@code Promise}
* @return {@link AsyncSupplier} of {@code Promise}s after transformation
*/
@Contract(pure = true)
@NotNull
default AsyncSupplier map(@NotNull Function super T, ? extends V> fn) {
return () -> get().map(fn);
}
/**
* Applies function to the result of supplied {@code Promise}.
*
* @param fn function to be applied to the result of {@code Promise}
*/
@Contract(pure = true)
@NotNull
default AsyncSupplier mapAsync(@NotNull Function super T, ? extends Promise> fn) {
return () -> get().then(fn);
}
}