com.jolbox.bonecp
Class LIFOQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by java.util.concurrent.LinkedBlockingDeque<E>
              extended by com.jolbox.bonecp.LIFOQueue<E>
Type Parameters:
E -
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingDeque<E>, java.util.concurrent.BlockingQueue<E>, java.util.Deque<E>, java.util.Queue<E>, TransferQueue<E>

public class LIFOQueue<E>
extends java.util.concurrent.LinkedBlockingDeque<E>
implements TransferQueue<E>

An implementation that uses LinkedBlockingDeque internally to hide the difference between this version and a BoundedLinkedTransferQueue (when used in FIFO mode). This changes the queue so that every insert function is inserted at the head of the list.

Author:
wallacew
See Also:
Serialized Form

Constructor Summary
LIFOQueue()
           
LIFOQueue(int capacity)
           
 
Method Summary
 int getWaitingConsumerCount()
          Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll.
 boolean hasWaitingConsumer()
          Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take() or timed poll.
 boolean offer(E e)
           
 void transfer(E e)
          Transfers the element to a consumer, waiting if necessary to do so.
 boolean tryTransfer(E e)
          Transfers the element to a waiting consumer immediately, if possible.
 boolean tryTransfer(E e, long timeout, java.util.concurrent.TimeUnit unit)
          Transfers the element to a consumer if it is possible to do so before the timeout elapses.
 
Methods inherited from class java.util.concurrent.LinkedBlockingDeque
add, addFirst, addLast, clear, contains, descendingIterator, drainTo, drainTo, element, getFirst, getLast, iterator, offer, offerFirst, offerFirst, offerLast, offerLast, peek, peekFirst, peekLast, poll, poll, pollFirst, pollFirst, pollLast, pollLast, pop, push, put, putFirst, putLast, remainingCapacity, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, size, take, takeFirst, takeLast, toArray, toArray, toString
 
Methods inherited from class java.util.AbstractQueue
addAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.BlockingQueue
add, contains, drainTo, drainTo, offer, poll, put, remainingCapacity, remove, take
 
Methods inherited from interface java.util.Queue
element, peek, poll, remove
 
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

LIFOQueue

public LIFOQueue(int capacity)

LIFOQueue

public LIFOQueue()
Method Detail

tryTransfer

public boolean tryTransfer(E e)
Description copied from interface: TransferQueue
Transfers the element to a waiting consumer immediately, if possible.

More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in BlockingQueue.take() or timed poll), otherwise returning false without enqueuing the element.

Specified by:
tryTransfer in interface TransferQueue<E>
Parameters:
e - the element to transfer
Returns:
true if the element was transferred, else false

transfer

public void transfer(E e)
              throws java.lang.InterruptedException
Description copied from interface: TransferQueue
Transfers the element to a consumer, waiting if necessary to do so.

More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in BlockingQueue.take() or timed poll), else waits until the element is received by a consumer.

Specified by:
transfer in interface TransferQueue<E>
Parameters:
e - the element to transfer
Throws:
java.lang.InterruptedException - if interrupted while waiting, in which case the element is not left enqueued

tryTransfer

public boolean tryTransfer(E e,
                           long timeout,
                           java.util.concurrent.TimeUnit unit)
                    throws java.lang.InterruptedException
Description copied from interface: TransferQueue
Transfers the element to a consumer if it is possible to do so before the timeout elapses.

More precisely, transfers the specified element immediately if there exists a consumer already waiting to receive it (in BlockingQueue.take() or timed poll), else waits until the element is received by a consumer, returning false if the specified wait time elapses before the element can be transferred.

Specified by:
tryTransfer in interface TransferQueue<E>
Parameters:
e - the element to transfer
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
true if successful, or false if the specified waiting time elapses before completion, in which case the element is not left enqueued
Throws:
java.lang.InterruptedException - if interrupted while waiting, in which case the element is not left enqueued

hasWaitingConsumer

public boolean hasWaitingConsumer()
Description copied from interface: TransferQueue
Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take() or timed poll. The return value represents a momentary state of affairs.

Specified by:
hasWaitingConsumer in interface TransferQueue<E>
Returns:
true if there is at least one waiting consumer

getWaitingConsumerCount

public int getWaitingConsumerCount()
Description copied from interface: TransferQueue
Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll. The return value is an approximation of a momentary state of affairs, that may be inaccurate if consumers have completed or given up waiting. The value may be useful for monitoring and heuristics, but not for synchronization control. Implementations of this method are likely to be noticeably slower than those for TransferQueue.hasWaitingConsumer().

Specified by:
getWaitingConsumerCount in interface TransferQueue<E>
Returns:
the number of consumers waiting to receive elements

offer

public boolean offer(E e)
Specified by:
offer in interface java.util.concurrent.BlockingDeque<E>
Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Specified by:
offer in interface java.util.Deque<E>
Specified by:
offer in interface java.util.Queue<E>
Overrides:
offer in class java.util.concurrent.LinkedBlockingDeque<E>


Copyright © 2009-2011 JolBox. All Rights Reserved.