From d958b42effe6adf8cd33b8c808f7ef6f98b5a846 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 11 Jun 2018 18:25:07 +0200 Subject: [PATCH] Add StanzaCollector.getCollectedStanzasAfterCancelled() --- .../jivesoftware/smack/StanzaCollector.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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. *