All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.logbook.Fold Maven / Gradle / Ivy

package org.zalando.logbook;

import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

final class Fold {

    private Fold() {

    }

    static  R fold(
            final Collection collection,
            final R seed,
            final BiFunction accumulator) {

        return collection.stream()
                .reduce(seed, accumulator, throwingCombiner());
    }

    @SuppressWarnings("unchecked")
    private static  BinaryOperator throwingCombiner() {
        return (BinaryOperator) NoCombiner.NONE;
    }

    // visible for testing
    enum NoCombiner implements BinaryOperator {
        NONE;

        @Override
        public Object apply(final Object left, final Object right) {
            throw new UnsupportedOperationException();
        }
    }

}