/*
 * Copyright 2013 Rackspace
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
 
package com.rackspacecloud.blueflood.concurrent;
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.rackspacecloud.blueflood.utils.TimeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.concurrent.*;
 
public class ThreadPoolBuilder {
    private static final Logger log = LoggerFactory.getLogger(ThreadPoolBuilder.class);
 
    private int corePoolSize = 10;
    private int maxPoolSize = 10;
    private int queueSize = 0;
     
    private RejectedExecutionHandler rejectedHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    private Thread.UncaughtExceptionHandler exceptionHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
            log.error(e.getMessage(), e);
        }
    };
    private String name = "Thread";
    private TimeValue keepAliveTime = new TimeValue(30, TimeUnit.SECONDS);
 
    public ThreadPoolBuilder() {
 
    }
 
    public ThreadPoolBuilder withCorePoolSize(int size) {
        this.corePoolSize = size;
        return this;
    }
 
    public ThreadPoolBuilder withMaxPoolSize(int size) {
        this.maxPoolSize = size;
        return this;
    }
 
    public ThreadPoolBuilder withUnboundedQueue() {
        this.queueSize = 0;
        return this;
    }
 
    public ThreadPoolBuilder withBoundedQueue(int size) {
        this.queueSize = size;
        return this;
    }
     
    public ThreadPoolBuilder withKeepAliveTime(TimeValue time) {
        this.keepAliveTime = time;
        return this;
    }
 
    public ThreadPoolBuilder withRejectedHandler(RejectedExecutionHandler rejectedHandler) {
        this.rejectedHandler = rejectedHandler;
        return this;
    }
 
    public ThreadPoolBuilder withName(String name) {
        // ensure we've got a spot to put the thread id.
        if (name.indexOf("%d") < 0) {
            name = name + "-%d";
        }
        this.name = name;
        return this;
 
    }
 
    public ThreadPoolBuilder withExeptionHandler(Thread.UncaughtExceptionHandler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
        return this;
    }
 
    public ThreadPoolExecutor build() {
        String metricName = name.subSequence(0, name.length() - 3) + " work queue size"; // don't need the '-%d'
        BlockingQueue<Runnable> workQueue = this.queueSize > 0 ? new ArrayBlockingQueue<Runnable>(queueSize) :
                    new LinkedBlockingQueue<Runnable>();
         
        return new InstrumentedThreadPoolExecutor(
                metricName,
                corePoolSize,
                maxPoolSize,
                keepAliveTime.getValue(),
                keepAliveTime.getUnit(),
                workQueue,
                new ThreadFactoryBuilder().setNameFormat(name).setPriority(Thread.NORM_PRIORITY).setUncaughtExceptionHandler(exceptionHandler).build(),
                rejectedHandler);
    }
}
@@ -613,18 +613,17 @@
 se.%0A */%0A
- 
 %0A
+
 package 
@@ -663,18 +663,17 @@
 urrent;%0A
- 
 %0A
+
 import c
@@ -731,61 +731,8 @@
 er;%0A
-import com.rackspacecloud.blueflood.utils.TimeValue;%0A
 impo
@@ -784,18 +784,17 @@
 actory;%0A
- 
 %0A
+
 import j
@@ -816,18 +816,17 @@
 rent.*;%0A
- 
 %0A
+
 public c
@@ -938,18 +938,17 @@
 class);%0A
- 
 %0A
+
     priv
@@ -1039,26 +1039,25 @@
 ze = 0;%0A    
- 
 %0A
+
     private 
@@ -1407,85 +1407,9 @@
 d%22;%0A
-    private TimeValue keepAliveTime = new TimeValue(30, TimeUnit.SECONDS);%0A 
 %0A
+
     
@@ -1437,26 +1437,24 @@
 der() %7B%0A
- 
 %0A
+
     %7D%0A
- 
 %0A
+
     publ
@@ -1556,34 +1556,33 @@
 urn this;%0A    %7D%0A
- 
 %0A
+
     public Threa
@@ -1674,34 +1674,33 @@
 urn this;%0A    %7D%0A
- 
 %0A
+
     public Threa
@@ -1782,34 +1782,33 @@
 urn this;%0A    %7D%0A
- 
 %0A
+
     public Threa
@@ -1911,143 +1911,9 @@
   %7D%0A
-     %0A    public ThreadPoolBuilder withKeepAliveTime(TimeValue time) %7B%0A        this.keepAliveTime = time;%0A        return this;%0A    %7D%0A 
 %0A
+
     
@@ -2068,34 +2068,33 @@
 urn this;%0A    %7D%0A
- 
 %0A
+
     public Threa
@@ -2319,18 +2319,16 @@
 is;%0A
- 
 %0A
+
     %7D%0A
- 
 %0A
+
     
@@ -2501,18 +2501,17 @@
 ;%0A    %7D%0A
- 
 %0A
+
     publ
@@ -2663,16 +2663,22 @@
         
+final 
 Blocking
@@ -2837,18 +2837,17 @@
         
- 
 %0A
+
         
@@ -2996,74 +2996,55 @@
     
-keepAliveTime.getValue(),%0A                keepAliveTime.getUnit(),
+30, TimeUnit.SECONDS, // hard code the timeout.
 %0A   

fix merge conflict in ThreadPoolBuilder without borking whitepsace