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 incase sensitive
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            // here we aggregate
047            // oldExchange is the current "winner"
048            // newExchange is the new candidate
049    
050            // we get the quotes of the two exchanges
051            String oldQute = oldExchange.getIn().getBody(String.class);
052            String newQute = newExchange.getIn().getBody(String.class);
053    
054            // now we compare the two and get a result indicate the best one
055            int result = new QuoteComparator().compare(oldQute, newQute);
056    
057            // we return the winner
058            return result > 0 ? newExchange : oldExchange;
059        }
060    
061        private class QuoteComparator implements Comparator<String> {
062    
063            public int compare(java.lang.String o1, java.lang.String o2) {
064                // here we compare the two quotes and picks the one that
065                // is in the top of the cool words list
066                int index1 = coolWords.indexOf(o1.toLowerCase());
067                int index2 = coolWords.indexOf(o2.toLowerCase());
068    
069                return index1 - index2;
070            }
071        }
072    
073    }