com.bazoud.elasticsearch.river.git.guava.FunctionFlow Maven / Gradle / Ivy
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 {
private static ESLogger logger = Loggers.getLogger(FunctionFlow.class);
private ImmutableList> 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 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 FunctionFlowBuilder flow() {
return new FunctionFlowBuilder();
}
public static class FunctionFlowBuilder {
private List> functions = new ArrayList>();
public FunctionFlow build() {
return new FunctionFlow(this);
}
public FunctionFlowBuilder add(Function function) {
functions.add(function);
return this;
}
}
}