jaulib v1.3.0
Jau Support Library (C++, Java, ..)
|
Simple implementation of Ringbuffer
, exposing lock-free get*(..)
and put*(..)
methods.
More...
Public Member Functions | |||||
final String | toString () | ||||
Returns a short string representation incl. More... | |||||
final void | dump (final PrintStream stream, final String prefix) | ||||
Debug functionality - Dumps the contents of the internal array. More... | |||||
LFRingbuffer (final T[] copyFrom) throws IllegalArgumentException | |||||
Create a full ring buffer instance w/ the given array's net capacity and content. More... | |||||
LFRingbuffer (final Class<? extends T[]> arrayType, final int capacity) | |||||
Create an empty ring buffer instance w/ the given net capacity . More... | |||||
final int | capacity () | ||||
Returns the net capacity of this ring buffer. More... | |||||
final void | clear () | ||||
Resets the read and write position according to an empty ring buffer and set all ring buffer slots to null . More... | |||||
final void | resetFull (final T[] copyFrom) throws IllegalArgumentException | ||||
Resets the read and write position according to a full ring buffer and fill all slots w/ elements of array copyFrom . More... | |||||
final int | size () | ||||
Returns the number of elements in this ring buffer. More... | |||||
final int | getFreeSlots () | ||||
Returns the number of free slots available to put. More... | |||||
final boolean | isEmpty () | ||||
Returns true if this ring buffer is empty, otherwise false. More... | |||||
final boolean | isFull () | ||||
Returns true if this ring buffer is full, otherwise false. More... | |||||
final T | get () | ||||
Dequeues the oldest enqueued element if available, otherwise null.The returned ring buffer slot will be set to null to release the reference and move ownership to the caller. Method is non blocking and returns immediately;.
| |||||
final T | getBlocking () throws InterruptedException | ||||
Dequeues the oldest enqueued element.The returned ring buffer slot will be set to null to release the reference and move ownership to the caller. Methods blocks until an element becomes available via put.
| |||||
final T | peek () | ||||
Peeks the next element at the read position w/o modifying pointer, nor blocking. More... | |||||
final T | peekBlocking () throws InterruptedException | ||||
Peeks the next element at the read position w/o modifying pointer, but w/ blocking. More... | |||||
final boolean | put (final T e) | ||||
Enqueues the given element.Returns true if successful, otherwise false in case buffer is full. Method is non blocking and returns immediately;. More... | |||||
final void | putBlocking (final T e) throws InterruptedException | ||||
Enqueues the given element.Method blocks until a free slot becomes available via get.
| |||||
final boolean | putSame (final boolean blocking) throws InterruptedException | ||||
Enqueues the same element at it's write position, if not full.Returns true if successful, otherwise false in case buffer is full. If blocking is true, method blocks until a free slot becomes available via get.
| |||||
final void | waitForFreeSlots (final int count) throws InterruptedException | ||||
Blocks until at least count free slots become available. More... | |||||
final void | growEmptyBuffer (final T[] newElements) throws IllegalStateException, IllegalArgumentException | ||||
Grows an empty ring buffer, increasing it's capacity about the amount. More... | |||||
final void | growFullBuffer (final int growAmount) throws IllegalStateException, IllegalArgumentException | ||||
Grows a full ring buffer, increasing it's capacity about the amount. More... | |||||
String | toString () | ||||
Returns a short string representation incl. More... | |||||
void | dump (PrintStream stream, String prefix) | ||||
Debug functionality - Dumps the contents of the internal array. More... | |||||
int | capacity () | ||||
Returns the net capacity of this ring buffer. More... | |||||
void | clear () | ||||
Resets the read and write position according to an empty ring buffer and set all ring buffer slots to null . More... | |||||
void | resetFull (T[] copyFrom) throws IllegalArgumentException | ||||
Resets the read and write position according to a full ring buffer and fill all slots w/ elements of array copyFrom . More... | |||||
int | size () | ||||
Returns the number of elements in this ring buffer. More... | |||||
int | getFreeSlots () | ||||
Returns the number of free slots available to put. More... | |||||
boolean | isEmpty () | ||||
Returns true if this ring buffer is empty, otherwise false. More... | |||||
boolean | isFull () | ||||
Returns true if this ring buffer is full, otherwise false. More... | |||||
T | get () | ||||
Dequeues the oldest enqueued element if available, otherwise null. More... | |||||
T | getBlocking () throws InterruptedException | ||||
Dequeues the oldest enqueued element. More... | |||||
T | peek () | ||||
Peeks the next element at the read position w/o modifying pointer, nor blocking. More... | |||||
T | peekBlocking () throws InterruptedException | ||||
Peeks the next element at the read position w/o modifying pointer, but w/ blocking. More... | |||||
boolean | put (T e) | ||||
Enqueues the given element. More... | |||||
void | putBlocking (T e) throws InterruptedException | ||||
Enqueues the given element. More... | |||||
boolean | putSame (boolean blocking) throws InterruptedException | ||||
Enqueues the same element at it's write position, if not full. More... | |||||
void | waitForFreeSlots (int count) throws InterruptedException | ||||
Blocks until at least count free slots become available. More... | |||||
void | growEmptyBuffer (T[] newElements) throws IllegalStateException, IllegalArgumentException | ||||
Grows an empty ring buffer, increasing it's capacity about the amount. More... | |||||
void | growFullBuffer (int amount) throws IllegalStateException, IllegalArgumentException | ||||
Grows a full ring buffer, increasing it's capacity about the amount. More... | |||||
Simple implementation of Ringbuffer
, exposing lock-free get*(..)
and put*(..)
methods.
Implementation utilizes the Always Keep One Slot Open, hence implementation maintains an internal array of capacity
plus one!
Implementation is thread safe if:
get*(..)
operations are performed from one thread only. put*(..)
operations are performed from one thread only. get*(..)
and put*(..)
thread may be the same. Following methods utilize global synchronization:
User needs to synchronize above methods w/ the lock-free w/ get*(..)
and put*(..)
methods, e.g. by controlling their threads before invoking the above.
Characteristics:
Empty | writePos == readPos | size == 0 |
Full | writePos == readPos - 1 | size == capacity |
Definition at line 71 of file LFRingbuffer.java.
org.jau.util.LFRingbuffer< T >.LFRingbuffer | ( | final T[] | copyFrom | ) | throws IllegalArgumentException |
Create a full ring buffer instance w/ the given array's net capacity and content.
Example for a 10 element Integer array:
Integer[] source = new Integer[10]; // fill source with content .. Ringbuffer<Integer> rb = new LFRingbuffer<Integer>(source);
isFull()
returns true on the newly created full ring buffer.
Implementation will allocate an internal array with size of array copyFrom
plus one, and copy all elements from array copyFrom
into the internal array.
copyFrom | mandatory source array determining ring buffer's net capacity() and initial content. |
IllegalArgumentException | if copyFrom is null |
Definition at line 117 of file LFRingbuffer.java.
org.jau.util.LFRingbuffer< T >.LFRingbuffer | ( | final Class<? extends T[]> | arrayType, |
final int | capacity | ||
) |
Create an empty ring buffer instance w/ the given net capacity
.
Example for a 10 element Integer array:
Ringbuffer<Integer> rb = new LFRingbuffer<Integer>(10, Integer[].class);
isEmpty()
returns true on the newly created empty ring buffer.
Implementation will allocate an internal array of size capacity
plus one.
arrayType | the array type of the created empty internal array. |
capacity | the initial net capacity of the ring buffer |
Definition at line 140 of file LFRingbuffer.java.
final int org.jau.util.LFRingbuffer< T >.capacity | ( | ) |
Returns the net capacity of this ring buffer.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 147 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.clear | ( | ) |
Resets the read and write position according to an empty ring buffer and set all ring buffer slots to null
.
isEmpty()
will return true
after calling this method.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 150 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.dump | ( | final PrintStream | stream, |
final String | prefix | ||
) |
Debug functionality - Dumps the contents of the internal array.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 88 of file LFRingbuffer.java.
final T org.jau.util.LFRingbuffer< T >.get | ( | ) |
Dequeues the oldest enqueued element if available, otherwise null.The returned ring buffer slot will be set to null
to release the reference and move ownership to the caller. Method is non blocking and returns immediately;.
Implementation advances the read position and returns the element at it, if not empty.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 205 of file LFRingbuffer.java.
final T org.jau.util.LFRingbuffer< T >.getBlocking | ( | ) | throws InterruptedException |
Dequeues the oldest enqueued element.The returned ring buffer slot will be set to null
to release the reference and move ownership to the caller. Methods blocks until an element becomes available via put.
InterruptedException |
Implementation advances the read position and returns the element at it, if not empty.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 218 of file LFRingbuffer.java.
final int org.jau.util.LFRingbuffer< T >.getFreeSlots | ( | ) |
Returns the number of free slots available to put.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 190 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.growEmptyBuffer | ( | final T[] | newElements | ) | throws IllegalStateException, IllegalArgumentException |
Grows an empty ring buffer, increasing it's capacity about the amount.
Growing an empty ring buffer increases it's size about the amount, i.e. renders it not empty. The new elements are inserted at the read position, able to be read out via get()
etc.
newElements | array of new full elements the empty buffer shall grow about. |
IllegalStateException | if buffer is not empty |
IllegalArgumentException | if newElements is null |
Implements org.jau.util.Ringbuffer< T >.
Definition at line 334 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.growFullBuffer | ( | final int | amount | ) | throws IllegalStateException, IllegalArgumentException |
Grows a full ring buffer, increasing it's capacity about the amount.
Growing a full ring buffer leaves the size intact, i.e. renders it not full. New null
elements are inserted at the write position, able to be written to via put(Object)
etc.
amount | the amount of elements the buffer shall grow about |
IllegalStateException | if buffer is not full |
IllegalArgumentException | if amount is < 0 |
Implements org.jau.util.Ringbuffer< T >.
Definition at line 382 of file LFRingbuffer.java.
final boolean org.jau.util.LFRingbuffer< T >.isEmpty | ( | ) |
Returns true if this ring buffer is empty, otherwise false.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 193 of file LFRingbuffer.java.
final boolean org.jau.util.LFRingbuffer< T >.isFull | ( | ) |
Returns true if this ring buffer is full, otherwise false.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 196 of file LFRingbuffer.java.
final T org.jau.util.LFRingbuffer< T >.peek | ( | ) |
Peeks the next element at the read position w/o modifying pointer, nor blocking.
null
if empty, otherwise the element which would be read next. Implements org.jau.util.Ringbuffer< T >.
Definition at line 223 of file LFRingbuffer.java.
final T org.jau.util.LFRingbuffer< T >.peekBlocking | ( | ) | throws InterruptedException |
Peeks the next element at the read position w/o modifying pointer, but w/ blocking.
null
if empty, otherwise the element which would be read next. Implements org.jau.util.Ringbuffer< T >.
Definition at line 229 of file LFRingbuffer.java.
final boolean org.jau.util.LFRingbuffer< T >.put | ( | final T | e | ) |
Enqueues the given element.Returns true if successful, otherwise false in case buffer is full. Method is non blocking and returns immediately;.
Implementation advances the write position and stores the given element at it, if not full.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 266 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.putBlocking | ( | final T | e | ) | throws InterruptedException |
Enqueues the given element.Method blocks until a free slot becomes available via get.
InterruptedException |
Implementation advances the write position and stores the given element at it, if not full.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 279 of file LFRingbuffer.java.
final boolean org.jau.util.LFRingbuffer< T >.putSame | ( | final boolean | blocking | ) | throws InterruptedException |
Enqueues the same element at it's write position, if not full.Returns true if successful, otherwise false in case buffer is full. If blocking
is true, method blocks until a free slot becomes available via get.
blocking | if true, wait until a free slot becomes available via get. |
InterruptedException |
Implementation advances the write position and keeps the element at it, if not full.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 292 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.resetFull | ( | final T[] | copyFrom | ) | throws IllegalArgumentException |
Resets the read and write position according to a full ring buffer and fill all slots w/ elements of array copyFrom
.
Array's copyFrom
elements will be copied into the internal array, hence it's length must be equal to capacity()
.
copyFrom | Mandatory array w/ length capacity() to be copied into the internal array. |
IllegalArgumentException | if copyFrom is null . |
IllegalArgumentException | if copyFrom 's length is different from capacity() . |
Implements org.jau.util.Ringbuffer< T >.
Definition at line 160 of file LFRingbuffer.java.
final int org.jau.util.LFRingbuffer< T >.size | ( | ) |
Returns the number of elements in this ring buffer.
Implements org.jau.util.Ringbuffer< T >.
Definition at line 187 of file LFRingbuffer.java.
final String org.jau.util.LFRingbuffer< T >.toString | ( | ) |
Returns a short string representation incl.
size/capacity and internal r/w index (impl. dependent).
Implements org.jau.util.Ringbuffer< T >.
Definition at line 83 of file LFRingbuffer.java.
final void org.jau.util.LFRingbuffer< T >.waitForFreeSlots | ( | final int | count | ) | throws InterruptedException |
Blocks until at least count
free slots become available.
InterruptedException |
Implements org.jau.util.Ringbuffer< T >.
Definition at line 323 of file LFRingbuffer.java.