aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/GTStreamUtil.java
blob: 4b71fe5ee85ff91836ced943e53d5403ecd8f26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
    }
}