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

com.nike.moirai.Suppliers Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package com.nike.moirai;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Utilities for composing {@link Supplier} instances.
 */
public class Suppliers {
    /**
     * Transforms the supplier by applying the given function to the results from the supplied {@link CompletableFuture}
     *
     * @param input supplier to transform
     * @param after the function to transform the future values
     * @param  the input value type
     * @param  the result value type
     * @return the transformed supplier
     */
    public static  Supplier> futureSupplierAndThen(Supplier> input, Function after) {
        return () -> input.get().thenApply(after);
    }

    /**
     * Transforms the supplier by applying the given function to the supplied result
     * @param input the supplier to transform
     * @param after the function to transform the supplied value
     * @param  the input value type
     * @param  the result value type
     * @return the transformed supplier
     */
    public static  Supplier supplierAndThen(Supplier input, Function after) {
        return () -> after.apply(input.get());
    }

    /**
     * Transforms a supplier of a value to a supplier of that value as CompletableFuture for use in an asynchronous context.
     *
     * @param supplier the supplier to transform
     * @param  the supplier value type
     * @return a supplier that wraps the value in a {@link CompletableFuture}
     */
    public static  Supplier> async(Supplier supplier) {
        return () -> CompletableFuture.supplyAsync(supplier);
    }

    private Suppliers() {
        // Prevent instantiation
    }
}