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

com.github.fge.jsonschema.processing.ProcessorChain Maven / Gradle / Ivy

/*
 * Copyright (c) 2013, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.github.fge.jsonschema.processing;

import com.github.fge.jsonschema.report.MessageProvider;
import com.github.fge.jsonschema.report.ProcessingReport;

/**
 * A processor chain
 *
 * 

This class allows to build a chain out of different {@link Processor} * instances. The result behaves like a processor itself, so it can be used in * other chains as well.

* *

Sample usage:

* *
 *     final Processor<X, Y> chain = ProcessorChain.startWith(p1)
 *         .chainWith(p2).chainWith(...).end();
 *
 *     // input is of type X
 *     final Y ret = chain.process(report, X);
 * 
* * @param the input type for that chain * @param the output type for that chain */ public final class ProcessorChain { /** * The resulting processor */ private final Processor processor; /** * Start a processing chain with a single processor * * @param p the processor * @param the input type * @param the output type * @return a single element processing chain */ public static ProcessorChain startWith(final Processor p) { return new ProcessorChain(p); } /** * Private constructor * * @param processor the processor */ private ProcessorChain(final Processor processor) { this.processor = processor; } public ProcessorChain failOnError() { final Processor fail = new Processor() { @Override public OUT process(final ProcessingReport report, final OUT input) throws ProcessingException { if (!report.isSuccess()) throw new ProcessingException("chain stopped"); return input; } }; return new ProcessorChain(merge(processor, fail)); } /** * Add an existing processor to that chain * *

Note that this returns a new chain.

* * @param p2 the processor to add * @param the return type for that new processor * @return a new chain consisting of the previous chain with the new * processor appended */ public ProcessorChain chainWith(final Processor p2) { return new ProcessorChain(merge(processor, p2)); } public Processor end() { return processor; } /** * Merge two processors * * @param p1 the first processor * @param p2 the second processor * @param the input type of {@code p1} * @param the output type of {@code p1} and input type of {@code p2} * @param the output type of {@code p2} * @return a processor resulting of applying {@code p2} to the output of * {@code p1} */ private static Processor merge(final Processor p1, final Processor p2) { return new Processor() { @Override public Z process(final ProcessingReport report, final X input) throws ProcessingException { return p2.process(report, p1.process(report, input)); } }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy