mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-26 14:02:06 +01:00
Add ArrayBlockingQueueWithShutdown.tryTake()
This commit is contained in:
parent
751c7b0d40
commit
8f691e4b1d
1 changed files with 66 additions and 0 deletions
|
@ -360,6 +360,72 @@ public class ArrayBlockingQueueWithShutdown<E> extends AbstractQueue<E> implemen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum TryTakeResultCode {
|
||||||
|
/**
|
||||||
|
* The method was unable to acquire the queue lock.
|
||||||
|
*/
|
||||||
|
couldNotLock,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The queue was shut down.
|
||||||
|
*/
|
||||||
|
queueWasShutDown,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The queue was empty.
|
||||||
|
*/
|
||||||
|
queueWasEmpty,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An element was successfully removed from the queue.
|
||||||
|
*/
|
||||||
|
takeSuccessful,
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class TryTakeResult<E> {
|
||||||
|
private final E element;
|
||||||
|
private final TryTakeResultCode resultCode;
|
||||||
|
|
||||||
|
private TryTakeResult(TryTakeResultCode resultCode) {
|
||||||
|
assert resultCode != null;
|
||||||
|
this.resultCode = resultCode;
|
||||||
|
this.element = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TryTakeResult(E element) {
|
||||||
|
assert element != null;
|
||||||
|
this.resultCode = TryTakeResultCode.takeSuccessful;
|
||||||
|
this.element = element;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TryTakeResultCode getResultCode() {
|
||||||
|
return resultCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E getElement() {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TryTakeResult<E> tryTake() {
|
||||||
|
boolean locked = lock.tryLock();
|
||||||
|
if (!locked) {
|
||||||
|
return new TryTakeResult<E>(TryTakeResultCode.couldNotLock);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (isShutdown) {
|
||||||
|
return new TryTakeResult<E>(TryTakeResultCode.queueWasShutDown);
|
||||||
|
}
|
||||||
|
if (hasNoElements()) {
|
||||||
|
return new TryTakeResult<E>(TryTakeResultCode.queueWasEmpty);
|
||||||
|
}
|
||||||
|
E element = extract();
|
||||||
|
return new TryTakeResult<E>(element);
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
|
||||||
long nanos = unit.toNanos(timeout);
|
long nanos = unit.toNanos(timeout);
|
||||||
|
|
Loading…
Reference in a new issue