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.testFramework;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.commons.pipeline.Stage;
026    import org.apache.commons.pipeline.StageContext;
027    import org.apache.commons.pipeline.StageException;
028    import org.apache.commons.pipeline.validation.ConsumedTypes;
029    import org.apache.commons.pipeline.validation.ProducesConsumed;
030    
031    @ConsumedTypes(Object.class)
032    @ProducesConsumed
033    public class TestStage implements Stage {
034        private Log log = LogFactory.getLog(TestStage.class);
035        private int index;
036        private StageContext context;
037    
038        public List<Object> processedObjects = Collections.synchronizedList(new ArrayList<Object>());
039        public boolean initialized = false;
040        public boolean preprocessed = false;
041        public boolean postprocessed = false;
042        public boolean released = false;
043        
044        /**
045         * Construct a TestStage with a numeric index used to easily identify this stage. 
046         * The {@link getIndex} and {@link toString} methods use this index.
047         * @param index acts as an identification number
048         */
049        public TestStage(int index) {
050            this.index = index;
051        }
052        
053        public int getIndex() {
054            return this.index;
055        }
056    
057        public void init(StageContext context) {        
058            this.context = context;
059            this.initialized = true;
060        }
061        
062        public void preprocess() throws StageException {
063            this.preprocessed = true;
064        }
065    
066        public void process(Object obj) throws StageException {
067            log.info(this + " is processing object " + obj);
068            this.processedObjects.add(obj);
069            this.context.getDownstreamFeeder(this).feed(obj);
070        }
071        
072        public void postprocess() throws StageException {
073            this.postprocessed = true;
074        }
075    
076        public void release() {
077            this.released = true;
078        }
079    
080        public String toString() {
081            return "TEST STAGE (" + this.index + ")";
082        }
083    }