package meerkat.voting.controller.callbacks;

import com.google.common.util.concurrent.FutureCallback;
import meerkat.voting.controller.commands.ControllerCommand;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * The base (abstract) class of all callbacks for requests sent by the controller to other components (ui, output-device)
 * It implements the FutureCallback interface
 * Its members are:
 *   -  requestIdentifier - uniquely identifies the request which this callback responds
 *   -  ballotSerialNumber - number of ballot which was currently active when request was sent
 *   -  controllerQueue - so the callback can issue and register a new command to the controller, once the request handling is finished
 */
public abstract class ControllerCallback<T> implements FutureCallback<T> {

    private final int requestIdentifier;
    private final long ballotSerialNumber;
    private LinkedBlockingQueue<ControllerCommand> controllerQueue;

    protected ControllerCallback (int requestId,
                                  long ballotSerialNumber,
                                  LinkedBlockingQueue<ControllerCommand> controllerQueue) {
        this.requestIdentifier = requestId;
        this.ballotSerialNumber = ballotSerialNumber;
        this.controllerQueue = controllerQueue;
    }

    protected int getRequestIdentifier () {
        return requestIdentifier;
    }

    protected long getBallotSerialNumber () {
        return ballotSerialNumber;
    }

    protected void enqueueCommand (ControllerCommand command) {
        controllerQueue.add(command);
    }

}
