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

io.activej.async.function.AsyncSupplier Maven / Gradle / Ivy

Go to download

A convenient way to organize asynchronous code. Promises are a faster and more efficient version of JavaScript's Promise and Java's CompletionStage's.

There is a newer version: 6.0-beta2
Show newest version
/*
 * 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 iterator) {
		return () -> Promise.of(iterator.hasNext() ? iterator.next() : null);
	}

	static  AsyncSupplier ofStream(@NotNull Stream stream) {
		return ofIterator(stream.iterator());
	}

	static  AsyncSupplier ofIterable(@NotNull Iterable iterable) {
		return ofIterator(iterable.iterator());
	}

	static  AsyncSupplier ofPromise(@NotNull Promise promise) {
		return () -> promise;
	}

	static  AsyncSupplier ofPromiseIterator(@NotNull Iterator> iterator) {
		return () -> iterator.hasNext() ? iterator.next() : Promise.of(null);
	}

	static  AsyncSupplier ofPromiseIterable(@NotNull Iterable> iterable) {
		return ofPromiseIterator(iterable.iterator());
	}

	static  AsyncSupplier ofPromiseStream(@NotNull Stream> stream) {
		return ofPromiseIterator(stream.iterator());
	}

	static  AsyncSupplier ofAsyncSupplierIterator(@NotNull Iterator> iterator) {
		return () -> iterator.hasNext() ? iterator.next().get() : Promise.of(null);
	}

	static  AsyncSupplier ofAsyncSupplierIterable(@NotNull Iterable> iterable) {
		return ofAsyncSupplierIterator(iterable.iterator());
	}

	static  AsyncSupplier ofAsyncSupplierStream(@NotNull Stream> 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 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 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> fn) {
		return () -> get().then(fn);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy