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.example.tracer;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.Comparator;
022    import java.util.List;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.processor.aggregate.AggregationStrategy;
026    
027    /**
028     * Our aggregator where we aggregate all the quotes and find the
029     * the best quotes based on the one that has the most cool words
030     * from our cools words list
031     */
032    public class QuoteAggregator implements AggregationStrategy {
033    
034        private List<String> coolWords = new ArrayList<String>();
035    
036        public void setCoolWords(List<String> coolWords) {
037            for (String s : coolWords) {
038                // use lower case to be case insensitive
039                this.coolWords.add(s.toLowerCase());
040            }
041            // reverse order so indexOf returning -1 will be the last instead
042            Collections.reverse(this.coolWords);
043        }
044    
045        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
046            if (oldExchange == null) {
047                // the first time then just return the new exchange
048                return newExchange;
049            }
050    
051            // here we aggregate
052            // oldExchange is the current "winner"
053            // newExchange is the new candidate
054    
055            // we get the quotes of the two exchanges
056            String oldQuote = oldExchange.getIn().getBody(String.class);
057            String newQuote = newExchange.getIn().getBody(String.class);
058    
059            // now we compare the two and get a result indicate the best one
060            int result = new QuoteComparator().compare(oldQuote, newQuote);
061    
062            // we return the winner
063            return result > 0 ? newExchange : oldExchange;
064        }
065    
066        private class QuoteComparator implements Comparator<String> {
067    
068            public int compare(java.lang.String o1, java.lang.String o2) {
069                // here we compare the two quotes and picks the one that
070                // is in the top of the cool words list
071                int index1 = coolWords.indexOf(o1.toLowerCase());
072                int index2 = coolWords.indexOf(o2.toLowerCase());
073    
074                return index1 - index2;
075            }
076        }
077    
078    }