aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/util/SyncedRingbuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/com/jogamp/common/util/SyncedRingbuffer.java')
-rw-r--r--src/java/com/jogamp/common/util/SyncedRingbuffer.java94
1 files changed, 47 insertions, 47 deletions
diff --git a/src/java/com/jogamp/common/util/SyncedRingbuffer.java b/src/java/com/jogamp/common/util/SyncedRingbuffer.java
index 747629b..8ef2970 100644
--- a/src/java/com/jogamp/common/util/SyncedRingbuffer.java
+++ b/src/java/com/jogamp/common/util/SyncedRingbuffer.java
@@ -3,14 +3,14 @@
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
- *
+ *
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
- *
+ *
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
@@ -20,7 +20,7 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
+ *
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
@@ -31,7 +31,7 @@ package com.jogamp.common.util;
import java.io.PrintStream;
import java.lang.reflect.Array;
-/**
+/**
* Simple synchronized implementation of {@link Ringbuffer}.
* <p>
* All methods utilize global synchronization.
@@ -56,12 +56,12 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
private int readPos;
private int writePos;
private int size;
-
+
@Override
public final String toString() {
return "SyncedRingbuffer<?>[filled "+size+" / "+capacity+", writePos "+writePos+", readPos "+readPos+"]";
}
-
+
@Override
public final void dump(PrintStream stream, String prefix) {
stream.println(prefix+" "+toString()+" {");
@@ -70,10 +70,10 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
}
stream.println("}");
}
-
- /**
+
+ /**
* Create a full ring buffer instance w/ the given array's net capacity and content.
- * <p>
+ * <p>
* Example for a 10 element Integer array:
* <pre>
* Integer[] source = new Integer[10];
@@ -89,7 +89,7 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
* and copy all elements from array <code>copyFrom</code> into the internal array.
* </p>
* @param copyFrom mandatory source array determining ring buffer's net {@link #capacity()} and initial content.
- * @throws IllegalArgumentException if <code>copyFrom</code> is <code>null</code>
+ * @throws IllegalArgumentException if <code>copyFrom</code> is <code>null</code>
*/
@SuppressWarnings("unchecked")
public SyncedRingbuffer(T[] copyFrom) throws IllegalArgumentException {
@@ -97,10 +97,10 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
array = (T[]) newArray(copyFrom.getClass(), capacity);
resetImpl(true, copyFrom);
}
-
- /**
+
+ /**
* Create an empty ring buffer instance w/ the given net <code>capacity</code>.
- * <p>
+ * <p>
* Example for a 10 element Integer array:
* <pre>
* Ringbuffer<Integer> rb = new SyncedRingbuffer<Integer>(10, Integer[].class);
@@ -120,13 +120,13 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
this.array = (T[]) newArray(arrayType, capacity);
resetImpl(false, null /* empty, nothing to copy */ );
}
-
+
@Override
public final T[] getInternalArray() { return array; }
-
+
@Override
public final int capacity() { return capacity; }
-
+
/**
* {@inheritDoc}
* <p>
@@ -142,12 +142,12 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
}
}
}
-
+
@Override
public final void resetFull(T[] copyFrom) throws IllegalArgumentException {
resetImpl(true, copyFrom);
}
-
+
private final void resetImpl(boolean full, T[] copyFrom) throws IllegalArgumentException {
synchronized ( syncGlobal ) {
if( null != copyFrom ) {
@@ -163,14 +163,14 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
size = full ? capacity : 0;
}
}
-
+
@Override
public final int size() {
synchronized ( syncGlobal ) {
return size;
}
}
-
+
@Override
public final int getFreeSlots() {
synchronized ( syncGlobal ) {
@@ -184,14 +184,14 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
return 0 == size;
}
}
-
+
@Override
public final boolean isFull() {
synchronized ( syncGlobal ) {
return capacity == size;
}
}
-
+
/**
* {@inheritDoc}
* <p>
@@ -215,7 +215,7 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
public final T getBlocking() throws InterruptedException {
return getImpl(true, false);
}
-
+
@Override
public final T peek() {
try {
@@ -226,9 +226,9 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
public final T peekBlocking() throws InterruptedException {
return getImpl(true, true);
}
-
+
private final T getImpl(boolean blocking, boolean peek) throws InterruptedException {
- synchronized( syncGlobal ) {
+ synchronized( syncGlobal ) {
if( 0 == size ) {
if( blocking ) {
while( 0 == size ) {
@@ -249,8 +249,8 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
return r;
}
}
-
- /**
+
+ /**
* {@inheritDoc}
* <p>
* Implementation stores the element at the current write position and advances it, if not full.
@@ -262,8 +262,8 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
return putImpl(e, false, false);
} catch (InterruptedException ie) { throw new RuntimeException(ie); }
}
-
- /**
+
+ /**
* {@inheritDoc}
* <p>
* Implementation stores the element at the current write position and advances it, if not full.
@@ -275,8 +275,8 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
throw new InternalError("Blocking put failed: "+this);
}
}
-
- /**
+
+ /**
* {@inheritDoc}
* <p>
* Implementation keeps the element at the current write position and advances it, if not full.
@@ -286,9 +286,9 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
public final boolean putSame(boolean blocking) throws InterruptedException {
return putImpl(null, true, blocking);
}
-
+
private final boolean putImpl(T e, boolean sameRef, boolean blocking) throws InterruptedException {
- synchronized( syncGlobal ) {
+ synchronized( syncGlobal ) {
if( capacity == size ) {
if( blocking ) {
while( capacity == size ) {
@@ -308,7 +308,7 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
return true;
}
}
-
+
@Override
public final void waitForFreeSlots(int count) throws InterruptedException {
synchronized ( syncGlobal ) {
@@ -319,8 +319,8 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
}
}
}
-
-
+
+
@Override
public final void growEmptyBuffer(final T[] newElements) throws IllegalStateException, IllegalArgumentException {
synchronized ( syncGlobal ) {
@@ -340,15 +340,15 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
if( readPos != writePos ) {
throw new InternalError("R/W pos not equal: "+this);
}
-
+
final int growAmount = newElements.length;
final int newCapacity = capacity + growAmount;
final T[] oldArray = array;
final T[] newArray = (T[]) newArray(arrayTypeInternal, newCapacity);
-
+
// writePos == readPos
writePos += growAmount; // warp writePos to the end of the new data location
-
+
if( readPos > 0 ) {
System.arraycopy(oldArray, 0, newArray, 0, readPos);
}
@@ -360,12 +360,12 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
System.arraycopy(oldArray, readPos, newArray, writePos, tail);
}
size = growAmount;
-
+
capacity = newCapacity;
array = newArray;
}
}
-
+
@Override
public final void growFullBuffer(final int growAmount) throws IllegalStateException, IllegalArgumentException {
synchronized ( syncGlobal ) {
@@ -374,7 +374,7 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
}
if( capacity != size ) {
throw new IllegalStateException("Buffer is not full: "+this);
- }
+ }
if( readPos != writePos ) {
throw new InternalError("R/W pos not equal: "+this);
}
@@ -384,10 +384,10 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
final int newCapacity = capacity + growAmount;
final T[] oldArray = array;
final T[] newArray = (T[]) newArray(arrayTypeInternal, newCapacity);
-
+
// writePos == readPos
readPos += growAmount; // warp readPos to the end of the new data location
-
+
if(writePos > 0) {
System.arraycopy(oldArray, 0, newArray, 0, writePos);
}
@@ -395,12 +395,12 @@ public class SyncedRingbuffer<T> implements Ringbuffer<T> {
if( tail > 0 ) {
System.arraycopy(oldArray, writePos, newArray, readPos, tail);
}
-
+
capacity = newCapacity;
array = newArray;
}
}
-
+
@SuppressWarnings("unchecked")
private static <T> T[] newArray(Class<? extends T[]> arrayType, int length) {
return ((Object)arrayType == (Object)Object[].class)