1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Improve ArrayBlockingQueueWithShutdown.tryPut()

to return the internal state when the operation is performed.
This commit is contained in:
Florian Schmaus 2018-05-31 17:19:36 +02:00
parent 27c77fcb1c
commit 751c7b0d40

View file

@ -263,20 +263,45 @@ public class ArrayBlockingQueueWithShutdown<E> extends AbstractQueue<E> implemen
} }
} }
public boolean tryPut(E e) { public enum TryPutResult {
/**
* The method was unable to acquire the queue lock.
*/
couldNotLock,
/**
* The queue was shut down.
*/
queueWasShutDown,
/**
* The method was unable to put another element into the queue because the queue was full.
*/
queueWasFull,
/**
* The element was successfully placed into the queue.
*/
putSuccessful,
}
public TryPutResult tryPut(E e) {
checkNotNull(e); checkNotNull(e);
boolean locked = lock.tryLock(); boolean locked = lock.tryLock();
if (!locked) { if (!locked) {
return false; return TryPutResult.couldNotLock;
} }
try { try {
if (isShutdown || isFull()) { if (isShutdown) {
return false; return TryPutResult.queueWasShutDown;
}
if (isFull()) {
return TryPutResult.queueWasFull;
} }
insert(e); insert(e);
return true; return TryPutResult.putSuccessful;
} finally { } finally {
lock.unlock(); lock.unlock();
} }