/*
** Module   : SubQueue.java
** Abstract : API for objects that represent a portion of a larger queue
**
** Copyright (c) 2009-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090318 An API for objects that contain one or more elements that
**                  represent a portion of a larger queue.
** 002 TJD 20230119 Allow restarting of Driver threads
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness;

/**
 * An API for objects that contain one or more elements that represent a
 * portion of a larger queue.
 * 
 * @param   <E>
 *          Type of queue element.
 */
public interface SubQueue<E>
{
   /**
    * Reports if there are any elements remaining in the queue. This can answer
    * the question of whether there will ever be elements available to
    * {@link #dequeue}. Note that due to dependencies between elements, it is
    * possible for a queue to be non-empty but to not yet be ready to accept
    * a <code>dequeue</code> request.
    *
    * @return   <code>true</code> if all queue elements have been dequeued.
    */
   public boolean isEmpty();
   
   /**
    * Reports if the queue has elements to {@link #dequeue}. This will be
    * <code>false</code> if there are no elements remaining OR if the elements
    * that are contained have dependencies upon other elements such that no
    * dequeuing is possible until the dependencies are satisfied. In such a
    * case the queue is considered not ready.
    *
    * @return   <code>true</code> if the queue contains at least one element
    *           which is ready to be dequeued.
    */
   public boolean isReady();
   
   /**
    * Reports if the queue acts as a barrier. A barrier is a sub-queue that
    * ends the search for elements until such time as the barrier sub-queue is
    * the first sub-queue. This is a mechanism to force all prior sub-queues to
    * be fully processed before any following sub-queues can be processed.
    * <p>
    * Barriers DO NOT have any elements of their own to return. Rather, they
    * just are a marker of known "sections" of the containing queue.
    *
    * @return   <code>true</code> if the queue is a barrier.
    */
   public boolean isBarrier();
   
   /**
    * Associates the next element in the queue with the caller, without
    * actually calling {@link #dequeue} which can block. This allows the
    * calling code to release any resources before the subsequent potentially
    * blocking operation. Any subsequent call to {@link #isReady} MUST take
    * this reservation into account. This means that all calls to this method
    * must be matched with a subsequent call to <code>dequeue</code>.
    *
    * @return   <code>true</code> if the reservation was successful. Otherwise
    *           the queue is not ready and caller MUST NOT call
    *           <code>dequeue</code>. 
    */
   public boolean reserve();
   
   /**
    * Obtain the next element from the queue, blocking until an element is
    * available. Elements can exist but be unavailable due to dependencies that
    * have not yet completed.
    *
    * @return   The next element in the queue.
    */
   public E dequeue();
   
   /**
    * Reports if the queue can be ignored, usually for given thread.
    *
    * @return   <code>true</code> if the queue can be ignored.
    */
   public boolean isIgnored();
}
