/*
 * Copyright 2016 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.pattern;

import net.uncontended.precipice.*;
import net.uncontended.precipice.concurrent.NewEventual;
import net.uncontended.precipice.concurrent.PrecipicePromise;
import net.uncontended.precipice.metrics.ActionMetrics;
import net.uncontended.precipice.metrics.LatencyMetrics;

import java.util.ArrayList;
import java.util.List;

public class PatternController<T extends Enum<T> & Result> {
    private final ActionMetrics<T> actionMetrics;
    private final LatencyMetrics<T> latencyMetrics;
    private final String name;
    private volatile boolean isShutdown = false;

    public PatternController(String name, PatternControllerProperties<T> properties) {
        this(name, properties.actionMetrics(), properties.latencyMetrics());
    }

    public PatternController(String name, ActionMetrics<T> actionMetrics, LatencyMetrics<T> latencyMetrics) {
        this.actionMetrics = actionMetrics;
        this.latencyMetrics = latencyMetrics;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public ActionMetrics<T> getActionMetrics() {
        return actionMetrics;
    }

    public LatencyMetrics<T> getLatencyMetrics() {
        return latencyMetrics;
    }

    public void ensureNotShutdown() {
        if (isShutdown) {
            throw new IllegalStateException("Service has been shutdown.");
        }
    }

    public <R> PrecipicePromise<T, R> getPromise() {
        ensureNotShutdown();
        long startTime = System.nanoTime();
        if (null != null) {
            actionMetrics.incrementRejectionCount(null, startTime);
            throw new RejectedActionException(null);
        }

        NewEventual<T, R> promise = new NewEventual<>(startTime);
        promise.internalOnComplete(new PrecipiceFunction<T, PerformingContext>() {
            @Override
            public void apply(T status, PerformingContext eventual) {
                long endTime = System.nanoTime();
                actionMetrics.incrementMetricCount(status);
                latencyMetrics.recordLatency(status, endTime - eventual.startNanos(), endTime);
            }
        });
        return promise;
    }

    public List<NewController<T>> getChildControllers() {
        return new ArrayList<>();
    }

    public void shutdown() {
        isShutdown = true;
    }
}

@@ -954,16 +954,68 @@
 il.List;
+%0Aimport java.util.concurrent.atomic.AtomicReference;
 %0A%0Apublic
@@ -1198,24 +1198,82 @@
 tring name;%0A
+    private AtomicReference%3CNewController%3CT%3E%5B%5D%3E children;%0A
     private 
@@ -2904,21 +2904,16 @@
  public 
-List%3C
 NewContr
@@ -2920,17 +2920,18 @@
 oller%3CT%3E
-%3E
+%5B%5D
  getChil
@@ -2966,23 +2966,20 @@
 urn 
-new ArrayList%3C%3E
+children.get
 ();%0A

Use arrays opposed to lists for child controllers