Package com.turtlequeue
Class GrowableArrayBlockingQueue<T>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<T>
-
- com.turtlequeue.GrowableArrayBlockingQueue<T>
-
- All Implemented Interfaces:
Iterable<T>,Collection<T>,BlockingQueue<T>,Queue<T>
public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements BlockingQueue<T>
This implements aBlockingQueuebacked by an array with no fixed capacity.When the capacity is reached, data will be moved to a bigger array.
-
-
Constructor Summary
Constructors Constructor Description GrowableArrayBlockingQueue()GrowableArrayBlockingQueue(int initialCapacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T e)voidclear()intdrainTo(Collection<? super T> c)intdrainTo(Collection<? super T> c, int maxElements)Telement()static intfindNextPositivePowerOfTwo(int value)Fast method of finding the next power of 2 greater than or equal to the supplied value.voidforEach(Consumer<? super T> action)Iterator<T>iterator()booleanoffer(T e)booleanoffer(T e, long timeout, TimeUnit unit)Tpeek()Tpoll()Tpoll(long timeout, TimeUnit unit)voidput(T e)intremainingCapacity()Tremove()booleanremove(Object o)static intsafeFindNextPositivePowerOfTwo(int value)Fast method of finding the next power of 2 greater than or equal to the supplied value.intsize()Ttake()List<T>toList()StringtoString()-
Methods inherited from class java.util.AbstractQueue
addAll
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.BlockingQueue
contains
-
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Method Detail
-
findNextPositivePowerOfTwo
public static int findNextPositivePowerOfTwo(int value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.If the value is
<= 0then 1 will be returned. This method is not suitable forInteger.MIN_VALUEor numbers greater than 2^30.- Parameters:
value- from which to search for next power of 2- Returns:
- The next power of 2 or the value itself if it is a power of 2
-
safeFindNextPositivePowerOfTwo
public static int safeFindNextPositivePowerOfTwo(int value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.This method will do runtime bounds checking and call
findNextPositivePowerOfTwo(int)if within a valid range.- Parameters:
value- from which to search for next power of 2- Returns:
- The next power of 2 or the value itself if it is a power of 2.
Special cases for return values are as follows:
<= 0-> 1>= 2^30-> 2^30
-
remove
public T remove()
-
element
public T element()
-
offer
public boolean offer(T e)
-
put
public void put(T e)
- Specified by:
putin interfaceBlockingQueue<T>
-
add
public boolean add(T e)
- Specified by:
addin interfaceBlockingQueue<T>- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceQueue<T>- Overrides:
addin classAbstractQueue<T>
-
offer
public boolean offer(T e, long timeout, TimeUnit unit)
- Specified by:
offerin interfaceBlockingQueue<T>
-
take
public T take() throws InterruptedException
- Specified by:
takein interfaceBlockingQueue<T>- Throws:
InterruptedException
-
poll
public T poll(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
pollin interfaceBlockingQueue<T>- Throws:
InterruptedException
-
remainingCapacity
public int remainingCapacity()
- Specified by:
remainingCapacityin interfaceBlockingQueue<T>
-
drainTo
public int drainTo(Collection<? super T> c)
- Specified by:
drainToin interfaceBlockingQueue<T>
-
drainTo
public int drainTo(Collection<? super T> c, int maxElements)
- Specified by:
drainToin interfaceBlockingQueue<T>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractQueue<T>
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceBlockingQueue<T>- Specified by:
removein interfaceCollection<T>- Overrides:
removein classAbstractCollection<T>
-
size
public int size()
- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-
iterator
public Iterator<T> iterator()
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
toString
public String toString()
- Overrides:
toStringin classAbstractCollection<T>
-
-