/*
** Module   : BarrierElement.java
** Abstract : represents a barrier element in a larger queue
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090318 Represents a barrier element in a larger queue.
*/

/*
** 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;

/**
 * Represents a barrier element in a larger queue. Instances contain no data,
 * maintain no state and only perform the role of a marker in the containing
 * queue. Processing of following sub-queues will not occur until the barrier
 * element is the first element in the containing queue (all previous
 * sub-queues must have completed processing and have been removed).
 * 
 * @param   <E>
 *          Type of queue element.
 */
public class BarrierElement<E>
implements SubQueue<E>
{
   /**
    * Construct an instance.
    */
   public BarrierElement()
   {
   }
   
   /**
    * 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   Always <code>false</code>.
    */
   public boolean isEmpty()
   {
      return false;
   }
   
   /**
    * 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   Always <code>false</code>.
    */
   public synchronized boolean isReady()
   {
      return false;
   }
   
   /**
    * 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   Always <code>true</code>.
    */
   public boolean isBarrier()
   {
      return true;
   }
   
   /**
    * 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   Always <code>false</code>. 
    */
   public boolean reserve()
   {
      return false;
   }
   
   /**
    * Obtain the (only) element from the queue.
    *
    * @return   Always <code>null</code>.
    */
   public E dequeue()
   {
      return null;
   }
}
