Skip to content

Instantly share code, notes, and snippets.

@trentonstrong
Created January 17, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trentonstrong/e011c60816f313cf5144d053d7b41e9f to your computer and use it in GitHub Desktop.
Save trentonstrong/e011c60816f313cf5144d053d7b41e9f to your computer and use it in GitHub Desktop.
This is WIP
package org.trentonstrong;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BlockingCircularBuffer implements Queue<Integer>, BlockingQueue<Integer> {
private final CircularBuffer buffer;
private Lock bufferLock = new ReentrantLock();
private Condition bufferNotFull = bufferLock.newCondition();
private Condition bufferNotEmpty = bufferLock.newCondition();
public BlockingCircularBuffer(int capacity) {
buffer = new CircularBuffer(capacity);
}
@Override
public boolean add(Integer e) {
bufferLock.lock();
try {
buffer.add(e);
bufferNotEmpty.signal();
return true;
} finally {
bufferLock.unlock();
}
}
@Override
public boolean offer(Integer e) {
bufferLock.lock();
try {
boolean added = buffer.offer(e);
if (added) {
bufferNotEmpty.signal();
}
return added;
} finally {
bufferLock.unlock();
}
}
@Override
public Integer remove() {
bufferLock.lock();
try {
int e = buffer.remove();
bufferNotFull.signal();
return e;
} finally {
bufferLock.unlock();
}
}
@Override
public Integer poll() {
bufferLock.lock();
try {
Integer e = buffer.poll();
if (e != null) {
bufferNotFull.signal();
}
return e;
} finally {
bufferLock.unlock();
}
}
@Override
public Integer element() {
bufferLock.lock();
try {
return buffer.element();
} finally {
bufferLock.unlock();
}
}
@Override
public Integer peek() {
bufferLock.lock();
try {
return buffer.peek();
} finally {
bufferLock.unlock();
}
}
@Override
public void put(Integer e) throws InterruptedException {
bufferLock.lock();
try {
while (remainingCapacity() == 0) {
bufferNotFull.await();
}
buffer.add(e);
bufferNotEmpty.signal();
} finally {
bufferLock.unlock();
}
}
@Override
public boolean offer(Integer e, long timeout, TimeUnit unit) throws InterruptedException {
bufferLock.lock();
try {
final boolean elapsed = bufferNotFull.await(timeout, unit);
if (!elapsed) {
buffer.add(e);
bufferNotEmpty.signal();
}
return elapsed;
} finally {
bufferLock.unlock();
}
}
@Override
public Integer take() throws InterruptedException {
bufferLock.lock();
try {
while(size() == 0) {
bufferNotEmpty.await();
}
int e = buffer.remove();
bufferNotFull.signal();
return e;
} finally {
bufferLock.unlock();
}
}
@Override
public Integer poll(long timeout, TimeUnit unit) throws InterruptedException {
return null;
}
@Override
public int remainingCapacity() {
return buffer.capacity - buffer.size();
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends Integer> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<Integer> iterator() {
return null;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
return null;
}
@Override
public int drainTo(Collection<? super Integer> c) {
return 0;
}
@Override
public int drainTo(Collection<? super Integer> c, int maxElements) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment