Coverage Report - org.apache.camel.processor.ChoiceProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoiceProcessor
100% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor;
 19  
 
 20  
 import org.apache.camel.Predicate;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.util.ServiceHelper;
 24  
 import org.apache.camel.impl.ServiceSupport;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Implements a Choice structure where one or more predicates are used which if they are true their processors
 31  
  * are used, with a default otherwise clause used if none match.
 32  
  *
 33  
  * @version $Revision: 534145 $
 34  
  */
 35  
 public class ChoiceProcessor extends ServiceSupport implements Processor {
 36  5
     private List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
 37  
     private Processor otherwise;
 38  
 
 39  5
     public ChoiceProcessor(List<FilterProcessor> filters, Processor otherwise) {
 40  5
         this.filters = filters;
 41  5
         this.otherwise = otherwise;
 42  5
     }
 43  
 
 44  
     public void process(Exchange exchange) throws Exception {
 45  6
         for (FilterProcessor filterProcessor : filters) {
 46  10
             Predicate<Exchange> predicate = filterProcessor.getPredicate();
 47  10
             if (predicate != null && predicate.matches(exchange)) {
 48  4
                 filterProcessor.getProcessor().process(exchange);
 49  4
                 return;
 50  
             }
 51  6
         }
 52  2
         if (otherwise != null) {
 53  2
             otherwise.process(exchange);
 54  
         }
 55  2
     }
 56  
 
 57  
     @Override
 58  
     public String toString() {
 59  8
         StringBuilder builder = new StringBuilder("choice{");
 60  8
         boolean first = true;
 61  8
         for (FilterProcessor processor : filters) {
 62  16
             if (first) {
 63  8
                 first = false;
 64  8
             }
 65  
             else {
 66  8
                 builder.append(", ");
 67  
             }
 68  16
             builder.append("when ");
 69  16
             builder.append(processor.getPredicate().toString());
 70  16
             builder.append(": ");
 71  16
             builder.append(processor.getProcessor());
 72  16
         }
 73  8
         if (otherwise != null) {
 74  8
             builder.append(", otherwise: ");
 75  8
             builder.append(otherwise);
 76  
         }
 77  8
         builder.append("}");
 78  8
         return builder.toString();
 79  
     }
 80  
 
 81  
     public List<FilterProcessor> getFilters() {
 82  1
         return filters;
 83  
     }
 84  
 
 85  
     public Processor getOtherwise() {
 86  1
         return otherwise;
 87  
     }
 88  
 
 89  
 
 90  
     protected void doStart() throws Exception {
 91  4
         ServiceHelper.startServices(filters);
 92  4
         ServiceHelper.startServices(otherwise);
 93  4
     }
 94  
 
 95  
     protected void doStop() throws Exception {
 96  4
         ServiceHelper.stopServices(otherwise);
 97  4
         ServiceHelper.stopServices(filters);
 98  4
     }
 99  
 }