From 751c7b0d4042ed56ef1eb7ed2b5728bb158d0be4 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 31 May 2018 17:19:36 +0200 Subject: [PATCH] Improve ArrayBlockingQueueWithShutdown.tryPut() to return the internal state when the operation is performed. --- .../util/ArrayBlockingQueueWithShutdown.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java index 3f860a16a..ada94e02f 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java @@ -263,20 +263,45 @@ public class ArrayBlockingQueueWithShutdown extends AbstractQueue 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(); }