package gregtech.api.util; import java.util.Arrays; import java.util.function.Supplier; import java.util.stream.Stream; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault public final class GT_StreamUtil { /** * Backport of {@link Stream#ofNullable}. */ public static Stream ofNullable(@Nullable T value) { return value == null ? Stream.empty() : Stream.of(value); } /** * Returns a sequential ordered {@code Stream} whose elements are the specified values, * if {@code condition} is true, otherwise returns an empty {@code Stream}. * * @param the type of stream elements * @param values the elements of the new stream * @return the new stream */ public static Stream ofConditional(boolean condition, T[] values) { return condition ? Arrays.stream(values) : Stream.empty(); } /** * Returns a sequential {@code Stream} containing a single element, which will be lazily evaluated from supplier. * * @param the type of stream elements * @param supplier the supplier for single stream element * @return the new stream */ public static Stream ofSupplier(Supplier supplier) { return Stream.generate(supplier) .limit(1); } }