Package com.bazoud.elasticsearch.river.git.guava

Source Code of com.bazoud.elasticsearch.river.git.guava.FunctionFlow$FunctionFlowBuilder

package com.bazoud.elasticsearch.river.git.guava;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;

import com.google.common.base.Function;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
* @author Olivier Bazoud
*/
public class FunctionFlow<C> {
    private static ESLogger logger = Loggers.getLogger(FunctionFlow.class);

    private ImmutableList<Function<C, C>> functions;

    public FunctionFlow(FunctionFlowBuilder functionFlowBuilder) {
        functions = ImmutableList.copyOf(functionFlowBuilder.functions);
    }

    public void apply(C context) {
        logger.info("Applying {} functions...", functions.size());
        Stopwatch stopwatch = new Stopwatch();
        for (Function<C, C> function : functions) {
            logger.info("Starting {} ...", function.getClass().getName());
            stopwatch.reset().start();
            context = function.apply(context);
            stopwatch.stop();
            logger.info("{} done. Tooks {} ms.", function.getClass().getName(), stopwatch.elapsed(MILLISECONDS));
        }
        logger.info("Apply done.");
    }

    public static <C> FunctionFlowBuilder flow() {
        return new FunctionFlowBuilder<C>();
    }

    public static class FunctionFlowBuilder<C> {
        private List<Function<C,C>> functions = new ArrayList<Function<C,C>>();

        public FunctionFlow build() {
            return new FunctionFlow<C>(this);
        }

        public FunctionFlowBuilder add(Function function) {
            functions.add(function);
            return this;
        }
    }

}
TOP

Related Classes of com.bazoud.elasticsearch.river.git.guava.FunctionFlow$FunctionFlowBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.