001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pipeline.stage;
019    
020    import org.apache.commons.pipeline.StageException;
021    import org.apache.commons.pipeline.event.PipelineShutdownRequest;
022    
023    /**
024     *
025     *
026     */
027    public class PipelineShutdownStage extends BaseStage {
028        
029        private int numberOfObjects = 1;
030        private int count;
031        
032        /** Creates a new instance of PipelineShutdownStage */
033        public PipelineShutdownStage() {
034            super();
035        }
036        
037        /** Creates a new instance of PipelineShutdownStage
038         *@param numberOfObjects The number of objects to process before shutting down.
039         */
040        public PipelineShutdownStage(int numberOfObjects){
041            this.numberOfObjects = numberOfObjects;
042        }
043    
044        /** Maintains a count of objects.  If the count equals or exceeds the numberOfObjects
045         * then the pipeline is shut down.
046         *@param obj The objects.
047         */
048        public void process(Object obj) throws StageException {
049            this.emit(obj);
050            if (count++ >= numberOfObjects){
051                context.raise(new PipelineShutdownRequest(this, "Maximum of " + count + " objects processed."));
052            }
053        }
054        
055    }