diff --git a/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java b/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java index cec4b3c95..0b933909e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/StanzaCollector.java @@ -17,6 +17,8 @@ package org.jivesoftware.smack; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; @@ -268,6 +270,27 @@ public class StanzaCollector { return result; } + private List collectedCache; + + /** + * Return a list of all collected stanzas. This method must be invoked after the collector has been cancelled. + * + * @return a list of collected stanzas. + * @since 4.3.0 + */ + public List getCollectedStanzasAfterCancelled() { + if (!cancelled) { + throw new IllegalStateException("Stanza collector was not yet cancelled"); + } + + if (collectedCache == null) { + collectedCache = new ArrayList<>(getCollectedCount()); + resultQueue.drainTo(collectedCache); + } + + return collectedCache; + } + /** * Get the number of collected stanzas this stanza collector has collected so far. *