// Path: porcupine/src/main/java/com/airhacks/porcupine/execution/control/PipelineStore.java
// @ApplicationScoped
// public class PipelineStore {
// 
//     ConcurrentHashMap<String, Pipeline> pipelines;
// 
//     @PostConstruct
//     public void init() {
//         this.pipelines = new ConcurrentHashMap<>();
//     }
// 
//     public Pipeline get(String name) {
//         return this.pipelines.get(name);
//     }
// 
//     public void put(String pipelineName, Pipeline pipeline) {
//         this.pipelines.put(pipelineName, pipeline);
//     }
// 
//     public Collection<Pipeline> pipelines() {
//         return this.pipelines.values();
// 
//     }
// 
//     public Statistics getStatistics(String name) {
//         Pipeline pipeline = this.pipelines.get(name);
//         if (pipeline != null) {
//             return pipeline.getStatistics();
//         } else {
//             return new Statistics();
//         }
//     }
// 
//     public int getNumberOfPipelines() {
//         return this.pipelines.size();
//     }
// 
//     public List<Statistics> getAllStatistics() {
//         return this.pipelines.values().stream().
//                 map(p -> p.getStatistics()).
//                 collect(Collectors.toList());
//     }
// 
//     public void clear() {
//         this.pipelines.clear();
//     }
// 
//     public Pipeline findPipeline(ThreadPoolExecutor executor) {
//         return this.pipelines().stream().filter((p) -> p.manages(executor)).findFirst().orElse(null);
//     }
// 
//     @PreDestroy
//     public void shutdown() {
//         this.pipelines.values().parallelStream().forEach(p -> p.shutdown());
//         this.pipelines.clear();
//     }
// 
// }
// 
// Path: porcupine/src/main/java/com/airhacks/porcupine/execution/entity/Pipeline.java
// public class Pipeline {
// 
//     private final String pipelineName;
//     private final InstrumentedThreadPoolExecutor tpe;
//     private final AtomicLong rejectedTasks;
// 
//     public Pipeline(String pipelineName, InstrumentedThreadPoolExecutor tpe) {
//         this.pipelineName = pipelineName;
//         this.tpe = tpe;
//         this.rejectedTasks = new AtomicLong();
//     }
// 
//     public Statistics getStatistics() {
//         int remainingQueueCapacity = this.tpe.getQueue().remainingCapacity();
//         int minQueueCapacity = this.tpe.getMinRemainingQueueCapacity();
//         int corePoolSize = this.tpe.getCorePoolSize();
//         long completedTaskCount = this.tpe.getCompletedTaskCount();
//         int activeThreadCount = this.tpe.getActiveCount();
//         int largestThreadPoolSize = this.tpe.getLargestPoolSize();
//         int currentThreadPoolSize = this.tpe.getPoolSize();
//         long totalNumberOfTasks = this.tpe.getTaskCount();
//         int maximumPoolSize = this.tpe.getMaximumPoolSize();
//         RejectedExecutionHandler handler = this.tpe.getRejectedExecutionHandler();
//         String rejectedExecutionHandlerName = null;
//         if (handler != null) {
//             rejectedExecutionHandlerName = handler.getClass().getSimpleName();
//         }
//         return new Statistics(this.pipelineName, remainingQueueCapacity, minQueueCapacity, completedTaskCount, activeThreadCount, corePoolSize, largestThreadPoolSize, currentThreadPoolSize, totalNumberOfTasks, maximumPoolSize, rejectedExecutionHandlerName, this.rejectedTasks.get());
// 
//     }
// 
//     public ExecutorService getExecutor() {
//         return tpe;
//     }
// 
//     public void shutdown() {
//         this.tpe.shutdown();
//     }
// 
//     public boolean manages(ThreadPoolExecutor executor) {
//         return this.tpe == executor;
//     }
// 
//     public void taskRejected() {
//         this.rejectedTasks.incrementAndGet();
//     }
// 
// }


import com.airhacks.porcupine.execution.control.PipelineStore;
import com.airhacks.porcupine.execution.entity.Pipeline;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/*
 * Copyright 2016 Adam Bien.
 *
 * 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.airhacks.porcupine.execution.boundary;


/**
 *
 * @author airhacks.com
 */
public class RejectionsCounter implements RejectedExecutionHandler {

    private RejectedExecutionHandler handler;
    private PipelineStore ps;

    public RejectionsCounter(RejectedExecutionHandler handler, PipelineStore pipeline) {
        this.handler = handler;
        this.ps = pipeline;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// Path: porcupine/src/main/java/com/airhacks/porcupine/execution/control/PipelineStore.java
// @ApplicationScoped
// public class PipelineStore {
// 
//     ConcurrentHashMap<String, Pipeline> pipelines;
// 
//     @PostConstruct
//     public void init() {
//         this.pipelines = new ConcurrentHashMap<>();
//     }
// 
//     public Pipeline get(String name) {
//         return this.pipelines.get(name);
//     }
// 
//     public void put(String pipelineName, Pipeline pipeline) {
//         this.pipelines.put(pipelineName, pipeline);
//     }
// 
//     public Collection<Pipeline> pipelines() {
//         return this.pipelines.values();
// 
//     }
// 
//     public Statistics getStatistics(String name) {
//         Pipeline pipeline = this.pipelines.get(name);
//         if (pipeline != null) {
//             return pipeline.getStatistics();
//         } else {
//             return new Statistics();
//         }
//     }
// 
//     public int getNumberOfPipelines() {
//         return this.pipelines.size();
//     }
// 
//     public List<Statistics> getAllStatistics() {
//         return this.pipelines.values().stream().
//                 map(p -> p.getStatistics()).
//                 collect(Collectors.toList());
//     }
// 
//     public void clear() {
//         this.pipelines.clear();
//     }
// 
//     public Pipeline findPipeline(ThreadPoolExecutor executor) {
//         return this.pipelines().stream().filter((p) -> p.manages(executor)).findFirst().orElse(null);
//     }
// 
//     @PreDestroy
//     public void shutdown() {
//         this.pipelines.values().parallelStream().forEach(p -> p.shutdown());
//         this.pipelines.clear();
//     }
// 
// }
// 
// Path: porcupine/src/main/java/com/airhacks/porcupine/execution/entity/Pipeline.java
// public class Pipeline {
// 
//     private final String pipelineName;
//     private final InstrumentedThreadPoolExecutor tpe;
//     private final AtomicLong rejectedTasks;
// 
//     public Pipeline(String pipelineName, InstrumentedThreadPoolExecutor tpe) {
//         this.pipelineName = pipelineName;
//         this.tpe = tpe;
//         this.rejectedTasks = new AtomicLong();
//     }
// 
//     public Statistics getStatistics() {
//         int remainingQueueCapacity = this.tpe.getQueue().remainingCapacity();
//         int minQueueCapacity = this.tpe.getMinRemainingQueueCapacity();
//         int corePoolSize = this.tpe.getCorePoolSize();
//         long completedTaskCount = this.tpe.getCompletedTaskCount();
//         int activeThreadCount = this.tpe.getActiveCount();
//         int largestThreadPoolSize = this.tpe.getLargestPoolSize();
//         int currentThreadPoolSize = this.tpe.getPoolSize();
//         long totalNumberOfTasks = this.tpe.getTaskCount();
//         int maximumPoolSize = this.tpe.getMaximumPoolSize();
//         RejectedExecutionHandler handler = this.tpe.getRejectedExecutionHandler();
//         String rejectedExecutionHandlerName = null;
//         if (handler != null) {
//             rejectedExecutionHandlerName = handler.getClass().getSimpleName();
//         }
//         return new Statistics(this.pipelineName, remainingQueueCapacity, minQueueCapacity, completedTaskCount, activeThreadCount, corePoolSize, largestThreadPoolSize, currentThreadPoolSize, totalNumberOfTasks, maximumPoolSize, rejectedExecutionHandlerName, this.rejectedTasks.get());
// 
//     }
// 
//     public ExecutorService getExecutor() {
//         return tpe;
//     }
// 
//     public void shutdown() {
//         this.tpe.shutdown();
//     }
// 
//     public boolean manages(ThreadPoolExecutor executor) {
//         return this.tpe == executor;
//     }
// 
//     public void taskRejected() {
//         this.rejectedTasks.incrementAndGet();
//     }
// 
// }


// Path: porcupine/src/main/java/com/airhacks/porcupine/execution/boundary/RejectionsCounter.java
import com.airhacks.porcupine.execution.control.PipelineStore;
import com.airhacks.porcupine.execution.entity.Pipeline;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/*
 * Copyright 2016 Adam Bien.
 *
 * 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.airhacks.porcupine.execution.boundary;


/**
 *
 * @author airhacks.com
 */
public class RejectionsCounter implements RejectedExecutionHandler {

    private RejectedExecutionHandler handler;
    private PipelineStore ps;

    public RejectionsCounter(RejectedExecutionHandler handler, PipelineStore pipeline) {
        this.handler = handler;
        this.ps = pipeline;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        Pipeline findPipeline = this.ps.findPipeline(executor);