com.github.fge.jsonschema.core.processing
Class ProcessorSelector<IN extends MessageProvider,OUT extends MessageProvider>

java.lang.Object
  extended by com.github.fge.jsonschema.core.processing.ProcessorSelector<IN,OUT>
Type Parameters:
IN - the input type of processors
OUT - the output type of processors

@Immutable
public final class ProcessorSelector<IN extends MessageProvider,OUT extends MessageProvider>
extends Object

A processor selector using predicates

This class allows you pair processors with a set of Predicates. 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.

See Also:
ProcessorSelectorPredicate

Constructor Summary
ProcessorSelector()
          Constructor
 
Method Summary
 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(Predicate<IN> predicate)
          Add a predicate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProcessorSelector

public ProcessorSelector()
Constructor

Method Detail

when

public ProcessorSelectorPredicate<IN,OUT> when(Predicate<IN> predicate)
Add a predicate

Parameters:
predicate - the predicate to add
Returns:
a ProcessorSelectorPredicate
Throws:
NullPointerException - the predicate is null

otherwise

public ProcessorSelector<IN,OUT> otherwise(Processor<IN,OUT> byDefault)
Set a default processor

Parameters:
byDefault - the default processor
Returns:
a new selector
Throws:
NullPointerException - default processor is null

getProcessor

public Processor<IN,OUT> getProcessor()
Build the processor from this selector

The returned processor is immutable: reusing this selector will not affect the result of this method in any way.

Returns:
the selector