package com.atlassian.stash.internal.concurrent;


import java.util.concurrent.ThreadPoolExecutor;

/**
 * Wrapping class for thread pools that is used to define the methods/attributes that should be exposed to JMX.
 * <p>
 * Provides a single non-delegate method {@link #getQueueLength()} that returns the worker queue length
 *
 * @since 3.8
 */
// Used via JMX
public class ThreadPoolMXBeanAdapter implements ThreadPoolMXBean {

    private final ThreadPoolExecutor delegate;

    /**
     * @param delegate the thread pool to be wrapped for exposure to JMX
     */
    public ThreadPoolMXBeanAdapter(ThreadPoolExecutor delegate) {
        this.delegate = delegate;
    }

    /**
     * Returns the approximate number of threads that are actively executing tasks.
     *
     * @return the number of threads
     */
    @Override
    public int getActiveCount() {
        return delegate.getActiveCount();
    }

    /**
     * Returns the number of work items that are awaiting processing.
     *
     * @return the length of the work item queue.
     */
    @Override
    public int getQueueLength() {
        return delegate.getQueue().size();
    }

    /**
     * Returns the current number of threads in the pool.
     *
     * @return the number of threads
     */
    @Override
    public int getPoolSize() {
        return delegate.getPoolSize();
    }

    /**
     * Returns the maximum allowed number of threads.
     *
     * @return the maximum allowed number of threads
     */
    @Override
    public int getMaximumPoolSize() {
        return delegate.getMaximumPoolSize();
    }

    /**
     * Returns the largest number of threads that have ever simultaneously been in the pool.
     *
     * @return the number of threads
     */
    @Override
    public int getLargestPoolSize() {
        return delegate.getLargestPoolSize();
    }

    /**
     * Returns the approximate total number of tasks that have completed execution. Because the states of tasks and
     * threads may change dynamically during computation, the returned value is only an approximation, but one that
     * does not ever decrease across successive calls.
     *
     * @return the number of tasks
     */
    @Override
    public long getCompletedTaskCount() {
        return delegate.getCompletedTaskCount();
    }

    /**
     * Returns the approximate total number of tasks that have ever been scheduled for execution. Because the states
     * of tasks and threads may change dynamically during computation, the returned value is only an approximation.
     *
     * @return the number of tasks
     */
    @Override
    public long getTaskCount() {
        return delegate.getTaskCount();
    }
 }