info.unterrainer.commons.jreutils.collections.DataQueue Maven / Gradle / Ivy
package info.unterrainer.commons.jreutils.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.collections4.queue.CircularFifoQueue;
/**
* A synchronized, size-limited FIFO-queue.
*/
public class DataQueue {
private CircularFifoQueue queue;
public DataQueue(final int maxSize) {
this(maxSize, (Collection) null);
}
public DataQueue(final int maxSize, final Collection collection) {
queue = new CircularFifoQueue<>(maxSize);
if (collection != null)
for (T element : collection)
queue.offer(element);
}
public DataQueue(final int maxSize, final T[] array) {
queue = new CircularFifoQueue<>(maxSize);
if (array != null)
for (T element : array)
queue.offer(element);
}
public synchronized DataQueue offer(final T element) {
queue.offer(element);
return this;
}
public synchronized T poll() {
return queue.poll();
}
public synchronized T peek() {
return queue.peek();
}
public synchronized void clear() {
queue.clear();
}
public synchronized List getListClone() {
List list = new ArrayList<>();
list.addAll(queue);
return list;
}
public synchronized List getListCloneAndClear() {
List list = new ArrayList<>();
list.addAll(queue);
queue.clear();
return list;
}
}