<commit_before>package eu.goodlike.functional;

import eu.goodlike.neat.Null;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Utility methods to work with CompletableFutures
 */
public final class Futures {

    /**
     * <pre>
     * Suppose that you need to execute some kind of computation in a particular Thread, but the context of this Thread
     * would only accept Runnables, and you required it to return a value - this method does exactly that
     *
     * Inspired by Android's runOnUiThread(Runnable)
     * </pre>
     * @return value of a supplier, executed in given context, wrapped in a CompletableFuture
     */
    public static <T> CompletableFuture<T> valueFromSupplierAsRunnable(Consumer<Runnable> context, Supplier<T> supplier) {
        Null.check(context, supplier).ifAny("Context and supplier cannot be null");

        CompletableFuture<T> future = new CompletableFuture<>();
        context.accept(() -> future.complete(supplier.get()));
        return future;
    }

    // PRIVATE

    private Futures() {
        throw new AssertionError("Do not instantiate, use static methods!");
    }

}<commit_msg>Added some CompletableFuture utilities<commit_after>package eu.goodlike.functional;

import eu.goodlike.neat.Null;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Utility methods to work with CompletableFutures
 */
public final class Futures {

    /**
     * <pre>
     * Suppose that you need to execute some kind of computation in a particular Thread, but the context of this Thread
     * would only accept Runnables, and you required it to return a value - this method does exactly that
     *
     * Inspired by Android's runOnUiThread(Runnable)
     * </pre>
     * @return value of a supplier, executed in given context, wrapped in a CompletableFuture
     * @throws NullPointerException if context or supplier are null
     */
    public static <T> CompletableFuture<T> valueFromSupplierAsRunnable(Consumer<Runnable> context, Supplier<T> supplier) {
        Null.check(context, supplier).ifAny("Context and supplier cannot be null");

        CompletableFuture<T> future = new CompletableFuture<>();
        context.accept(() -> future.complete(supplier.get()));
        return future;
    }

    /**
     * @return CompletableFuture which has failed using given exception
     * @throws NullPointerException if throwable is null
     */
    public static <T> CompletableFuture<T> failedFuture(Throwable throwable) {
        Null.check(throwable).ifAny("Throwable cannot be null");

        CompletableFuture<T> failedFuture = new CompletableFuture<>();
        failedFuture.completeExceptionally(throwable);
        return failedFuture;
    }

    /**
     * @return BiConsumer, which handles the CompletableFuture::whenComplete method; if an error is present (not null)
     * the errorConsumer is called, otherwise the resultConsumer is called
     * @throws NullPointerException if resultConsumer or errorConsumer are null
     */
    public static <Result, Error> BiConsumer<Result, Error> completionHandler(Consumer<Result> resultConsumer,
                                                                              Consumer<Error> errorConsumer) {
        Null.check(resultConsumer, errorConsumer).ifAny("Result consumer and error consumer cannot be null");

        return (result, error) ->  {
            if (error == null)
                resultConsumer.accept(result);
            else
                errorConsumer.accept(error);
        };
    }

    // PRIVATE

    private Futures() {
        throw new AssertionError("Do not instantiate, use static methods!");
    }

}
{'fraction_non_alphanumeric': 0.08801253591015931, 'fraction_numerical': 0.0, 'mean_word_length': 3.0615058324496287, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '408799', 'n_tokens_mistral': 1007, 'n_tokens_neox': 973, 'n_words': 327}