/*
 * Copyright 2014-2017 Real Logic Ltd.
 *
 * 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 org.agrona.concurrent;

import org.agrona.ErrorHandler;
import org.agrona.concurrent.status.AtomicCounter;

import java.nio.channels.ClosedByInterruptException;
import java.util.Objects;

/**
 * {@link Agent} container which does not start a thread. It instead allows the duty cycle {@link Agent#doWork()} to be
 * invoked directly. {@link #start()} should be called to allow the {@link Agent#onStart()} to fire before any calls
 * to {@link #invoke()} of the agent duty cycle.
 * <p>
 * Exceptions which occur during the {@link Agent#doWork()} invocation will be caught and passed to the provided
 * {@link ErrorHandler}.
 * <p>
 * <b>Note:</b> This class is not threadsafe.
 */
public class AgentInvoker implements AutoCloseable
{
    private boolean isClosed = false;
    private boolean isStarted = false;

    private final AtomicCounter errorCounter;
    private final ErrorHandler errorHandler;
    private final Agent agent;

    /**
     * Create an agent and initialise it.
     *
     * @param errorHandler to be called if an {@link Throwable} is encountered
     * @param errorCounter to be incremented each time an exception is encountered. This may be null.
     * @param agent        to be run in this thread.
     */
    public AgentInvoker(
        final ErrorHandler errorHandler,
        final AtomicCounter errorCounter,
        final Agent agent)
    {
        Objects.requireNonNull(errorHandler, "errorHandler");
        Objects.requireNonNull(agent, "agent");

        this.errorHandler = errorHandler;
        this.errorCounter = errorCounter;
        this.agent = agent;
    }

    /**
     * Has the {@link Agent} been started?
     *
     * @return has the {@link Agent} been started?
     */
    public boolean isStarted()
    {
        return isStarted;
    }

    /**
     * Has the {@link Agent} been closed?
     *
     * @return has the {@link Agent} been closed?
     */
    public boolean isClosed()
    {
        return isClosed;
    }

    /**
     * The {@link Agent} which is contained.
     *
     * @return {@link Agent} being contained.
     */
    public Agent agent()
    {
        return agent;
    }

    /**
     * Invoke the {@link Agent#doWork()} method and return the work count.
     * <p>
     * If an error occurs then the {@link AtomicCounter#increment()} will be called on the errorCounter if not null
     * and the {@link Throwable} will be passed to the {@link ErrorHandler#onError(Throwable)} method. If the error
     * is an {@link AgentTerminationException} then {@link #close()} will be called after the error handler.
     * <p>
     * If not successfully started or after closed then this method will return without invoking the {@link Agent}.
     *
     * @return the work count for the {@link Agent#doWork()} method.
     */
    public int invoke()
    {
        int workCount = 0;
        if (isStarted && !isClosed)
        {
            try
            {
                workCount = agent.doWork();
            }
            catch (final InterruptedException | ClosedByInterruptException ignore)
            {
                Thread.interrupted();
            }
            catch (final AgentTerminationException ex)
            {
                handleError(ex);
                close();
            }
            catch (final Throwable throwable)
            {
                handleError(throwable);
            }
        }

        return workCount;
    }

    /**
     * Mark the invoker as started and call the {@link Agent#onStart()} method.
     * <p>
     * Startup logic will only be performed once.
     */
    public void start()
    {
        try
        {
            if (!isStarted)
            {
                agent.onStart();
                isStarted = true;
            }
        }
        catch (final Throwable throwable)
        {
            handleError(throwable);
        }
    }

    /**
     * Mark the invoker as closed and call the {@link Agent#onClose()} logic for clean up.
     * <p>
     * The clean up logic will only be performed once.
     */
    public final void close()
    {
        try
        {
            if (!isClosed)
            {
                isClosed = true;
                agent.onClose();
            }
        }
        catch (final Throwable throwable)
        {
            handleError(throwable);
        }
    }

    private void handleError(final Throwable throwable)
    {
        if (null != errorCounter)
        {
            errorCounter.increment();
        }

        errorHandler.onError(throwable);
    }
}

@@ -2741,32 +2741,479 @@
 n agent;%0A    %7D%0A%0A
+    /**%0A     * Mark the invoker as started and call the %7B@link Agent#onStart()%7D method.%0A     * %3Cp%3E%0A     * Startup logic will only be performed once.%0A     */%0A    public void start()%0A    %7B%0A        try%0A        %7B%0A            if (!isStarted)%0A            %7B%0A                agent.onStart();%0A                isStarted = true;%0A            %7D%0A        %7D%0A        catch (final Throwable throwable)%0A        %7B%0A            handleError(throwable);%0A        %7D%0A    %7D%0A%0A
     /**%0A     * I
@@ -4485,455 +4485,8 @@
  %7D%0A%0A
-    /**%0A     * Mark the invoker as started and call the %7B@link Agent#onStart()%7D method.%0A     * %3Cp%3E%0A     * Startup logic will only be performed once.%0A     */%0A    public void start()%0A    %7B%0A        try%0A        %7B%0A            if (!isStarted)%0A            %7B%0A                agent.onStart();%0A                isStarted = true;%0A            %7D%0A        %7D%0A        catch (final Throwable throwable)%0A        %7B%0A            handleError(throwable);%0A        %7D%0A    %7D%0A%0A
     

Order methods to follow lifecycle.