/*
 * Copyright 2014 Timothy Brooks
 *
 * 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 net.uncontended.precipice;

import net.uncontended.precipice.circuit.CircuitBreaker;
import net.uncontended.precipice.circuit.NoOpCircuitBreaker;
import net.uncontended.precipice.concurrent.IntegerSemaphore;
import net.uncontended.precipice.metrics.ActionMetrics;
import net.uncontended.precipice.utils.PrecipiceExecutors;

import java.util.concurrent.ExecutorService;

public class Services {

    public static SubmissionService submissionService(String name, int poolSize, int concurrencyLevel) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel));
    }

    public static SubmissionService submissionService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics);
    }

    public static SubmissionService submissionService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static SubmissionService submissionService(ExecutorService executor, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static RunService runService(String name, int concurrencyLevel) {
        return new DefaultRunService(new IntegerSemaphore(concurrencyLevel));
    }

    public static RunService runService(String name, int concurrencyLevel, ActionMetrics metrics) {
        return new DefaultRunService(new IntegerSemaphore(concurrencyLevel), metrics);
    }

    public static RunService runService(String name, int concurrencyLevel, ActionMetrics metrics,
                                        CircuitBreaker breaker) {
        return new DefaultRunService(new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static RunService completionServiceWithNoOpBreaker(String name, int concurrencyLevel) {
        return new DefaultRunService(new IntegerSemaphore(concurrencyLevel), new NoOpCircuitBreaker());
    }

    public static CompletionService completionService(String name, int poolSize, int concurrencyLevel) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel));
    }

    public static CompletionService completionService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics);
    }

    public static CompletionService completionService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static CompletionService completionService(ExecutorService executor, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static CompletionService completionServiceWithNoOpBreaker(String name, int poolSize, int concurrencyLevel) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), new NoOpCircuitBreaker());
    }

    public static MultiService defaultService(String name, int poolSize, int concurrencyLevel) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel));
    }

    public static MultiService defaultServiceWithNoOpBreaker(String name, int poolSize, int concurrencyLevel) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), new NoOpCircuitBreaker());
    }

    public static MultiService defaultService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics);
    }

    public static MultiService defaultService(String name, int poolSize, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        ExecutorService executor = PrecipiceExecutors.threadPoolExecutor(name, poolSize, concurrencyLevel);
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }

    public static MultiService defaultService(ExecutorService executor, int concurrencyLevel, ActionMetrics
            metrics, CircuitBreaker breaker) {
        return new DefaultService(executor, new IntegerSemaphore(concurrencyLevel), metrics, breaker);
    }
}

@@ -1226,32 +1226,42 @@
 urn new DefaultS
+ubmissionS
 ervice(executor,
@@ -1294,32 +1294,32 @@
 urrencyLevel));%0A
-
     %7D%0A%0A    publi
@@ -1576,32 +1576,42 @@
 urn new DefaultS
+ubmissionS
 ervice(executor,
@@ -1959,32 +1959,42 @@
 urn new DefaultS
+ubmissionS
 ervice(executor,
@@ -2242,32 +2242,42 @@
 urn new DefaultS
+ubmissionS
 ervice(executor,
@@ -3407,32 +3407,42 @@
 turn new Default
+Completion
 Service(executor
@@ -3757,32 +3757,42 @@
 turn new Default
+Completion
 Service(executor
@@ -4140,32 +4140,42 @@
 turn new Default
+Completion
 Service(executor
@@ -4423,32 +4423,42 @@
 turn new Default
+Completion
 Service(executor
@@ -4745,32 +4745,32 @@
 currencyLevel);%0A
-
         return n
@@ -4771,32 +4771,42 @@
 turn new Default
+Completion
 Service(executor

Update static construction methods to create specialized versions of services