<commit_before>/*
 * (C) Copyright 2016 <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 com.github.richardballard.arbeetestutils.test;

import com.github.richardballard.arbeecoretypes.function.ToBooleanFunction;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableCollection;
import net.jcip.annotations.ThreadSafe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mockito.stubbing.Answer;

import java.util.concurrent.Callable;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.mockito.AdditionalAnswers.returnsElementsOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ThreadSafe
public enum MoreMockUtils {
    ;

    /**
     * Returns a mock function that always returns {@code value}.
     */
    @SuppressWarnings("unchecked")
    @NotNull
    public static <K, V> Function<K, V> mockFunctionSingleAnswer(@NotNull final Class<? extends K> keyClass,
                                                                 @Nullable final V value) {
        assert keyClass != null;

        final Function<K, V> function = mock(Function.class);

        when(function.apply(null))
                .thenReturn(value);

        when(function.apply(any(keyClass)))
                .thenReturn(value);

        return function;
    }

    /**
     * Returns a mock function that returns the elements of {@code values}.
     */
    @SuppressWarnings("unchecked")
    @NotNull
    public static <K, V> Function<K, V> mockFunctionMultipleAnswers(@NotNull final Class<? extends K> keyClass,
                                                                    @NotNull final ImmutableCollection<V> values) {
        assert keyClass != null;
        assert values != null;

        final Function<K, V> function = mock(Function.class);

        // have the same answer instance so both calls return from the next list position
        final Answer<Object> answer = returnsElementsOf(values);

        when(function.apply(null))
                .thenAnswer(answer);

        when(function.apply(any(keyClass)))
                .thenAnswer(answer);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <V> Supplier<V> mockSupplierSingleAnswer(@Nullable final V value) {
        final Supplier<V> supplier = mock(Supplier.class);

        when(supplier.get())
                .thenReturn(value);

        return supplier;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <V> Supplier<V> mockSupplierMultipleAnswers(@NotNull final ImmutableCollection<V> values) {
        assert values != null;

        final Supplier<V> supplier = mock(Supplier.class);

        when(supplier.get())
                .thenAnswer(returnsElementsOf(values));

        return supplier;
    }

    @NotNull
    public static BooleanSupplier mockBooleanSupplierSingleAnswer(final boolean value) {
        final BooleanSupplier supplier = mock(BooleanSupplier.class);

        when(supplier.getAsBoolean())
                .thenReturn(value);

        return supplier;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> ToBooleanFunction<T> mockToBooleanFunctionSingleAnswer(@NotNull final Class<T> applyClass,
                                                                             final boolean value) {
        final ToBooleanFunction<T> function = mock(ToBooleanFunction.class);

        when(function.applyAsBoolean(null))
                .thenReturn(value);

        when(function.applyAsBoolean(any(applyClass)))
                .thenReturn(value);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> ToBooleanFunction<T> mockToBooleanFunctionMultipleAnswers(@NotNull final Class<T> applyClass,
                                                                                @NotNull final ImmutableCollection<Boolean> values) {
        assert values != null;

        final ToBooleanFunction<T> function = mock(ToBooleanFunction.class);

        // have the same answer instance so both calls return from the next list position
        final Answer<Object> answer = returnsElementsOf(values);

        when(function.applyAsBoolean(null))
                .thenAnswer(answer);

        when(function.applyAsBoolean(any(applyClass)))
                .thenAnswer(answer);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Callable<T> mockCallableSingleAnswer(@NotNull final T value) {
        assert value != null;

        final Callable<T> callable = mock(Callable.class);

        try {
            when(callable.call())
                    .thenReturn(value);
        }
        catch(final Exception exc) {
            // shouldn't happen - keeping compiler happy - RMB 2016/6/19
            throw Throwables.propagate(exc);
        }

        return callable;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Callable<T> mockCallableMultipleAnswers(@NotNull final ImmutableCollection<T> values) {
        assert values != null;

        final Callable<T> callable = mock(Callable.class);

        try {
            when(callable.call())
                    .thenAnswer(returnsElementsOf(values));
        }
        catch(final Exception exc) {
            // shouldn't happen - keeping compiler happy - RMB 2016/6/19
            throw Throwables.propagate(exc);
        }

        return callable;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Consumer<T> mockConsumer() {
        return mock(Consumer.class);
    }


}
<commit_msg>fixed up deprecated warnings<commit_after>/*
 * (C) Copyright 2016 <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 com.github.richardballard.arbeetestutils.test;

import com.github.richardballard.arbeecoretypes.function.ToBooleanFunction;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableCollection;
import net.jcip.annotations.ThreadSafe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mockito.stubbing.Answer;

import java.util.concurrent.Callable;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.mockito.AdditionalAnswers.returnsElementsOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ThreadSafe
public enum MoreMockUtils {
    ;

    /**
     * Returns a mock function that always returns {@code value}.
     */
    @SuppressWarnings("unchecked")
    @NotNull
    public static <K, V> Function<K, V> mockFunctionSingleAnswer(@NotNull final Class<? extends K> keyClass,
                                                                 @Nullable final V value) {
        assert keyClass != null;

        final Function<K, V> function = mock(Function.class);

        when(function.apply(null))
                .thenReturn(value);

        when(function.apply(any(keyClass)))
                .thenReturn(value);

        return function;
    }

    /**
     * Returns a mock function that returns the elements of {@code values}.
     */
    @SuppressWarnings("unchecked")
    @NotNull
    public static <K, V> Function<K, V> mockFunctionMultipleAnswers(@NotNull final Class<? extends K> keyClass,
                                                                    @NotNull final ImmutableCollection<V> values) {
        assert keyClass != null;
        assert values != null;

        final Function<K, V> function = mock(Function.class);

        // have the same answer instance so both calls return from the next list position
        final Answer<Object> answer = returnsElementsOf(values);

        when(function.apply(null))
                .thenAnswer(answer);

        when(function.apply(any(keyClass)))
                .thenAnswer(answer);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <V> Supplier<V> mockSupplierSingleAnswer(@Nullable final V value) {
        final Supplier<V> supplier = mock(Supplier.class);

        when(supplier.get())
                .thenReturn(value);

        return supplier;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <V> Supplier<V> mockSupplierMultipleAnswers(@NotNull final ImmutableCollection<V> values) {
        assert values != null;

        final Supplier<V> supplier = mock(Supplier.class);

        when(supplier.get())
                .thenAnswer(returnsElementsOf(values));

        return supplier;
    }

    @NotNull
    public static BooleanSupplier mockBooleanSupplierSingleAnswer(final boolean value) {
        final BooleanSupplier supplier = mock(BooleanSupplier.class);

        when(supplier.getAsBoolean())
                .thenReturn(value);

        return supplier;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> ToBooleanFunction<T> mockToBooleanFunctionSingleAnswer(@NotNull final Class<T> applyClass,
                                                                             final boolean value) {
        final ToBooleanFunction<T> function = mock(ToBooleanFunction.class);

        when(function.applyAsBoolean(null))
                .thenReturn(value);

        when(function.applyAsBoolean(any(applyClass)))
                .thenReturn(value);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> ToBooleanFunction<T> mockToBooleanFunctionMultipleAnswers(@NotNull final Class<T> applyClass,
                                                                                @NotNull final ImmutableCollection<Boolean> values) {
        assert values != null;

        final ToBooleanFunction<T> function = mock(ToBooleanFunction.class);

        // have the same answer instance so both calls return from the next list position
        final Answer<Object> answer = returnsElementsOf(values);

        when(function.applyAsBoolean(null))
                .thenAnswer(answer);

        when(function.applyAsBoolean(any(applyClass)))
                .thenAnswer(answer);

        return function;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Callable<T> mockCallableSingleAnswer(@NotNull final T value) {
        assert value != null;

        final Callable<T> callable = mock(Callable.class);

        try {
            when(callable.call())
                    .thenReturn(value);
        }
        catch(final Exception exc) {
            // shouldn't happen - keeping compiler happy - RMB 2016/6/19
            Throwables.throwIfUnchecked(exc);
            throw new RuntimeException(exc);
        }

        return callable;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Callable<T> mockCallableMultipleAnswers(@NotNull final ImmutableCollection<T> values) {
        assert values != null;

        final Callable<T> callable = mock(Callable.class);

        try {
            when(callable.call())
                    .thenAnswer(returnsElementsOf(values));
        }
        catch(final Exception exc) {
            // shouldn't happen - keeping compiler happy - RMB 2016/6/19
            Throwables.throwIfUnchecked(exc);
            throw new RuntimeException(exc);
        }

        return callable;
    }

    @SuppressWarnings("unchecked")
    @NotNull
    public static <T> Consumer<T> mockConsumer() {
        return mock(Consumer.class);
    }


}

{'fraction_non_alphanumeric': 0.09214513246833968, 'fraction_numerical': 0.003418537798150882, 'mean_word_length': 2.4288758657432075, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5530599', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3219, 'n_words': 865}