/*
 * 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.teradata.benchto.driver.listeners.benchmark;

import com.facebook.presto.jdbc.internal.guava.collect.Ordering;
import com.google.common.collect.ImmutableList;
import com.teradata.benchto.driver.Benchmark;
import com.teradata.benchto.driver.execution.BenchmarkExecutionResult;
import com.teradata.benchto.driver.execution.QueryExecution;
import com.teradata.benchto.driver.execution.QueryExecutionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import java.util.List;

@Component
public class BenchmarkStatusReporter
{
    @Autowired
    private List<BenchmarkExecutionListener> executionListeners;

    @PostConstruct
    public void sortExecutionListeners()
    {
        // HACK: listeners have to be sorted to provide tests determinism
        executionListeners = ImmutableList.copyOf(
                Ordering.<Ordered>from(OrderComparator.INSTANCE::compare)
                        .compound(Ordering.usingToString())
                        .sortedCopy(executionListeners));
    }

    public void reportBenchmarkStarted(Benchmark benchmark)
    {
        for (BenchmarkExecutionListener listener : executionListeners) {
            listener.benchmarkStarted(benchmark);
        }
    }

    public void reportBenchmarkFinished(BenchmarkExecutionResult benchmarkExecutionResult)
    {
        for (BenchmarkExecutionListener listener : executionListeners) {
            listener.benchmarkFinished(benchmarkExecutionResult);
        }
    }

    public void reportExecutionStarted(QueryExecution queryExecution)
    {
        for (BenchmarkExecutionListener listener : executionListeners) {
            listener.executionStarted(queryExecution);
        }
    }

    public void reportExecutionFinished(QueryExecutionResult queryExecutionResult)
    {
        for (BenchmarkExecutionListener listener : executionListeners) {
            listener.executionFinished(queryExecutionResult);
        }
    }
}

@@ -1236,16 +1236,90 @@
 il.List;
+%0Aimport java.util.concurrent.Future;%0Aimport java.util.function.BiFunction;
 %0A%0A@Compo
@@ -1912,93 +1912,49 @@
    f
-or (BenchmarkExecutionListener listener : executionListeners) %7B%0A            listener.
+ireListeners(BenchmarkExecutionListener::
 benc
@@ -1965,17 +1965,18 @@
 kStarted
-(
+, 
 benchmar
@@ -1979,26 +1979,16 @@
 hmark);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2096,93 +2096,49 @@
    f
-or (BenchmarkExecutionListener listener : executionListeners) %7B%0A            listener.
+ireListeners(BenchmarkExecutionListener::
 benc
@@ -2150,17 +2150,18 @@
 Finished
-(
+, 
 benchmar
@@ -2175,34 +2175,24 @@
 ionResult);%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -2263,35 +2263,44 @@
 %0A    %7B%0A        f
-or 
+ireListeners
 (BenchmarkExecut
@@ -2314,39 +2314,138 @@
 ener
- listener : executionListeners)
+::executionStarted, queryExecution);%0A    %7D%0A%0A    public void reportExecutionFinished(QueryExecutionResult queryExecutionResult)%0A   
  %7B%0A 
@@ -2455,152 +2455,205 @@
     
-    l
+fireL
 istener
-.executionStarted(queryExecution);%0A        %7D%0A    %7D%0A%0A    public void reportExecutionFinished(QueryExecutionResult queryExecutionResul
+s(BenchmarkExecutionListener::executionFinished, queryExecutionResult);%0A    %7D%0A%0A    private %3CT%3E void fireListeners(BiFunction%3CBenchmarkExecutionListener, T, Future%3C?%3E%3E invoker, T argumen
 t)%0A 
@@ -2746,54 +2746,39 @@
     
-listener.executionFinished(queryExecutionResul
+invoker.apply(listener, argumen
 t);%0A

Refactor BenchmarkStatusReporter