diff options
Diffstat (limited to 'src/main/java/gregtech/api/util/GTStreamUtil.java')
-rw-r--r-- | src/main/java/gregtech/api/util/GTStreamUtil.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/util/GTStreamUtil.java b/src/main/java/gregtech/api/util/GTStreamUtil.java new file mode 100644 index 0000000000..4b71fe5ee8 --- /dev/null +++ b/src/main/java/gregtech/api/util/GTStreamUtil.java @@ -0,0 +1,44 @@ +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 GTStreamUtil { + + /** + * Backport of {@link Stream#ofNullable}. + */ + public static <T> Stream<T> 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 <T> the type of stream elements + * @param values the elements of the new stream + * @return the new stream + */ + public static <T> Stream<T> 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 <T> the type of stream elements + * @param supplier the supplier for single stream element + * @return the new stream + */ + public static <T> Stream<T> ofSupplier(Supplier<T> supplier) { + return Stream.generate(supplier) + .limit(1); + } +} |