Coverage Report - org.apache.camel.processor.ChoiceProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoiceProcessor
100% 
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.processor;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.Predicate;
 24  
 import org.apache.camel.Processor;
 25  
 import org.apache.camel.impl.ServiceSupport;
 26  
 import org.apache.camel.util.ServiceHelper;
 27  
 
 28  
 /**
 29  
  * Implements a Choice structure where one or more predicates are used which if
 30  
  * they are true their processors are used, with a default otherwise clause used
 31  
  * if none match.
 32  
  * 
 33  
  * @version $Revision: 563607 $
 34  
  */
 35  
 public class ChoiceProcessor extends ServiceSupport implements Processor {
 36  33
     private List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
 37  
     private Processor otherwise;
 38  
 
 39  33
     public ChoiceProcessor(List<FilterProcessor> filters, Processor otherwise) {
 40  33
         this.filters = filters;
 41  33
         this.otherwise = otherwise;
 42  33
     }
 43  
 
 44  
     public void process(Exchange exchange) throws Exception {
 45  36
         for (FilterProcessor filterProcessor : filters) {
 46  48
             Predicate<Exchange> predicate = filterProcessor.getPredicate();
 47  48
             if (predicate != null && predicate.matches(exchange)) {
 48  21
                 filterProcessor.getProcessor().process(exchange);
 49  21
                 return;
 50  
             }
 51  27
         }
 52  15
         if (otherwise != null) {
 53  15
             otherwise.process(exchange);
 54  
         }
 55  15
     }
 56  
 
 57  
     @Override
 58  
     public String toString() {
 59  21
         StringBuilder builder = new StringBuilder("choice{");
 60  21
         boolean first = true;
 61  21
         for (FilterProcessor processor : filters) {
 62  42
             if (first) {
 63  21
                 first = false;
 64  21
             } else {
 65  21
                 builder.append(", ");
 66  
             }
 67  42
             builder.append("when ");
 68  42
             builder.append(processor.getPredicate().toString());
 69  42
             builder.append(": ");
 70  42
             builder.append(processor.getProcessor());
 71  42
         }
 72  21
         if (otherwise != null) {
 73  21
             builder.append(", otherwise: ");
 74  21
             builder.append(otherwise);
 75  
         }
 76  21
         builder.append("}");
 77  21
         return builder.toString();
 78  
     }
 79  
 
 80  
     public List<FilterProcessor> getFilters() {
 81  3
         return filters;
 82  
     }
 83  
 
 84  
     public Processor getOtherwise() {
 85  3
         return otherwise;
 86  
     }
 87  
 
 88  
     protected void doStart() throws Exception {
 89  30
         ServiceHelper.startServices(filters);
 90  30
         ServiceHelper.startServices(otherwise);
 91  30
     }
 92  
 
 93  
     protected void doStop() throws Exception {
 94  30
         ServiceHelper.stopServices(otherwise);
 95  30
         ServiceHelper.stopServices(filters);
 96  30
     }
 97  
 }