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