com.nike.moirai.Suppliers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moirai-core Show documentation
Show all versions of moirai-core Show documentation
A feature-flag and resource-reloading library for the JVM
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
}
}