mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-21 22:02:06 +01:00
Improve ArrayBlockingQueueWithShutdown.tryPut()
to return the internal state when the operation is performed.
This commit is contained in:
parent
27c77fcb1c
commit
751c7b0d40
1 changed files with 30 additions and 5 deletions
|
@ -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);
|
||||
|
||||
boolean locked = lock.tryLock();
|
||||
if (!locked) {
|
||||
return false;
|
||||
return TryPutResult.couldNotLock;
|
||||
}
|
||||
try {
|
||||
if (isShutdown || isFull()) {
|
||||
return false;
|
||||
if (isShutdown) {
|
||||
return TryPutResult.queueWasShutDown;
|
||||
}
|
||||
if (isFull()) {
|
||||
return TryPutResult.queueWasFull;
|
||||
}
|
||||
|
||||
insert(e);
|
||||
return true;
|
||||
return TryPutResult.putSuccessful;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue