Coverage Report - org.apache.camel.builder.ChoiceBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoiceBuilder
89% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.camel.Predicate;
 23  
 import org.apache.camel.Processor;
 24  
 import org.apache.camel.processor.ChoiceProcessor;
 25  
 import org.apache.camel.processor.FilterProcessor;
 26  
 
 27  
 /**
 28  
  * @version $Revision: 532790 $
 29  
  */
 30  
 public class ChoiceBuilder extends FromBuilder {
 31  
 
 32  
     private final FromBuilder parent;
 33  5
     private List<WhenBuilder> predicateBuilders = new ArrayList<WhenBuilder>();
 34  
     private FromBuilder otherwise;
 35  
 
 36  
     public ChoiceBuilder(FromBuilder parent) {
 37  5
         super(parent);
 38  5
         this.parent = parent;
 39  5
     }
 40  
 
 41  
     /**
 42  
      * Adds a predicate which if it is true then the message exchange is sent to the given destination
 43  
      *
 44  
      * @return a builder for creating a when predicate clause and action
 45  
      */
 46  
     @Fluent(nestedActions=true)
 47  
     public WhenBuilder when(
 48  
                     @FluentArg(value="predicate",element=true) 
 49  
                     Predicate predicate) {
 50  10
         WhenBuilder answer = new WhenBuilder(this, predicate);
 51  10
         predicateBuilders.add(answer);
 52  10
         return answer;
 53  
     }
 54  
 
 55  
     @Fluent(nestedActions=true)
 56  
     public FromBuilder otherwise() {
 57  5
         this.otherwise = new FromBuilder(parent);
 58  5
         return otherwise;
 59  
     }
 60  
 
 61  
     public List<WhenBuilder> getPredicateBuilders() {
 62  0
         return predicateBuilders;
 63  
     }
 64  
 
 65  
     public FromBuilder getOtherwise() {
 66  0
         return otherwise;
 67  
     }
 68  
 
 69  
     @Override
 70  
     public Processor createProcessor() throws Exception {
 71  5
         List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
 72  5
         for (WhenBuilder predicateBuilder : predicateBuilders) {
 73  10
             filters.add(predicateBuilder.createProcessor());
 74  10
         }
 75  5
         Processor otherwiseProcessor = null;
 76  5
         if (otherwise != null) {
 77  5
             otherwiseProcessor = otherwise.createProcessor();
 78  
         }
 79  5
         return new ChoiceProcessor(filters, otherwiseProcessor);
 80  
     }
 81  
 }