private QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT> dequeueHelper(boolean block){
  QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT> qq=null;
  boolean first=true;
  if (block) {
    acquire();
  }
  if (currentNode == null) {
    currentNode=queryQueue.getHead();
    readCurrent=false;
    if (currentNode == null) {
      return null;
    }
  }
 else {
    if (readCurrent) {
      QueueListNode<QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT>> tempNode=currentNode.getNext();
      if (tempNode != null) {
        currentNode=tempNode;
        readCurrent=false;
      }
 else {
        return null;
      }
    }
  }
  while (true) {
    if (block && !first) {
      acquire();
      if (currentNode == null) {
        currentNode=queryQueue.getHead();
        readCurrent=false;
        if (currentNode == null) {
          return null;
        }
      }
 else {
        if (readCurrent) {
          QueueListNode<QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT>> tempNode=currentNode.getNext();
          if (tempNode != null) {
            currentNode=tempNode;
            readCurrent=false;
          }
 else {
            return null;
          }
        }
      }
    }
synchronized (numLeft) {
      numLeft.getAndDecrement();
      QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT> queryQueueable=currentNode.getPayload();
      first=false;
      QueueListNode<QueryBundle<QUERY_TYPE,META_QUERY,QUEUE_CONTEXT>> nextNode=currentNode.getNext();
      if (removeBundle(queryQueueable)) {
        queryQueue.removeNode(currentNode);
        removedNode(currentNode);
        if (block) {
          if (nextNode == null) {
            readCurrent=true;
          }
 else {
            currentNode=nextNode;
            readCurrent=false;
          }
        }
 else {
          if (nextNode == null) {
            readCurrent=true;
            break;
          }
 else {
            currentNode=nextNode;
          }
        }
      }
 else {
        qq=currentNode.getPayload();
        if (nextNode == null) {
          readCurrent=true;
        }
 else {
          currentNode=nextNode;
          readCurrent=false;
        }
        break;
      }
    }
  }
  if (semaphore.availablePermits() > numLeft.get()) {
    try {
      semaphore.acquire(semaphore.availablePermits() - numLeft.get());
    }
 catch (    InterruptedException ex) {
      throw new RuntimeException(ex);
    }
  }
  return qq;
}

private QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > dequeueHelper ( boolean block ) { QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > qq = null ; boolean first = true ; if ( block ) { acquire ( ) ; } if ( currentNode == null ) { currentNode = queryQueue . getHead ( ) ; readCurrent = false ; if ( currentNode == null ) { return null ; } } else { if ( readCurrent ) { QueueListNode < QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > > tempNode = currentNode . getNext ( ) ; if ( tempNode != null ) { currentNode = tempNode ; readCurrent = false ; } else { return null ; } } } while ( true ) { if ( block && ! first ) { acquire ( ) ; if ( currentNode == null ) { currentNode = queryQueue . getHead ( ) ; readCurrent = false ; if ( currentNode == null ) { return null ; } } else { if ( readCurrent ) { QueueListNode < QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > > tempNode = currentNode . getNext ( ) ; if ( tempNode != null ) { currentNode = tempNode ; readCurrent = false ; } else { return null ; } } } } synchronized ( numLeft ) { numLeft . getAndDecrement ( ) ; QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > queryQueueable = currentNode . getPayload ( ) ; first = false ; QueueListNode < QueryBundle < QUERY_TYPE , META_QUERY , QUEUE_CONTEXT > > nextNode = currentNode . getNext ( ) ; if ( removeBundle ( queryQueueable ) ) { queryQueue . removeNode ( currentNode ) ; removedNode ( currentNode ) ; if ( block ) { if ( nextNode == null ) { readCurrent = true ; } else { currentNode = nextNode ; readCurrent = false ; } } else { if ( nextNode == null ) { readCurrent = true ; break ; } else { currentNode = nextNode ; } } } else { qq = currentNode . getPayload ( ) ; if ( nextNode == null ) { readCurrent = true ; } else { currentNode = nextNode ; readCurrent = false ; } break ; } } } if ( semaphore . availablePermits ( ) > numLeft . get ( ) ) { try { semaphore . acquire ( semaphore . availablePermits ( ) - numLeft . get ( ) ) ; } catch ( InterruptedException ex ) { throw new RuntimeException ( ex ) ; } } return qq ; }
this is a helper method for dequeueing queries .
