<commit_before>/*
 * Copyright (C) <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package no.digipost.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import static no.digipost.DiggBase.friendlyName;

/**
 * A stream which is guarantied to produce at least one element. It provides
 * some extensions to the general {@link Stream} API for processing the stream knowing
 * it is non-empty, including, but not limited to:
 * <ul>
 *   <li>{@link #first()} and {@link #any()}</li>
 *   <li>{@link #limitToNonEmpty(long)}</li>
 *   <li>{@link #collect(EmptyResultIfEmptySourceCollector)}</li>
 *   <li>{@link #reduceFromFirst(BinaryOperator)}</li>
 * </ul>
 *
 * A selection of operations which maintains the cardinality of the stream returns {@code NonEmptyStream},
 * such as:
 * <ul>
 *   <li>{@link #map(Function)}
 * </ul>
 * This selection may be extended in the future. For operations which can not guarantie the
 * result will still be a non-empty stream, such as {@code filter} and {@code limit}, will yield a
 * regular {@link Stream}.
 *
 *
 * @param <T> the type of the stream elements
 */
public class NonEmptyStream<T> implements Stream<T> {

    public static <T> NonEmptyStream<T> of(T singleElement) {
        return of(singleElement, Stream.empty());
    }

    @SafeVarargs
    public static <T> NonEmptyStream<T> of(T firstElement, T ... remainingElements) {
        return of(firstElement, Arrays.stream(remainingElements));
    }

    public static <T> NonEmptyStream<T> of(T firstElement, Stream<T> remainingElements) {
        return of((Supplier<T>) () -> firstElement, remainingElements);
    }

    public static <T> NonEmptyStream<T> of(Supplier<? extends T> firstElement, Stream<T> remainingElements) {
        return new NonEmptyStream<>(firstElement, remainingElements);
    }

    /**
     * Create the same stream as produced by {@link Stream#iterate(Object, UnaryOperator)},
     * but typed as {@link NonEmptyStream}.
     *
     * @param <T> the type of stream elements
     * @param seed the initial element
     * @param f a function to be applied to to the previous element to produce a new element
     *
     * @return the new infinite non-empty stream
     *
     * @see Stream#iterate(Object, UnaryOperator)
     */
    public static <T> NonEmptyStream<T> iterate(T seed, UnaryOperator<T> f) {
        return new NonEmptyStream<>(Stream.iterate(seed, f));
    }
<commit_msg>Add NonEmptyStream variant of Stream.generate<commit_after>/*
 * Copyright (C) <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package no.digipost.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import static no.digipost.DiggBase.friendlyName;

/**
 * A stream which is guarantied to produce at least one element. It provides
 * some extensions to the general {@link Stream} API for processing the stream knowing
 * it is non-empty, including, but not limited to:
 * <ul>
 *   <li>{@link #first()} and {@link #any()}</li>
 *   <li>{@link #limitToNonEmpty(long)}</li>
 *   <li>{@link #collect(EmptyResultIfEmptySourceCollector)}</li>
 *   <li>{@link #reduceFromFirst(BinaryOperator)}</li>
 * </ul>
 *
 * A selection of operations which maintains the cardinality of the stream returns {@code NonEmptyStream},
 * such as:
 * <ul>
 *   <li>{@link #map(Function)}
 * </ul>
 * This selection may be extended in the future. For operations which can not guarantie the
 * result will still be a non-empty stream, such as {@code filter} and {@code limit}, will yield a
 * regular {@link Stream}.
 *
 *
 * @param <T> the type of the stream elements
 */
public class NonEmptyStream<T> implements Stream<T> {

    public static <T> NonEmptyStream<T> of(T singleElement) {
        return of(singleElement, Stream.empty());
    }

    @SafeVarargs
    public static <T> NonEmptyStream<T> of(T firstElement, T ... remainingElements) {
        return of(firstElement, Arrays.stream(remainingElements));
    }

    public static <T> NonEmptyStream<T> of(T firstElement, Stream<T> remainingElements) {
        return of((Supplier<T>) () -> firstElement, remainingElements);
    }

    public static <T> NonEmptyStream<T> of(Supplier<? extends T> firstElement, Stream<T> remainingElements) {
        return new NonEmptyStream<>(firstElement, remainingElements);
    }

    /**
     * Create the same stream as produced by {@link Stream#iterate(Object, UnaryOperator)},
     * but typed as {@link NonEmptyStream}.
     *
     * @param <T> the type of stream elements
     * @param seed the initial element
     * @param f a function to be applied to to the previous element to produce a new element
     *
     * @return the new infinite non-empty stream
     *
     * @see Stream#iterate(Object, UnaryOperator)
     */
    public static <T> NonEmptyStream<T> iterate(T seed, UnaryOperator<T> f) {
        return new NonEmptyStream<>(Stream.iterate(seed, f));
    }

    /**
     * Create the same stream as produced by {@link Stream#generate(Supplier)},
     * but typed as {@link NonEmptyStream}.
     *
     * @param <T> the type of stream elements
     * @param s the {@code Supplier} of generated elements
     *
     * @return a new infinite non-empty stream
     *
     * @see Stream#generate(Supplier)
     */
    public static<T> NonEmptyStream<T> generate(Supplier<T> s) {
        return new NonEmptyStream<>(Stream.generate(s));
    }

{'fraction_non_alphanumeric': 0.10977157360406091, 'fraction_numerical': 0.0010152284263959391, 'mean_word_length': 3.925625, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895477', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2227, 'n_words': 765}