<commit_before>package org.javafunk.funk;

import static org.javafunk.funk.Eagerly.any;
import static org.javafunk.funk.Predicates.equalTo;

public class Checks {
    public static <T> T returnOrThrowIfNull(T value, RuntimeException exception) {
        return orThrowIf(value, value == null, exception);
    }

    public static <S, T extends Iterable<S>> T returnOrThrowIfEmpty(
            T value, RuntimeException exception) {
        return orThrowIf(
                value,
                !value.iterator().hasNext(),
                exception);
    }

    public static <S, T extends Iterable<S>> T returnOrThrowIfContainsNull(T iterable) {
        return orThrowIf(
                iterable,
                any(iterable, equalTo(null)),
                new NullPointerException()
        );
    }

    private static <T> T orThrowIf(T value, boolean condition, RuntimeException exception) {
        throwIf(condition, exception);
        return value;
    }

    private static void throwIf(boolean condition, RuntimeException exception) {
        if (condition) throw exception;
    }
}
<commit_msg>Cache NPE instance since exception creation is expensive.<commit_after>package org.javafunk.funk;

import static org.javafunk.funk.Eagerly.any;
import static org.javafunk.funk.Predicates.equalTo;

public class Checks {
    private static final NullPointerException NULL_POINTER_EXCEPTION = new NullPointerException();;

    public static <T> T returnOrThrowIfNull(T value, RuntimeException exception) {
        return orThrowIf(value, value == null, exception);
    }

    public static <S, T extends Iterable<S>> T returnOrThrowIfEmpty(
            T value, RuntimeException exception) {
        return orThrowIf(
                value,
                !value.iterator().hasNext(),
                exception);
    }

    public static <S, T extends Iterable<S>> T returnOrThrowIfContainsNull(T iterable) {
        return orThrowIf(
                iterable,
                any(iterable, equalTo(null)),
                NULL_POINTER_EXCEPTION);
    }

    private static <T> T orThrowIf(T value, boolean condition, RuntimeException exception) {
        throwIf(condition, exception);
        return value;
    }

    private static void throwIf(boolean condition, RuntimeException exception) {
        if (condition) throw exception;
    }
}

{'fraction_non_alphanumeric': 0.08326253186066271, 'fraction_numerical': 0.0, 'mean_word_length': 2.634259259259259, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1558518', 'n_tokens_mistral': 646, 'n_tokens_neox': 622, 'n_words': 177}