org.fiolino.common.processing.MultiProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
General structure to easily create dynamic logic via MethodHandles and others.
package org.fiolino.common.processing;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kuli on 25.11.15.
*/
public class MultiProcessor implements Processor {
private final List> processors = new ArrayList<>();
@SafeVarargs
public MultiProcessor(Processor super S, ? super T>... initial) {
for (Processor super S, ? super T> p : initial) {
processors.add(p);
}
}
public void addProcessor(Processor super S, ? super T> processor) {
processors.add(processor);
}
@Override
public > X find(Class type) {
for (Processor super S, ? super T> p : processors) {
if (type.isInstance(p)) {
return type.cast(p);
}
}
return null;
}
@Override
public Processor andThen(Processor super S, ? super T> next) {
addProcessor(next);
return this;
}
@Override
public Processor beforeDo(Processor previous) {
processors.add(0, previous);
return this;
}
@Override
public void process(S source, T model) throws Exception {
for (Processor super S, ? super T> p : processors) {
p.process(source, model);
}
}
}