com.amplitude.MiddlewareRunner Maven / Gradle / Ivy
The newest version!
package com.amplitude;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
public class MiddlewareRunner {
private final ConcurrentLinkedQueue middlewares;
public MiddlewareRunner() {
middlewares = new ConcurrentLinkedQueue<>();
}
public void add(Middleware middleware) {
this.middlewares.add(middleware);
}
private void runMiddlewares(List middlewares, MiddlewarePayload payload, MiddlewareNext next) {
if (middlewares.size() == 0) {
next.run(payload);
return;
}
middlewares.get(0).run(payload,
curPayload -> runMiddlewares((middlewares.subList(1, middlewares.size())), curPayload, next));
}
public boolean run(MiddlewarePayload payload) {
AtomicBoolean middlewareCompleted = new AtomicBoolean(false);
this.run(payload,
curPayload -> middlewareCompleted.set(true));
return middlewareCompleted.get();
}
public void run(MiddlewarePayload payload, MiddlewareNext next) {
List middlewareList = new ArrayList<>(this.middlewares);
runMiddlewares(middlewareList, payload, next);
}
}