org.fiolino.common.processing.Processor Maven / Gradle / Ivy
Show all versions of commons Show documentation
package org.fiolino.common.processing;
import org.fiolino.annotations.PostProcessor;
/**
* These entities process an existing model bean.
*
* Created by kuli on 18.11.15.
*/
public interface Processor {
void process(S source, T model) throws Exception;
default > X find(Class type) {
return type.isInstance(this) ? type.cast(this) : null;
}
default Processor andThen(Processor super S, ? super T> next) {
return new MultiProcessor<>(this, next);
}
default Processor beforeDo(Processor previous) {
return previous.andThen(this);
}
@SuppressWarnings("unchecked")
static Processor doNothing() {
return (Processor) VoidProcessor.INSTANCE;
}
static T postProcess(T instance) {
if (instance instanceof PostProcessor) {
((PostProcessor) instance).postConstruct();
}
return instance;
}
class VoidProcessor implements Processor {
private static final VoidProcessor, ?> INSTANCE = new VoidProcessor<>();
private VoidProcessor() {
}
@Override
public void process(S source, T model) {
// Do nothing
}
@Override
@SuppressWarnings("unchecked")
public Processor andThen(Processor super S, ? super T> next) {
return (Processor) next;
}
@Override
public Processor beforeDo(Processor previous) {
return previous;
}
}
}