// Path: util/src/main/java/hm/binkley/util/function/ThrowingBinaryOperator.java
// @Nonnull
// static <T, E extends Exception> ThrowingBinaryOperator<T, E> maxBy(
//         @Nonnull final Comparator<? super T> comparator) {
//     return (a, b) -> 0 <= comparator.compare(a, b) ? a : b;
// }
// 
// Path: util/src/main/java/hm/binkley/util/function/ThrowingPredicate.java
// @Nonnull
// static <T> ThrowingPredicate<T, RuntimeException> isEqual(
//         final Object targetRef) {
//     return null == targetRef ? Objects::isNull : targetRef::equals;
// }
// 
// Path: util/src/main/java/hm/binkley/util/stream/CheckedStream.java
// @Nonnull
// public static <T> CheckedStream<T> checked(@Nonnull final Stream<T> stream) {
//     return new SequentialCheckedStream<>(stream);
// }


import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.nio.file.AccessDeniedException;
import java.security.AccessControlException;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.stream.Stream;
import static hm.binkley.util.function.ThrowingBinaryOperator.maxBy;
import static hm.binkley.util.function.ThrowingPredicate.isEqual;
import static hm.binkley.util.stream.CheckedStream.checked;
import static java.lang.System.out;
import static java.lang.Thread.currentThread;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
        checked(Stream.of(1)).
                parallel(threads).
                map(i -> currentThread()).
                forEach(t -> assertThat(t.getName(), is(equalTo("Foo!"))));
    }

    @Test
    public void shouldChangeToSequentialFromParallel()
            throws Exception {
        checked(Stream.of(1), new ForkJoinPool()).
                sequential().
                map(i -> currentThread()).
                forEach(t -> assertThat(t, is(sameInstance(currentThread()))));
    }

    @Test
    public void shouldTerminateForAnyMatchWhenSequential()
            throws InterruptedException {
        assertThat(checked(Stream.of(1, 2, 3)).<RuntimeException>anyMatch(isEqual(1)), is(true));
    }

    @Test
    public void shouldTerminateForAllMatchWhenSequential()
            throws InterruptedException {
        assertThat(checked(Stream.of(1, 2, 3)).<RuntimeException>anyMatch(i -> true), is(true));
    }

    @Test
    public void shouldTerminateForNoneMatchWhenSequential()
            throws InterruptedException {
// Path: util/src/main/java/hm/binkley/util/function/ThrowingBinaryOperator.java
// @Nonnull
// static <T, E extends Exception> ThrowingBinaryOperator<T, E> maxBy(
//         @Nonnull final Comparator<? super T> comparator) {
//     return (a, b) -> 0 <= comparator.compare(a, b) ? a : b;
// }
// 
// Path: util/src/main/java/hm/binkley/util/function/ThrowingPredicate.java
// @Nonnull
// static <T> ThrowingPredicate<T, RuntimeException> isEqual(
//         final Object targetRef) {
//     return null == targetRef ? Objects::isNull : targetRef::equals;
// }
// 
// Path: util/src/main/java/hm/binkley/util/stream/CheckedStream.java
// @Nonnull
// public static <T> CheckedStream<T> checked(@Nonnull final Stream<T> stream) {
//     return new SequentialCheckedStream<>(stream);
// }


// Path: util/src/test/java/hm/binkley/util/stream/CheckedStreamTest.java
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.nio.file.AccessDeniedException;
import java.security.AccessControlException;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.stream.Stream;
import static hm.binkley.util.function.ThrowingBinaryOperator.maxBy;
import static hm.binkley.util.function.ThrowingPredicate.isEqual;
import static hm.binkley.util.stream.CheckedStream.checked;
import static java.lang.System.out;
import static java.lang.Thread.currentThread;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;

        checked(Stream.of(1)).
                parallel(threads).
                map(i -> currentThread()).
                forEach(t -> assertThat(t.getName(), is(equalTo("Foo!"))));
    }

    @Test
    public void shouldChangeToSequentialFromParallel()
            throws Exception {
        checked(Stream.of(1), new ForkJoinPool()).
                sequential().
                map(i -> currentThread()).
                forEach(t -> assertThat(t, is(sameInstance(currentThread()))));
    }

    @Test
    public void shouldTerminateForAnyMatchWhenSequential()
            throws InterruptedException {
        assertThat(checked(Stream.of(1, 2, 3)).<RuntimeException>anyMatch(isEqual(1)), is(true));
    }

    @Test
    public void shouldTerminateForAllMatchWhenSequential()
            throws InterruptedException {
        assertThat(checked(Stream.of(1, 2, 3)).<RuntimeException>anyMatch(i -> true), is(true));
    }

    @Test
    public void shouldTerminateForNoneMatchWhenSequential()
            throws InterruptedException {
        assertThat(checked(Stream.of(1, 2, 3)).<RuntimeException>noneMatch(i -> false), is(true));