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