Coverage Report - org.apache.camel.processor.aggregate.AggregationCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
AggregationCollection
94% 
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.aggregate;
 18  
 
 19  
 import java.util.AbstractCollection;
 20  
 import java.util.Iterator;
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.camel.Exchange;
 25  
 import org.apache.camel.Expression;
 26  
 
 27  
 /**
 28  
  * A {@link Collection} which aggregates exchanges together using a correlation
 29  
  * expression so that there is only a single message exchange sent for a single
 30  
  * correlation key.
 31  
  * 
 32  
  * @version $Revision: 1.1 $
 33  
  */
 34  300
 public class AggregationCollection extends AbstractCollection<Exchange> {
 35  
     private final Expression<Exchange> correlationExpression;
 36  
     private final AggregationStrategy aggregationStrategy;
 37  3
     private Map<Object, Exchange> map = new LinkedHashMap<Object, Exchange>();
 38  
 
 39  
     public AggregationCollection(Expression<Exchange> correlationExpression,
 40  3
                                  AggregationStrategy aggregationStrategy) {
 41  3
         this.correlationExpression = correlationExpression;
 42  3
         this.aggregationStrategy = aggregationStrategy;
 43  3
     }
 44  
 
 45  
     @Override
 46  
     public boolean add(Exchange exchange) {
 47  300
         Object correlationKey = correlationExpression.evaluate(exchange);
 48  300
         Exchange oldExchange = map.get(correlationKey);
 49  300
         Exchange newExchange = exchange;
 50  300
         if (oldExchange != null) {
 51  297
             newExchange = aggregationStrategy.aggregate(oldExchange, newExchange);
 52  
         }
 53  
 
 54  
         // the strategy may just update the old exchange and return it
 55  300
         if (newExchange != oldExchange) {
 56  300
             map.put(correlationKey, newExchange);
 57  
         }
 58  300
         return true;
 59  
     }
 60  
 
 61  
     public Iterator<Exchange> iterator() {
 62  9
         return map.values().iterator();
 63  
     }
 64  
 
 65  
     public int size() {
 66  0
         return map.size();
 67  
     }
 68  
 }