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

com.github.thorbenkuck.network.pipeline.Branch Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
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);
			}
		}
	}

}