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    package org.apache.camel.builder;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.Predicate;
023    import org.apache.camel.Processor;
024    import org.apache.camel.processor.ChoiceProcessor;
025    import org.apache.camel.processor.FilterProcessor;
026    
027    /**
028     * @version $Revision: 532790 $
029     */
030    public class ChoiceBuilder extends FromBuilder {
031    
032        private final FromBuilder parent;
033        private List<WhenBuilder> predicateBuilders = new ArrayList<WhenBuilder>();
034        private FromBuilder otherwise;
035    
036        public ChoiceBuilder(FromBuilder parent) {
037            super(parent);
038            this.parent = parent;
039        }
040    
041        /**
042         * Adds a predicate which if it is true then the message exchange is sent to the given destination
043         *
044         * @return a builder for creating a when predicate clause and action
045         */
046        @Fluent(nestedActions=true)
047        public WhenBuilder when(
048                    @FluentArg(value="predicate",element=true) 
049                    Predicate predicate) {
050            WhenBuilder answer = new WhenBuilder(this, predicate);
051            predicateBuilders.add(answer);
052            return answer;
053        }
054    
055        @Fluent(nestedActions=true)
056        public FromBuilder otherwise() {
057            this.otherwise = new FromBuilder(parent);
058            return otherwise;
059        }
060    
061        public List<WhenBuilder> getPredicateBuilders() {
062            return predicateBuilders;
063        }
064    
065        public FromBuilder getOtherwise() {
066            return otherwise;
067        }
068    
069        @Override
070        public Processor createProcessor() throws Exception {
071            List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
072            for (WhenBuilder predicateBuilder : predicateBuilders) {
073                filters.add(predicateBuilder.createProcessor());
074            }
075            Processor otherwiseProcessor = null;
076            if (otherwise != null) {
077                otherwiseProcessor = otherwise.createProcessor();
078            }
079            return new ChoiceProcessor(filters, otherwiseProcessor);
080        }
081    }