/*
 * 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;

import net.uncontended.precipice.metrics.counts.WritableCounts;
import net.uncontended.precipice.metrics.histogram.WritableLatency;
import net.uncontended.precipice.time.Clock;

public class GuardRailBuilder<Result extends Enum<Result> & Failable, Rejected extends Enum<Rejected>> {

    private GuardRailProperties<Result, Rejected> properties = new GuardRailProperties<>();


    public GuardRailBuilder<Result, Rejected> addBackPressure(BackPressure<Rejected> backPressure) {
        this.properties.backPressureList.add(backPressure);
        return this;
    }

    public GuardRailBuilder<Result, Rejected> name(String name) {
        this.properties.name = name;
        return this;
    }

    public GuardRailBuilder<Result, Rejected> resultMetrics(WritableCounts<Result> resultMetrics) {
        this.properties.resultMetrics = resultMetrics;
        return this;
    }

    public GuardRailBuilder<Result, Rejected> rejectedMetrics(WritableCounts<Rejected> rejectedMetrics) {
        this.properties.rejectedMetrics = rejectedMetrics;
        return this;
    }

    public GuardRailBuilder<Result, Rejected> resultLatency(WritableLatency<Result> resultLatency) {
        this.properties.resultLatency = resultLatency;
        return this;
    }

    public GuardRailBuilder<Result, Rejected> clock(Clock clock) {
        this.properties.clock = clock;
        return this;
    }

    public GuardRail<Result, Rejected> build() {
        if (properties.name == null) {
            throw new IllegalArgumentException();
        } else if (properties.resultMetrics == null) {
            throw new IllegalArgumentException();
        } else if (properties.rejectedMetrics == null) {
            throw new IllegalArgumentException();
        }

        if (properties.resultLatency == null) {
//            PartitionedCount<Result> total = properties.resultMetrics.total();
//            NoOpLatency<Result> totalLatency = new NoOpLatency<>(total.getMetricClazz());
//            NoOpLatency<Result> currentLatency = new NoOpLatency<>(total.getMetricClazz());
//            properties.resultLatency = new MetricRecorder<PartitionedHistogram<Result>>(totalLatency, currentLatency);
        }

        return GuardRail.create(properties);
    }
}

@@ -691,16 +691,80 @@
 Counts;%0A
+import net.uncontended.precipice.metrics.histogram.NoOpLatency;%0A
 import n
@@ -2492,18 +2492,16 @@
 null) %7B%0A
-//
         
@@ -2508,147 +2508,60 @@
     
-PartitionedCount%3CResult%3E total = properties.resultMetrics.total();%0A//            NoOpLatency%3CResult%3E totalLatency = new NoOpLatency%3C%3E(total
+Class%3CResult%3E metricClazz = properties.resultMetrics
 .get
@@ -2565,37 +2565,34 @@
 getMetricClazz()
-)
 ;%0A
-//
             NoOp
@@ -2607,24 +2607,17 @@
 Result%3E 
-currentL
+l
 atency =
@@ -2639,18 +2639,9 @@
 y%3C%3E(
-total.getM
+m
 etri
@@ -2650,15 +2650,11 @@
 lazz
-()
 );%0A
-//
     
@@ -2692,86 +2692,15 @@
 y = 
-new MetricRecorder%3CPartitionedHistogram%3CResult%3E%3E(totalLatency, currentLatency)
+latency
 ;%0A  

Fix building gr with no latency