com.github.thorbenkuck.network.pipeline.Branch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easy-net Show documentation
Show all versions of easy-net Show documentation
An EventStream driven, simple to use Client/Server framework
package com.github.thorbenkuck.network.pipeline;
import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Consumer;
import java.util.function.Function;
public class Branch {
private final Queue callQueue = new LinkedList<>();
public Stage add(Function function) {
callQueue.add(function);
return new PipelineStage<>(callQueue);
}
public Stage add(Consumer consumer) {
callQueue.add(new ConsumerWrapper<>(consumer));
return new PipelineStage<>(callQueue);
}
public Stage add(Runnable runnable) {
callQueue.add(new RunnableWrapper<>(runnable));
return new PipelineStage<>(callQueue);
}
public void propagate(T t) {
Queue copy = new LinkedList<>(callQueue);
Function current;
Object object = t;
while (copy.peek() != null) {
try {
current = copy.poll();
object = current.apply(object);
} catch (ClassCastException c) {
throw new IllegalStateException("[UNRECOVERABLE] Illegal Stage combination!", c);
}
}
}
}