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.processor.aggregate;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Expression;
025    import org.apache.camel.Predicate;
026    
027    /**
028     * An aggregator collection which uses a predicate to decide when an aggregation is completed for
029     * a particular correlation key
030     *
031     * @version $Revision: 36321 $
032     */
033    public class PredicateAggregationCollection extends AggregationCollection {
034        private Predicate aggregationCompletedPredicate;
035        private List<Exchange> collection = new ArrayList<Exchange>();
036    
037        public PredicateAggregationCollection(Expression<Exchange> correlationExpression, AggregationStrategy aggregationStrategy, Predicate aggregationCompletedPredicate) {
038            super(correlationExpression, aggregationStrategy);
039            this.aggregationCompletedPredicate = aggregationCompletedPredicate;
040        }
041    
042        @Override
043        protected void onAggregation(Object correlationKey, Exchange newExchange) {
044            if (aggregationCompletedPredicate.matches(newExchange)) {
045                // this exchange has now aggregated so lets add it to the collection of things
046                // to send
047                super.getMap().remove(correlationKey);
048                collection.add(newExchange);
049            }
050        }
051    
052        @Override
053        public Iterator<Exchange> iterator() {
054            return collection.iterator();
055        }
056    
057        @Override
058        public int size() {
059            return collection.size();
060        }
061    
062        @Override
063        public void clear() {
064            collection.clear();
065            super.clear();
066        }
067    }