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

com.norconex.commons.lang.pipeline.Pipeline Maven / Gradle / Ivy

Go to download

Norconex Commons Lang is a Java library containing utility classes that complements the Java API and are not found in commonly available libraries (such as the great Apache Commons Lang, which it relies on).

There is a newer version: 2.0.2
Show newest version
/* Copyright 2014-2016 Norconex Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.norconex.commons.lang.pipeline;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * Represent a very simple pipeline container for a list of executable tasks 
 * called "pipeline stages" (defined using
 * {@link IPipelineStage}).
 * This pipeline class can also be used a pipeline stage to create 
 * pipe hierarchies.
 * For more sophisticated work flow needs, consider using a more advanced 
 * framework such as Norconex JEF.
 * @author Pascal Essiembre
 * @param  pipeline context type
 * @since 1.5.0
 */
//TODO make it implement List?
public class Pipeline implements IPipelineStage {

    private static final Logger LOG = LogManager.getLogger(Pipeline.class);
    
    private final List> stages = new ArrayList<>();
    
    /**
     * Constructor.
     */
    public Pipeline() {
        super();
    }
    /**
     * Creates a new pipeline with the specified stages.
     * @param stages the stages to execute
     */
    public Pipeline(List> stages) {
        this.stages.addAll(stages);
    }

    /**
     * Gets the pipeline stages.
     * @return the pipeline stages
     */
    public List> getStages() {
        return Collections.unmodifiableList(stages);
    }

    /**
     * Adds stages to the pipeline.
     * @param stages pipeline stages to add
     * @return this instance, for chaining
     */
    public Pipeline addStages(List> stages) {
        this.stages.addAll(stages);
        return this;
    }
    /**
     * Adds a stage to the pipeline.
     * @param stage pipeline stage to add
     * @return this instance, for chaining
     */
    public Pipeline addStage(IPipelineStage stage) {
        this.stages.add(stage);
        return this;
    }

    public void clearStages() {
        stages.clear();
    }
    
    @Override
    public boolean execute(T context) {
        for (IPipelineStage stage : stages) {
            if (!stage.execute(context)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Pipeline execution stopped at stage: " + stage);
                }
                return false;
            }
        }
        return true;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy