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

com.strobel.core.Aggregate Maven / Gradle / Ivy

/*
 * Aggregate.java
 *
 * Copyright (c) 2012 Mike Strobel
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0.
 * A copy of the license can be found in the License.html file at the root of this distribution.
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the
 * Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 */

package com.strobel.core;

import com.strobel.util.ContractUtils;

/**
 * @author Mike Strobel
 */
public final class Aggregate {
    private Aggregate() {
        throw ContractUtils.unreachable();
    }

    public static  TAccumulate aggregate(
        final Iterable source,
        final Accumulator accumulator) {

        return aggregate(source, null, accumulator);
    }

    public static  TAccumulate aggregate(
        final Iterable source,
        final TAccumulate seed,
        final Accumulator accumulator) {

        VerifyArgument.notNull(source, "source");
        VerifyArgument.notNull(accumulator, "accumulator");

        TAccumulate accumulate = seed;

        for (final TSource item : source) {
            accumulate = accumulator.accumulate(accumulate, item);
        }

        return accumulate;
    }

    public static  TResult aggregate(
        final Iterable source,
        final Accumulator accumulator,
        final Selector resultSelector) {

        return aggregate(source, null, accumulator, resultSelector);
    }

    public static  TResult aggregate(
        final Iterable source,
        final TAccumulate seed,
        final Accumulator accumulator,
        final Selector resultSelector) {

        VerifyArgument.notNull(source, "source");
        VerifyArgument.notNull(accumulator, "accumulator");
        VerifyArgument.notNull(resultSelector, "resultSelector");

        TAccumulate accumulate = seed;

        for (final TSource item : source) {
            accumulate = accumulator.accumulate(accumulate, item);
        }

        return resultSelector.select(accumulate);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy