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 */
017package org.apache.camel.util.toolbox;
018
019import org.apache.camel.processor.aggregate.AggregationStrategy;
020import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
021import org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy;
022import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
023import org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy;
024
025/**
026 * Toolbox class to create commonly used Aggregation Strategies in a fluent manner.
027 * For more information about the supported {@link AggregationStrategy}, see links to the Javadocs of the relevant class below.
028 * 
029 * @since 2.12
030 */
031public final class AggregationStrategies {
032
033    private AggregationStrategies() { }
034
035    /**
036     * Creates a {@link FlexibleAggregationStrategy} pivoting around a particular type, e.g. it casts all <tt>pick expression</tt> 
037     * results to the desired type.
038     * 
039     * @param type The type the {@link FlexibleAggregationStrategy} deals with.
040     */
041    public static <T> FlexibleAggregationStrategy<T> flexible(Class<T> type) {
042        return new FlexibleAggregationStrategy<T>(type);
043    }
044    
045    /**
046     * Creates a {@link FlexibleAggregationStrategy} with no particular type, i.e. performing no casts or type conversion of 
047     * <tt>pick expression</tt> results.
048     */
049    public static FlexibleAggregationStrategy<Object> flexible() {
050        return new FlexibleAggregationStrategy<Object>();
051    }
052
053    /**
054     * Use the latest incoming exchange.
055     *
056     * @see org.apache.camel.processor.aggregate.UseLatestAggregationStrategy
057     */
058    public static AggregationStrategy useLatest() {
059        return new UseLatestAggregationStrategy();
060    }
061    
062    /**
063     * Use the original exchange.
064     *
065     * @see org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy
066     */
067    public static AggregationStrategy useOriginal() {
068        return new UseOriginalAggregationStrategy();
069    }
070
071    /**
072     * Creates a {@link GroupedExchangeAggregationStrategy} aggregation strategy.
073     */
074    public static AggregationStrategy groupedExchange() {
075        return new GroupedExchangeAggregationStrategy();
076    }
077
078    /**
079     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
080     */
081    public static AggregationStrategy bean(Object bean) {
082        return new AggregationStrategyBeanAdapter(bean);
083    }
084
085    /**
086     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
087     */
088    public static AggregationStrategy bean(Object bean, String methodName) {
089        return new AggregationStrategyBeanAdapter(bean, methodName);
090    }
091
092    /**
093     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
094     */
095    public static AggregationStrategy beanAllowNull(Object bean, String methodName) {
096        AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(bean, methodName);
097        adapter.setAllowNullOldExchange(true);
098        adapter.setAllowNullNewExchange(true);
099        return adapter;
100    }
101
102    /**
103     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
104     */
105    public static AggregationStrategy bean(Class<?> type) {
106        return new AggregationStrategyBeanAdapter(type);
107    }
108
109    /**
110     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
111     */
112    public static AggregationStrategy bean(Class<?> type, String methodName) {
113        return new AggregationStrategyBeanAdapter(type, methodName);
114    }
115
116    /**
117     * Creates a {@link AggregationStrategyBeanAdapter} for using a POJO as the aggregation strategy.
118     */
119    public static AggregationStrategy beanAllowNull(Class<?> type, String methodName) {
120        AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(type, methodName);
121        adapter.setAllowNullOldExchange(true);
122        adapter.setAllowNullNewExchange(true);
123        return adapter;
124    }
125
126    /**
127     * Creates a {@link XsltAggregationStrategy} as the aggregation strategy.
128     */
129    public static XsltAggregationStrategy xslt(String xslFileLocation) {
130        return XsltAggregationStrategy.create(xslFileLocation);
131    }
132
133}