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: 1383 $
032     */
033    public class PredicateAggregationCollection extends DefaultAggregationCollection {
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        public 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 to send
046                super.getMap().remove(correlationKey);
047                collection.add(newExchange);
048            }
049        }
050    
051        @Override
052        public Iterator<Exchange> iterator() {
053            return collection.iterator();
054        }
055    
056        @Override
057        public int size() {
058            return collection.size();
059        }
060    
061        @Override
062        public void clear() {
063            collection.clear();
064            super.clear();
065        }
066    }