IN
- the input type of processorsOUT
- the output type of processors@Immutable public final class ProcessorSelector<IN extends MessageProvider,OUT extends MessageProvider> extends Object
This class allows you pair processors with a set of Predicate
s.
Internally, it uses a LinkedHashMap
where keys are predicates on
the input type IN
, and values are processors to use if this
predicate returns true
.
As it is a LinkedHashMap
, order matters: the first added predicate
will be evaluated first, etc. If no predicate evaluates to true
, the
default action takes place. Depending on whether you have set a default
processor, this processor will be selected, or a ProcessingException
will be thrown indicating that no appropriate selector could be found for the
input.
Sample usage:
final Processor<X, Y> processor = new ProcessorSelector<X, Y>() .when(predicate1).then(processor1) .when(predicate2).then(processor2) .otherwise(byDefault) .getProcessor();
The returned processor is immutable.
ProcessorSelectorPredicate
Constructor and Description |
---|
ProcessorSelector()
Constructor
|
Modifier and Type | Method and Description |
---|---|
Processor<IN,OUT> |
getProcessor()
Build the processor from this selector
|
ProcessorSelector<IN,OUT> |
otherwise(Processor<IN,OUT> byDefault)
Set a default processor
|
ProcessorSelectorPredicate<IN,OUT> |
when(com.google.common.base.Predicate<IN> predicate)
Add a predicate
|
public ProcessorSelectorPredicate<IN,OUT> when(com.google.common.base.Predicate<IN> predicate)
predicate
- the predicate to addProcessorSelectorPredicate
NullPointerException
- the predicate is nullpublic ProcessorSelector<IN,OUT> otherwise(Processor<IN,OUT> byDefault)
byDefault
- the default processorNullPointerException
- default processor is nullCopyright © 2016. All rights reserved.