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 java.util.concurrent.TimeUnit;
020
021import org.apache.camel.spi.ThreadPoolProfile;
022import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy;
023
024/**
025 * Builder to build {@link org.apache.camel.spi.ThreadPoolProfile}.
026 * <p/>
027 * Use the {@link #build()} method when done setting up the profile.
028 */
029public class ThreadPoolProfileBuilder {
030    private final ThreadPoolProfile profile;
031
032    public ThreadPoolProfileBuilder(String id) {
033        this.profile = new ThreadPoolProfile(id);
034    }
035
036    public ThreadPoolProfileBuilder(String id, ThreadPoolProfile origProfile) {
037        this.profile = origProfile.clone();
038        this.profile.setId(id);
039    }
040
041    public ThreadPoolProfileBuilder defaultProfile(Boolean defaultProfile) {
042        if (defaultProfile != null) {
043            this.profile.setDefaultProfile(defaultProfile);
044        }
045        return this;
046    }
047
048    public ThreadPoolProfileBuilder poolSize(Integer poolSize) {
049        if (poolSize != null) {
050            profile.setPoolSize(poolSize);
051        }
052        return this;
053    }
054
055    public ThreadPoolProfileBuilder maxPoolSize(Integer maxPoolSize) {
056        if (maxPoolSize != null) {
057            profile.setMaxPoolSize(maxPoolSize);
058        }
059        return this;
060    }
061
062    public ThreadPoolProfileBuilder keepAliveTime(Long keepAliveTime, TimeUnit timeUnit) {
063        if (keepAliveTime != null) {
064            profile.setKeepAliveTime(keepAliveTime);
065        }
066        if (timeUnit != null) {
067            profile.setTimeUnit(timeUnit);
068        }
069        return this;
070    }
071
072    public ThreadPoolProfileBuilder keepAliveTime(Long keepAliveTime) {
073        if (keepAliveTime != null) {
074            profile.setKeepAliveTime(keepAliveTime);
075        }
076        return this;
077    }
078
079    public ThreadPoolProfileBuilder maxQueueSize(Integer maxQueueSize) {
080        if (maxQueueSize != null) {
081            profile.setMaxQueueSize(maxQueueSize);
082        }
083        return this;
084    }
085
086    public ThreadPoolProfileBuilder allowCoreThreadTimeOut(Boolean allowCoreThreadTimeOut) {
087        if (allowCoreThreadTimeOut != null) {
088            profile.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
089        }
090        return this;
091    }
092
093    public ThreadPoolProfileBuilder rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
094        if (rejectedPolicy != null) {
095            profile.setRejectedPolicy(rejectedPolicy);
096        }
097        return this;
098    }
099
100    /**
101     * Builds the thread pool profile
102     * 
103     * @return the thread pool profile
104     */
105    public ThreadPoolProfile build() {
106        return profile;
107    }
108
109}